参考:http://www.cnblogs.com/tobecrazy/p/4579631.html
创建该项目之前请确保各方面的环境已准备好,环境搭建请参考:
http://www.360doc.com/showWeb/0/0/548808952.aspx
1.打开eclipse创建一java项目
2.添加appium+testng搭建的项目所需的包
2.1、添加TestNg(右键项目->Build Path->Configure Build Path...)
2.2、添加AndroidDriver及DesiredCapabilities依赖的包(注意它们的版本,版本不适合可能会出错)
java-client-2.2.0.jar
selenium-server-standalone-2.48.2.jar(版本低了会出错)
加完后的项目:
2.3、若需要再集成ReportNg,则需要加reportng所需的包(可选)
guice-3.0.jar
reportng-1.1.4.jar
velocity-dep-1.4.jar
3.在该项目下创建一TestNg Class,"new - > other"如图
点击 下一步 进入如下图页面(注意Source folder,默认会带上包,此处带包以后所创建的class存放的位置不对):
创建TestNg Class 会生成一testng.xml配置文件
4.AppiumTest.java代码内容
package com.appium.test;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class AppiumTest {
private AndroidDriver driver;
DesiredCapabilities capabilities = new DesiredCapabilities();
@Test
public void loginApp() {
//等待
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//获得用户名密码输入框
WebElement accountEle = driver.findElement(By.id("com.auto51.checker:id/et_account"));
accountEle.sendKeys("wenbin.li");
WebElement passwordEle = driver.findElement(By.id("com.auto51.checker:id/et_password"));
passwordEle.sendKeys("123456");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//获取检测师app登录按钮元素
WebElement loginButton = driver.findElement(By.id("com.auto51.checker:id/btn_login"));
loginButton.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.out.println("test1................................");
}
@BeforeMethod
public void beforeMethod() {
try {
//set up appium
File classpathRoot = new File(System.getProperty("user.dir"));//获取跟路径
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "checker_2.3.8.apk");//所测app存放的位置
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "a4572f75");//设备号,通过adb命令获得
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.auto51.checker");//所测app的包名
//支持中文
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
//no need sign
capabilities.setCapability("noSign", "True");
capabilities.setCapability("appActivity", ".ASplashActivity");//主activity,通过appium的客户端可以查看
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("set up.................................");
}
@AfterMethod
public void afterMethod() {
driver.close();
}
}
5.TestNg配置文件(该配置文件还是有挺多知识点的,很多属性需要注意,可以深究,这里仅为运行该demo配置)
6.检查你的appium是否开启/手机是否连接至电脑(在cmd命令行输入:appium 开启)
右键testng.xml配置文件,Run As -> TestNg Suite 则开始运行