1、App自动化测试框架
- 打开测试App代码封装为公共方法,提取到BaseTest类中,所有测试类都继承与BaseTest
public class BaseTest { public static AndroidDriver androidDriver; private Logger logger = Logger.getLogger(BaseTest.class); public void openApp() throws MalformedURLException { DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); desiredCapabilities.setCapability("deviceName", Constants.DEVICE_NAME); desiredCapabilities.setCapability("platformName",Constants.PLATFORM_NAME); desiredCapabilities.setCapability("appPackage",Constants.APP_PACAKGE); desiredCapabilities.setCapability("appActivity",Constants.APP_ACTIVITY); desiredCapabilities.setCapability("noReset",Constants.IS_RESET); URL url= new URL("http://"+Constants.APPIUM_URL+"/wd/hub");//http://127.0.0.1:4723/wd/hub androidDriver = new AndroidDriver(url,desiredCapabilities); logger.info("==========================打开测试App========================="); }
- App配置信息通过常量类Constants维护(public static final )
public class Constants { //测试设备名 public static final String DEVICE_NAME="127.0.0.1:62001"; //测试平台名 public static final String PLATFORM_NAME="Android"; //测试App包名 public static final String APP_PACAKGE="com.lemon.lemonban"; //测试App启动入口 public static final String APP_ACTIVITY="com.lemon.lemonban.activity.WelcomeActivity"; //是否清除测试App的数据,为true表示不清除App的数据,为false清除掉 public static final boolean IS_RESET=false; //Appium的地址 IP地址:端口号 public static final String APPIUM_URL="127.0.0.1:4723"; }
- 引入显示等待,避免找元素失败的问题
/* * 等待元素可见封装 */ public static WebElement waitElementVisible(By by){ //By对象里面会保存元素定位方式和元素定位 id->com.lemon.lemonban:id/navigation_tiku //怎么在方法里面获取到驱动?? WebDriverWait webDriverWait = new WebDriverWait(BaseTest.androidDriver,10); return webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by)); }
- Page Object设计模式-基于
1、提高测试用例可读性
2、提高后期的可维护性
- 引入Log4j日志,追溯问题/定位问题
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
- 引入Allure报告,对结果做统一统计,展示的效果会比较美观
- 失败用例截图,帮助定位问题
1、把截图保存到本地
2、把截图嵌入到Allure报表中
public class TestResultListener implements IHookable { //public int i = 1; @Override public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) { //在执行的时候替换掉@Test注解标注的测试方法 //System.out.println("这里是监听器的run方法"); //我们的目的是为了监听测试结果的,不是为了禁止掉测试方法执行 //让测试方法可以正常的执行 iHookCallBack.runTestMethod(iTestResult); //监听测试的结果(失败的???) iTestResult--》保存测试的结果 if(iTestResult.getThrowable() != null){ //把截图嵌入到Allure报告中 //OutputType.BYTES -->返回值类型为byte[]字节数组 byte[] screenshot = BaseTest.androidDriver.getScreenshotAs(OutputType.BYTES); saveScreenshot(screenshot); } } //Attachement注解不是由TestNG所提供的,而是由Allure所提供的 @Attachment(value = "Page screenshot", type = "image/png") public byte[] saveScreenshot(byte[] screenShot) { return screenShot; } }
Allure测试报告
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng --> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.13.1</version> </dependency>
编码设置,避免乱码
<properties> <aspectj.version>1.8.10</aspectj.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties>
<listeners>
<!--可以让监听器生效的-->
<listener class-name="com.lemon.listener.TestResultListener"></listener>
</listeners>