一个基于 Appium 1.8.1、TestNG,Page Object 模式开发的 UI 自动化测试框架
https://github.com/lgxqf/AppUIAutomation
Test case 层的代码高度利用,只需要考虑业务逻辑,无需关心系统平台及如何查找元素
以下代码在iOS和Android上均可运行
//打开我的朋友圈
public void showMyMoment(){
//打开微信主页面,点击"我"
WeiXinMainPage.verify()
.clickMeButton();
//校验"我"页面,打开"朋友圈"
WeiXinMePage.verify()
.clickMoment();
//校验"朋友圈页面",下划一段距离,然后打开带图片的朋友圈
WeiXinMomentPage.verify()
.scroll()
.clickMyMoment();
Driver.sleep(10);
}
//朋友圈的Page类
public class WeiXinMomentPage extends BasePage {
//能过静态方法返回页面实例
public static WeiXinMomentPage verify(){
if( !Util.isAndroid() ) {
//默认情况下写的Page类是Android的UI
//若Android与iOS UI上有差异,需继承Android的Page类再写个iOS Page
return new WeiXinMomentPageiOS();
}
return new WeiXinMomentPage();
}
//不允许调用构造函数
protected WeiXinMomentPage(){
Driver.findElementByText(getRes("MOMENT_PAGE_ME_TEXT"));
}
//所有成员函数只返回this或void,确保每个Page类的独立性,不依赖于任何其它Page类
public WeiXinMomentPage scroll(){
Driver.scrollUp();
return this;
}
public WeiXinMomentPage clickMyMoment(){
MobileElement elem = Driver.findElemByIdWithoutException (getRes("MY_POST_PAGE_MOMENT_PIC_ID"));
if(elem == null){
elem = Driver.findElementById(getRes("MY_POST_PAGE_MOMENT_ARTICLE_ID"));
}
elem.click();
return this;
}
}
AndroidRES.yml
MAIN_PAGE_WEIXIN_TEXT: '微信'
MAIN_PAGE_CONTACT_TEXT: '通讯录'
MAIN_PAGE_DISCOVER_TEXT: '发现'
MAIN_PAGE_ME_TEXT: '我'
ME_PAGE_MY_POST_TEXT: '相册'
MOMENT_PAGE_ME_TEXT: '我的相册'
MY_POST_PAGE_MOMENT_PIC_ID: 'com.tencent.mm:id/dep'
MY_POST_PAGE_MOMENT_ARTICLE_ID: 'com.tencent.mm:id/yk'
IOSRES.yml
MAIN_PAGE_WEIXIN_TEXT: '微信'
MAIN_PAGE_CONTACT_TEXT: '通讯录'
MAIN_PAGE_DISCOVER_TEXT: '发现'
MAIN_PAGE_ME_TEXT: '我'
ME_PAGE_MY_POST_TEXT: '相册'
MOMENT_PAGE_ME_TEXT: '我'
MY_POST_PAGE_MOMENT_PIC_ID: 'visible == true AND type == "XCUIElementTypeStaticText" AND name CONTAINS "月"'
在任务的xml中有四个值需要配置
1. port : Appium 端口
2. udid : 设备ID
3. wdaPort : iOS设备运行的时的WDA port
4. class : 待运行的测试类
测试执行时输入的xml样例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite">
<listeners>
<listener class-name="framework.TestListener" />
</listeners>
<test name="Performance">
<parameter name = "port" value = "4723"/>
<parameter name = "udid" value = "SJE0217B29005225"/>
<parameter name = "wdaPort" value = "8001"/>
<classes>
<class name="testcase.weclass.WCPerformanceTestCPU"/>
<class name="testcase.weclass.WCPerformanceTestBattery"/>
</classes>
</test>
</suite>