本篇文章主要介绍一个简单的自动化案例脚本
public class TrainSearch {
private DesiredCapabilities desiredCapabilities = null;
private AndroidDriver<WebElement> androidDriver = null;
@BeforeTest
public void setAppStartConfig() throws MalformedURLException {
/**
* 1.创建配置对象
**/
desiredCapabilities = new DesiredCapabilities();
/**
* 2.添加配置
**/
//deviceName:找到测试设备
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62025");
//platformName:确定测试平台
desiredCapabilities.setCapability("platformName", "Android");
//appPackage:找到需要测试的APP
desiredCapabilities.setCapability("appPackage", "com.MobileTicket");
//appActivity:测试APP的启动入口
desiredCapabilities.setCapability("appActivity", "com.alipay.mobile.quinox.LauncherActivity");
//设置自动化使用的引擎
desiredCapabilities.setCapability("automationName", "uiautomator2");
//设置app启动不清除数据
desiredCapabilities.setCapability("noReset", true);
/**
* 3.创建驱动
**/
androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
}
@Test
public void testTrainSearch() {
/**
* 设置始发站
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/train_home_dep", 10).click();
androidDriver.context("WEBVIEW_com.MobileTicket");
FindElement.findElementByXpath(androidDriver, "//input[@placeholder='请输入城市/车站名'] ", 5).sendKeys("武汉");
FindElement.findElementByXpath(androidDriver, "//b[text()='武汉']//ancestor::li[1]", 3).click();
androidDriver.context("NATIVE_APP");
/**
* 设置终到站
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/train_home_arrive", 3).click();
IntervalTime.waitTime(2);
androidDriver.context("WEBVIEW_com.MobileTicket");
FindElement.findElementByXpath(androidDriver, "//input[@placeholder='请输入城市/车站名'] ", 6).sendKeys("上海");
FindElement.findElementByXpath(androidDriver, "//b[text()='上海']//ancestor::li[1]", 3).click();
androidDriver.context("NATIVE_APP");
/**
* 设置出发时间
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_depart_date_view_container", 3).click();
IntervalTime.waitTime(2);
androidDriver.context("WEBVIEW_com.MobileTicket");
FindElement.findElementByXpath(androidDriver, "//strong[text()=20]//ancestor::li[1]", 3).click();
androidDriver.context("NATIVE_APP");
/**
* 选中只看高铁/动车选项
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_type_checkbox", 3).click();
/**
* 单击查询按钮
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_btn_search", 0).click();
/**
* 校验页面Activity
**/
Assert.assertEquals(androidDriver.currentActivity(), "com.alipay.mobile.nebulacore.ui.H5Activity");
}
}
public class TrainSearch {
private DesiredCapabilities desiredCapabilities = null;
private AndroidDriver<WebElement> androidDriver = null;
@BeforeTest
public void setAppStartConfig() throws MalformedURLException {
/**
* 1.创建配置对象
**/
desiredCapabilities = new DesiredCapabilities();
/**
* 2.添加配置
**/
//deviceName:找到测试设备
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62025");
//platformName:确定测试平台
desiredCapabilities.setCapability("platformName", "Android");
//appPackage:找到需要测试的APP
desiredCapabilities.setCapability("appPackage", "com.MobileTicket");
//appActivity:测试APP的启动入口
desiredCapabilities.setCapability("appActivity", "com.alipay.mobile.quinox.LauncherActivity");
//设置自动化使用的引擎
desiredCapabilities.setCapability("automationName", "uiautomator2");
//设置app启动不清除数据
desiredCapabilities.setCapability("noReset", true);
/**
* 3.创建驱动
**/
androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
}
@Test
public void testTrainSearch() {
/**
* 设置始发站
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/train_home_dep", 10).click();
chooseStation(androidDriver, "武汉");
/**
* 设置终到站
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/train_home_arrive", 3).click();
IntervalTime.waitTime(2);
chooseStation(androidDriver, "上海");
/**
* 设置出发时间
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_depart_date_view_container", 3).click();
IntervalTime.waitTime(2);
androidDriver.context("WEBVIEW_com.MobileTicket");
FindElement.findElementByXpath(androidDriver, "//strong[text()=20]//ancestor::li[1]", 3).click();
androidDriver.context("NATIVE_APP");
/**
* 选中只看高铁/动车选项
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_type_checkbox", 3).click();
/**
* 单击查询按钮
**/
FindElement.findElementById(androidDriver, "com.MobileTicket.launcher:id/ticket_home_btn_search", 0).click();
/**
* 校验页面Activity
**/
Assert.assertEquals(androidDriver.currentActivity(), "com.alipay.mobile.nebulacore.ui.H5Activity");
}
/**
* 车站选择公共方法
**/
public void chooseStation(AndroidDriver<WebElement> androidDriver,String stationName) {
androidDriver.context("WEBVIEW_com.MobileTicket");
FindElement.findElementByXpath(androidDriver, "//input[@placeholder='请输入城市/车站名'] ", 6).sendKeys(stationName);
FindElement.findElementByXpath(androidDriver, "//b[text()='" + stationName + "']//ancestor::li[1]", 3).click();
androidDriver.context("NATIVE_APP");
}
}