在学习过Appium 启动设置,和Appium 控件封装之后,我们就可以用TestNG 来管理用例,
初始化设置Appium 参数放在@BeforeTest 方法内,销毁操作放在@AfterTest
中间是@Test 注解的用例
一个@Test 看成一条用例
整个脚本可以这么写
private static AndroidDriver driver;
@Test
public void f() throws InterruptedException, IOException {
Util.Object.sliding(driver, "", "", "左滑", "");
Util.Object.sliding(driver, "", "", "左滑", "");
Util.Object.sliding(driver, "", "", "左滑", "");
Util.Object.sliding(driver, "", "", "左滑", "");
Util.Object.click(driver, "By.name", "立即体验", "", "");
Thread.sleep(2000);
Assert.assertEquals("首页", driver.findElement(By.id("com.umaman.laiyifen:id/rb_home")).getText().toString().trim());
}
@Test
public void f1() throws InterruptedException, IOException {
Util.Object.click(driver, "By.name", "我的", "", "");
Util.Object.click(driver, "By.name", "登录", "", "");
Util.Object.input(driver, "By.id", "com.umaman.laiyifen:id/et_username", "13764142840", "");
Util.Object.input(driver, "By.id", "com.umaman.laiyifen:id/et_password", "123456", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/tv_login", "", "");
Thread.sleep(2000);
String NameInfo=driver.findElement(By.id("com.umaman.laiyifen:id/tv_user_name")).getText().toString().trim();
System.out.println("运行的值是"+NameInfo);
Assert.assertEquals("HI 13764142840", NameInfo);
}
@Test
public void f2() throws InterruptedException, IOException {
Util.Object.click(driver, "By.name", "分类", "", "");
Util.Object.click(driver, "By.name", "坚果炒货", "", "");
Util.Object.click(driver, "By.name", "花生", "", "");
Util.Object.click(driver, "By.name", "多味花生250g", "", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/addshopcar", "", "");
Util.Object.click(driver, "By.name", "确定", "", "");
String txt=driver.findElement(By.xpath("//android.widget.FrameLayout[contains(@index,0)]/android.widget.TextView[contains(@index,1)]")).getText().toString().trim();
System.out.println("获得的数量是"+txt);
Assert.assertEquals("1",txt);
}
@Test
public void f3() throws InterruptedException, IOException {
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/imgMessagesIcon", "", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/iv_edit", "", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/check", "", "");
Util.Object.click(driver, "By.name", "删除", "", "");
Util.Object.click(driver, "By.name", "确定", "", "");
String txt=driver.findElement(By.id("com.umaman.laiyifen:id/toSeck")).getText().toString().trim();
System.out.println("运行的值是"+txt);
Assert.assertEquals("去逛逛", txt);
}
@Test
public void f4() throws InterruptedException, IOException {
Util.Object.click(driver, "By.name", "我的", "", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/setting", "", "");
Util.Object.click(driver, "By.id", "com.umaman.laiyifen:id/logoutRelative", "", "");
Util.Object.click(driver, "By.id", "android:id/up", "", "");
String txt=driver.findElement(By.id("com.umaman.laiyifen:id/noLogin")).getText().toString().trim();
System.out.println("运行的值是"+txt);
Assert.assertEquals("", txt);
}
@BeforeTest
public void beforeTest() throws InterruptedException, MalformedURLException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps/ContactManager");
File app = new File(appDir, "laiyifen.apk");
//判断Apk 是否存在
if (!app.exists()){
System.out.println("本次需要安装的APK 不存在");
}
//设置启动参数
DesiredCapabilities capabilities = new DesiredCapabilities();
//设置浏览器类型 如果为空,就取用Appium 中设定的浏览器
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//设置键盘使得支持中文输入
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
//设置测试平台
capabilities.setCapability("platformName","Android");
//设置设备名称
capabilities.setCapability("deviceName","Android Emulator");
//设置Android 版本
capabilities.setCapability("platformVersion","4.4");
//设置Android 版本
capabilities.setCapability("udid","192.168.56.101:5555");
//获取绝对路径
capabilities.setCapability("app", app.getAbsolutePath());
//设置启动包名
capabilities.setCapability("app-package", "com.umaman.laiyifen");
//设置启动主Activity
capabilities.setCapability("app-activity", "com.laiyifen.app.activity.SplashActivity");
//设置代理服务器
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
//设置隐式等待超时时间
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
System.out.println("2222");
Thread.sleep(10000);
}
@AfterTest
public void afterTest() {
driver.close();
}