1.打开eclipse
New---java Project 让我们新建一个java的工程. 直接Finish
然后build Path-----Add External Archives..... 将我们的java-client-2.2.0.jar、selenium-java-2.44.0.jar、selenium-server-standalone-2.44.0.jar
导入到我们工程下.
在src下新建一个package,在这里包名我们可以随意的去取
最后在包下新建一个class
接下来生成相应的代码:
package com.test.AppiumDriver;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class AppiumDriver {
private static AndroidDriver driver=null;
static{
try{
DesiredCapabilities desiredCapabilities=new DesiredCapabilities();
desiredCapabilities.setCapability("automationName","Appium");
desiredCapabilities.setCapability("platformName","Android"); //设置ios,android平台
desiredCapabilities.setCapability("deviceName","LZUS8DHMUWS46SQW");//adb devices获得测试机name
desiredCapabilities.setCapability("platformVersion","4.4.2"); //设置测试手机android型号
desiredCapabilities.setCapability("appPackage", "com.android.maiguo.activity");//测试包名
desiredCapabilities.setCapability("appActivity","com.android.maiguo.activity.MainActivity");//测试activity
desiredCapabilities.setCapability("sessionOverride", "true"); //每次启动时覆盖session,否则第二次后运行会报错不能新建session
desiredCapabilities.setCapability("resetKeyboard", "true"); //设置默认键盘为appium的键盘
desiredCapabilities.setCapability("unicodeKeyboard", "True");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
Thread.sleep(3000);
}catch (Exception e) {
e.printStackTrace();
}
}
public AndroidDriver getDriver(){ //提供外部获取driver的一个方法
return driver;
}
}
这个时候一个初始化driver的类就已经完成啦.我们可以在新建一个测试类来试试效果.
我们在新建一个run的Class类
package com.test.AppiumDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class run {
AndroidDriver driver = AppiumDriver.getDriver();
@BeforeClass
public void ready(){
new AppiumDriver();
}
@Test
public void demo(){
driver.findElementById("com.android.maiguo.activity:id/btn_login").click();
driver.findElementById("com.android.maiguo.activity:id/et_account").clear();
driver.findElementById("com.android.maiguo.activity:id/et_account").sendKeys("7154990");
driver.findElementById("com.android.maiguo.activity:id/et_password").clear();
driver.findElementById("com.android.maiguo.activity:id/et_password").sendKeys("123456");
driver.findElementById("com.android.maiguo.activity:id/btn_login").click();
}
@AfterClass
public void End(){
driver.quit();
}
}
将我们的Appium服务启动,运行脚本
这样我们的第一个脚本就已经启动成功啦.