├── .gitignore ├── Makefile ├── README.md ├── app ├── android-app-bootstrap.zip └── ios-app-bootstrap.zip ├── autoui.iml ├── checkstyle.xml ├── pom.xml ├── settings.xml └── src ├── main └── java │ └── com │ └── javademo │ └── autoui │ └── App.java └── test └── java └── com └── javademo ├── autoui └── AppTest.java ├── cases ├── BaseTest.java └── SampleTest.java ├── pages ├── BaiDuPage.java ├── HomeListPage.java ├── HomePage.java ├── LoginPage.java ├── PersonalPage.java └── WebviewPage.java ├── pageuis ├── BaiDuPageUI.java ├── HomeListPageUI.java ├── HomePageUI.java ├── LoginPageUI.java ├── PersonalPageUI.java └── WebviewPageUI.java └── utils └── Config.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | /target/ 3 | /screenshot/ 4 | .settings 5 | .classpath 6 | .project 7 | *.sw* 8 | *.un~ 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: test 2 | test: server 3 | mvn -s settings.xml clean install 4 | mvn test 5 | server: 6 | macaca server --verbose & 7 | install: 8 | mvn -s settings.xml clean install -Dmaven.test.skip=true 9 | .PHONY: test 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bootstrap 2 | 3 | --- 4 | 5 | 基于Macaca-Java版的UI自动化实现demo 6 | 7 | ### 文档参考(从入门到精通) 8 | 9 | [https://testerhome.com/junhe](https://testerhome.com/junhe) 10 | 11 | ### 安装Macaca环境 12 | 13 | 参考链接:(Macaca-Java版入门指南)[https://testerhome.com/topics/6431) 14 | 15 | ### 使用步骤 16 | 17 | ### 1. 下载源码 18 | 19 | ```bash 20 | $ git clone https://github.com/macaca-sample/macaca-java-biz-sample.git 21 | ``` 22 | 23 | ### 2. 更新依赖 24 | 25 | ```bash 26 | $ cd macaca-java-biz-sample 27 | $ make install 28 | ``` 29 | 30 | 如果下载依赖过程中报错,可能是由于mvn -s命令没有生效导致的,建议将根目录下settings.xml中的依赖配置到本地Maven目录下的settings.xml中。 31 | 32 | ### 如何修改目标平台 ios/android? 33 | 34 | ```java 35 | //package com.javademo.common; 36 | // Config.java 37 | // 目标平台- ios android 38 | 39 | public static final String PLATFORM = "ios"; 40 | ``` 41 | 注意:执行iOS用例时需要将XCode升级到最新的8.1,执行用例前请先启动目标模拟器。 42 | 43 | ### 3. 启动server 44 | 45 | ```bash 46 | $ macaca server --verbose 47 | ``` 48 | 49 | 注意启动server时不能加代理,因为server在本机启动需要连接localhost,加代理会导致无法建立连接。 50 | 51 | ### 4. 执行测试用例 52 | 53 | 新建cmd窗口(记得新建cmd窗口哦,不要跟macaca server在同一个窗口执行) ,执行mvn test(默认为启动iOS用例) 54 | 55 | ```bash 56 | $ mvn -s settings.xml test 57 | ``` 58 | 59 | 或者选中SampleTest.java,右键执行run as -> JunitTest 60 | 61 | ### ReleaseNotes 62 | 63 | #### v0.0.2 64 | 65 | 将原公共库代码common部分提出来单独建立了代码库,以jar包的形式上传至Jcenter,可通过Maven直接引用。 66 | 67 | 公共库代码也已开源: [//github.com/macaca-sample/macaca-java-biz-framework](//github.com/macaca-sample/macaca-java-biz-framework) 68 | 69 | 欢迎大家引用biz.jar,使用过程中遇到问题,可以提issue,我会尽快解决。 70 | 71 | 72 | 73 | ## Contributors 74 | 75 | |[
Yinxl](https://github.com/Yinxl)
|[
xudafeng](https://github.com/xudafeng)
76 | | :---: | :---: | 77 | 78 | 79 | This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto upated at `Sat Apr 28 2018 19:15:25 GMT+0800`. 80 | 81 | 82 | -------------------------------------------------------------------------------- /app/android-app-bootstrap.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macaca-sample/macaca-java-biz-sample/2de3e7a39655df2444913ce0fe41c3253def578a/app/android-app-bootstrap.zip -------------------------------------------------------------------------------- /app/ios-app-bootstrap.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macaca-sample/macaca-java-biz-sample/2de3e7a39655df2444913ce0fe41c3253def578a/app/ios-app-bootstrap.zip -------------------------------------------------------------------------------- /autoui.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.javademo 6 | autoui 7 | 0.0.2 8 | jar 9 | 10 | autoui 11 | http://maven.apache.org 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-checkstyle-plugin 17 | 2.17 18 | 19 | 20 | validate 21 | validate 22 | 23 | checkstyle.xml 24 | UTF-8 25 | true 26 | true 27 | true 28 | 29 | 30 | check 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | UTF-8 39 | 40 | 41 | 42 | 43 | junit 44 | junit 45 | 4.12 46 | test 47 | 48 | 49 | macaca.java 50 | biz 51 | 1.1.17 52 | jar 53 | 54 | 55 | com.alibaba 56 | fastjson 57 | 1.2.15 58 | 59 | 60 | org.apache.httpcomponents 61 | httpclient 62 | 4.5.2 63 | 64 | 65 | log4j 66 | log4j 67 | 1.2.17 68 | 69 | 70 | org.hamcrest 71 | hamcrest-library 72 | 1.2 73 | test 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | false 14 | 15 | bintray-orgz-macaca-java-biz 16 | bintray 17 | http://dl.bintray.com/orgz/macaca-java-biz 18 | 19 | 20 | 21 | false 22 | 23 | central 24 | bintray 25 | http://jcenter.bintray.com 26 | 27 | 28 | 29 | 30 | 31 | false 32 | 33 | bintray-orgz-macaca-java-biz 34 | bintray-plugins 35 | http://dl.bintray.com/orgz/macaca-java-biz 36 | 37 | 38 | 39 | 40 | false 41 | 42 | central 43 | bintray-plugins 44 | http://jcenter.bintray.com 45 | 46 | 47 | 48 | bintray 49 | 50 | 51 | 52 | bintray 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/main/java/com/javademo/autoui/App.java: -------------------------------------------------------------------------------- 1 | package com.javademo.autoui; 2 | 3 | /** 4 | * Hello world! 5 | */ 6 | public class App { 7 | public static void main(String[] args) { 8 | System.out.println("Hello World!"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/autoui/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.javademo.autoui; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase { 12 | /** 13 | * Create the test case 14 | * 15 | * @param testName name of the test case 16 | */ 17 | public AppTest(String testName) { 18 | super(testName); 19 | } 20 | 21 | /** 22 | * @return the suite of tests being tested 23 | */ 24 | public static Test suite() { 25 | return new TestSuite(AppTest.class); 26 | } 27 | 28 | /** 29 | * Rigourous Test :-) 30 | */ 31 | public void testApp() { 32 | assertTrue(true); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/cases/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.javademo.cases; 2 | 3 | import java.io.File; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | 8 | import macaca.java.biz.BaseErrorType; 9 | import macaca.java.biz.BaseMacacaClient; 10 | import macaca.java.biz.ResultGenerator; 11 | import macaca.java.biz.BaseMacacaClient.PlatformType; 12 | import com.javademo.utils.Config; 13 | import com.alibaba.fastjson.JSONObject; 14 | 15 | public class BaseTest { 16 | 17 | // 屏幕截图的数目,为了实现递增的顺序 18 | private int screenNum = 1; 19 | 20 | BaseMacacaClient driver = new BaseMacacaClient(); 21 | 22 | @Before 23 | public void setUp() throws Exception { 24 | 25 | // 清除日志记录 26 | ResultGenerator.clearOldData(); 27 | //清理截图重新记录 28 | File file = new File(Config.SCREEN_SHOT_PATH); 29 | deleteOldScreen(file); 30 | 31 | // 初始化应用基础信息 32 | JSONObject props = new JSONObject(); 33 | if (Config.PLATFORM.equals("ios")) { 34 | 35 | // 创建ios实例 36 | props.put("app", Config.IOS_APP); 37 | props.put("platformName", Config.IOS_PLATFORM_NAME); 38 | props.put("deviceName", Config.IOS_DEVICE_NAME); 39 | // props.put("udid", Config.IOS_UDID); 40 | driver.setCurPlatform(PlatformType.IOS); 41 | } else { 42 | 43 | //创建安卓实例 44 | props.put("app", Config.ADR_APP); 45 | props.put("platformName", Config.ADR_PLATFORM_NAME); 46 | driver.setCurPlatform(PlatformType.ANDROID); 47 | } 48 | 49 | // 覆盖安装 50 | props.put("reuse", Config.REUSE); 51 | 52 | JSONObject desiredCapabilities = new JSONObject(); 53 | desiredCapabilities.put("desiredCapabilities", props); 54 | driver.initDriver(desiredCapabilities); 55 | 56 | } 57 | 58 | 59 | @After 60 | public void tearDown() throws Exception { 61 | 62 | try { 63 | driver.quit(); 64 | } catch (Exception e) { 65 | // TODO: handle exception 66 | ResultGenerator.fail("quit fail", "", BaseErrorType.FUNCTION_FAILED); 67 | } 68 | 69 | } 70 | 71 | /** 72 | * 保存当前屏幕截图-生成的截图会按照截图的先后顺序生成有序的名称 73 | * 74 | * @param fileName 图片名称,默认为.png格式,图片默认保存在screenShot目录下 75 | */ 76 | public void saveScreen(String fileName) { 77 | try { 78 | // 判断是否存在对应目录,不存在的话则创建 79 | File file = new File(Config.SCREEN_SHOT_PATH); 80 | if (!file.exists() || !file.isDirectory()) { 81 | // 没有目录 创建截屏目录 82 | System.out.println("没有screenshot目录,创建目录"); 83 | boolean isMkdirSucc = file.mkdir(); 84 | if (isMkdirSucc) { 85 | System.out.println("创建screenshot目录成功"); 86 | } else { 87 | System.out.println("创建screenshot目录失败"); 88 | } 89 | } else { 90 | System.out.println("存在screenshot目录"); 91 | } 92 | 93 | driver.saveScreenshot( 94 | Config.SCREEN_SHOT_PATH + File.separator + screenNum + "_" + fileName + ".png"); 95 | screenNum++; 96 | } catch (Exception e) { 97 | // TODO: handle exception 98 | ResultGenerator.fail("截屏异常", "", BaseErrorType.FUNCTION_FAILED); 99 | } 100 | } 101 | 102 | //删除screenshot目录下旧的截图 103 | public void deleteOldScreen(File oldScreen) { 104 | if (oldScreen.exists() && oldScreen.isDirectory()) { 105 | File[] files = oldScreen.listFiles(); 106 | for (File file : files) { 107 | deleteOldScreen(file); 108 | } 109 | } else { 110 | oldScreen.delete(); 111 | } 112 | 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/cases/SampleTest.java: -------------------------------------------------------------------------------- 1 | package com.javademo.cases; 2 | 3 | import com.alibaba.fastjson.JSONArray; 4 | import com.alibaba.fastjson.JSONObject; 5 | import macaca.client.common.GetElementWay; 6 | import macaca.java.biz.CommonUIBean; 7 | import org.junit.Test; 8 | 9 | import macaca.java.biz.ResultGenerator; 10 | import com.javademo.pages.BaiDuPage; 11 | import com.javademo.pages.HomeListPage; 12 | import com.javademo.pages.HomePage; 13 | import com.javademo.pages.LoginPage; 14 | import com.javademo.pages.PersonalPage; 15 | import com.javademo.pages.WebviewPage; 16 | import com.javademo.pageuis.BaiDuPageUI; 17 | import com.javademo.pageuis.HomeListPageUI; 18 | import com.javademo.pageuis.HomePageUI; 19 | import com.javademo.pageuis.LoginPageUI; 20 | import com.javademo.pageuis.PersonalPageUI; 21 | import com.javademo.pageuis.WebviewPageUI; 22 | 23 | public class SampleTest extends BaseTest { 24 | 25 | @Test 26 | public void test() throws Exception { 27 | 28 | // 处理登录 29 | LoginPage loginPage = new LoginPage("登录页"); 30 | loginPage.setDriver(driver); 31 | if (loginPage.hasPageShown(LoginPageUI.LOGIN_BTN)) { 32 | saveScreen(loginPage.pageDesc); 33 | ResultGenerator.loadPageSucc(loginPage); 34 | loginPage.login("test", "123"); 35 | } else { 36 | ResultGenerator.loadPageFail(loginPage); 37 | 38 | } 39 | 40 | // 测试首页 41 | HomePage homePage = new HomePage("首页"); 42 | homePage.setDriver(driver); 43 | if (homePage.hasPageShown(HomePageUI.HOME_TAB)) { 44 | saveScreen(homePage.pageDesc); 45 | ResultGenerator.loadPageSucc(homePage); 46 | // 进入list 47 | homePage.goToList(); 48 | } else { 49 | // 首页没有加载成功,后面的用例都不用执行了,return 50 | ResultGenerator.loadPageFail(homePage); 51 | return; 52 | } 53 | 54 | // 自动化首页List页 55 | testHomePageList(); 56 | driver.sleep(1000); 57 | 58 | // 测试webView Tab 59 | homePage.tabWebView(); 60 | testWebView(); 61 | driver.sleep(1000); 62 | 63 | // 测试浏览器 64 | homePage.tabBaiDu(); 65 | testBaiDu(); 66 | driver.sleep(1000); 67 | 68 | // 测试Personal 69 | homePage.tabPersonal(); 70 | testPersonalPage(); 71 | 72 | } 73 | 74 | public void testHomePageList() throws Exception { 75 | // 首页List页 76 | HomeListPage homeListPage = new HomeListPage("首页列表页"); 77 | homeListPage.setDriver(driver); 78 | if (homeListPage.hasPageShown(HomeListPageUI.LIST_VIEW)) { 79 | 80 | saveScreen(homeListPage.pageDesc); 81 | ResultGenerator.loadPageSucc(homeListPage); 82 | 83 | // 滑动 84 | homeListPage.scroll(); 85 | 86 | // 回到首页 87 | driver.customBack(); 88 | } 89 | 90 | } 91 | 92 | 93 | public void testWebView() throws Exception { 94 | WebviewPage webviewPage = new WebviewPage("webviewPage"); 95 | webviewPage.setDriver(driver); 96 | if (webviewPage.hasPageShown(WebviewPageUI.WEB_VIEW_TITLE)) { 97 | saveScreen(webviewPage.pageDesc); 98 | ResultGenerator.loadPageSucc(webviewPage); 99 | 100 | // Push view 101 | webviewPage.pushView(); 102 | driver.sleep(1000); 103 | driver.customBack(); 104 | 105 | // 修改标题 106 | webviewPage.setTitle(); 107 | saveScreen(webviewPage.pageDesc + "_newTitle"); 108 | } else { 109 | ResultGenerator.loadPageFail(webviewPage); 110 | } 111 | 112 | } 113 | 114 | 115 | public void testBaiDu() throws Exception { 116 | BaiDuPage baiDuPage = new BaiDuPage("百度"); 117 | baiDuPage.setDriver(driver); 118 | 119 | driver.sleep(3000); 120 | // 切换上下文到当前H5 window 121 | driver.switchFromNativeToWebView(); 122 | 123 | if (baiDuPage.hasPageShown(BaiDuPageUI.SEARCH_BTN)) { 124 | saveScreen(baiDuPage.pageDesc); 125 | ResultGenerator.loadPageSucc(baiDuPage); 126 | 127 | // 遍历所有href属性的跳转链接 128 | iterateBaiDuPage(); 129 | driver.sleep(5000); 130 | 131 | 132 | // 常规的H5操作 133 | // 顶部登录按钮 134 | driver.onclickBean(BaiDuPageUI.USER_LOGIN_BEAN); 135 | driver.sleep(2000); 136 | saveScreen("userLoginBtn"); 137 | 138 | // 进入登录页之后,需要更新当前上下文再对登录页做处理 139 | driver.updateTopContext(); 140 | 141 | // 为了方便示例展示,此处将控件的定义放在外层,控件的value值通过在inspect页面对应控件上右键-Copy-Copy Selector得到 142 | CommonUIBean loginRightNowBean = new CommonUIBean(GetElementWay.CSS, "#page-bd > section.user-profile > div.user-login.clearfix > a", "立即登录"); 143 | driver.onclickBean(loginRightNowBean); 144 | driver.sleep(2000); 145 | saveScreen("loginPage"); 146 | 147 | driver.execute("history.back();"); 148 | 149 | driver.execute("history.back();"); 150 | 151 | 152 | } else { 153 | ResultGenerator.loadPageFail(baiDuPage); 154 | } 155 | 156 | driver.switchFromeWebviewToNative(); 157 | 158 | } 159 | 160 | 161 | public void testPersonalPage() throws Exception { 162 | PersonalPage personalPage = new PersonalPage("个人主页"); 163 | personalPage.setDriver(driver); 164 | if (personalPage.hasPageShown(PersonalPageUI.LOG_OUT_BTN)) { 165 | saveScreen(personalPage.pageDesc); 166 | ResultGenerator.loadPageSucc(personalPage); 167 | 168 | // logout 169 | personalPage.logout(); 170 | 171 | } else { 172 | ResultGenerator.loadPageFail(personalPage); 173 | } 174 | } 175 | 176 | public void iterateBaiDuPage() throws Exception { 177 | JSONObject schemaJson = driver.execute("var arr=[];function getSchemas(){$.each($('[href]'),function(index,item){arr.push(item.getAttribute('href'))}); return arr;};return getSchemas();"); 178 | JSONArray schemas = schemaJson.getJSONArray("value"); 179 | System.out.println("schemas===========" + schemas); 180 | 181 | if (schemas != null && schemas.size() > 0) { 182 | // 循环进入各个二级跳转链接 183 | // 此处为避免href过多导致执行时间过长,限定了循环次数,实际中可以直接指定循环次数为数组长度 184 | // for (int i = 0; i < schemas.size(); i++) { 185 | for (int i = 0; i < 5; i++) { 186 | 187 | String tmpHref = (String) schemas.get(i); 188 | String jsCode = "location.assign(\"" + tmpHref + "\")"; 189 | driver.execute(jsCode); 190 | driver.sleep(3000); 191 | saveScreen(tmpHref); 192 | 193 | driver.execute("history.back();"); 194 | 195 | } 196 | 197 | } 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/BaiDuPage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import macaca.java.biz.BasePage; 4 | import com.javademo.pageuis.BaiDuPageUI; 5 | 6 | public class BaiDuPage extends BasePage { 7 | 8 | public BaiDuPage(String pageDesc) { 9 | super(pageDesc); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | /** 14 | * 按照关键字搜索 15 | * 16 | * @param keywords 关键字 17 | */ 18 | public void search(String keywords) { 19 | 20 | driver.inputBean(BaiDuPageUI.SEARCH_FIELD, keywords); 21 | driver.onclickBean(BaiDuPageUI.SEARCH_BTN); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/HomeListPage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.javademo.pageuis.HomeListPageUI; 5 | 6 | import macaca.java.biz.BasePage; 7 | 8 | public class HomeListPage extends BasePage { 9 | 10 | public HomeListPage(String pageDesc) { 11 | super(pageDesc); 12 | // TODO Auto-generated constructor stub 13 | } 14 | 15 | /** 16 | * 上下滑动 17 | * 18 | * @throws Exception 19 | */ 20 | public void scroll() throws Exception { 21 | 22 | JSONObject windowSize = driver.getWindowSize(); 23 | int windowWidth = windowSize.getIntValue("width"); 24 | int windowHeight = windowSize.getIntValue("height"); 25 | 26 | int centerX = (int) windowWidth / 2; 27 | // driver.swipe(centerX,(int)windowHeight-100, centerX, 300, 500); 28 | driver.drag(centerX, (int) windowHeight - 100, centerX, 300, 0.05); 29 | driver.sleep(1000); 30 | // driver.swipe(centerX, 300, centerX, (int)windowHeight-100, 500); 31 | driver.drag(centerX, 300, centerX, (int) windowHeight - 100, 0.05); 32 | } 33 | 34 | /** 35 | * 点击指定的cell 36 | * 37 | * @param index 要点击的cell的index,限可视区域 38 | */ 39 | public void onclickOneCell(int index) throws Exception { 40 | driver.onclickBeanAtIndex(HomeListPageUI.CELL, index); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/HomePage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import macaca.java.biz.BasePage; 4 | import com.javademo.pageuis.HomePageUI; 5 | 6 | public class HomePage extends BasePage { 7 | 8 | public HomePage(String pageDesc) { 9 | super(pageDesc); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | /** 14 | * 点击home tab 15 | */ 16 | public void tabHome() { 17 | driver.onclickBean(HomePageUI.HOME_TAB); 18 | } 19 | 20 | /** 21 | * 点击webView tab 22 | */ 23 | public void tabWebView() { 24 | driver.onclickBean(HomePageUI.WEBVIEW_TAB); 25 | } 26 | 27 | /** 28 | * 点击百度 29 | */ 30 | public void tabBaiDu() { 31 | driver.onclickBean(HomePageUI.BAIDU_TAB); 32 | } 33 | 34 | /** 35 | * 点击Personal tab 36 | */ 37 | public void tabPersonal() { 38 | driver.onclickBean(HomePageUI.PERSONAL_TAB); 39 | } 40 | 41 | /** 42 | * 进入List页 43 | */ 44 | public void goToList() { 45 | driver.onclickBean(HomePageUI.LIST_BTN); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/LoginPage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import macaca.java.biz.BasePage; 4 | import com.javademo.pageuis.LoginPageUI; 5 | 6 | public class LoginPage extends BasePage { 7 | 8 | public LoginPage(String pageDesc) { 9 | super(pageDesc); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | // 登录操作 14 | public void login(String username, String password) { 15 | driver.inputBean(LoginPageUI.USER_NAME, username); 16 | driver.inputBean(LoginPageUI.PASSWORD, password); 17 | driver.onclickBean(LoginPageUI.KEY_BOARD); 18 | driver.onclickBean(LoginPageUI.LOGIN_BTN); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/PersonalPage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import macaca.java.biz.BasePage; 4 | import com.javademo.pageuis.PersonalPageUI; 5 | 6 | public class PersonalPage extends BasePage { 7 | 8 | public PersonalPage(String pageDesc) { 9 | super(pageDesc); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | /** 14 | * 执行登出操作 15 | * 16 | * @throws Exception 17 | */ 18 | public void logout() throws Exception { 19 | driver.onclickBean(PersonalPageUI.LOG_OUT_BTN); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pages/WebviewPage.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pages; 2 | 3 | import macaca.java.biz.BasePage; 4 | import com.javademo.pageuis.WebviewPageUI; 5 | 6 | public class WebviewPage extends BasePage { 7 | 8 | public WebviewPage(String pageDesc) { 9 | super(pageDesc); 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | /** 14 | * 点击pushView 15 | */ 16 | public void pushView() { 17 | driver.onclickBean(WebviewPageUI.PUSH_VIEW); 18 | } 19 | 20 | /** 21 | * 点击popView 22 | */ 23 | public void popView() { 24 | driver.onclickBean(WebviewPageUI.POP_VIEW); 25 | } 26 | 27 | /** 28 | * 修改标题 29 | */ 30 | public void setTitle() { 31 | driver.onclickBean(WebviewPageUI.SET_TITLE); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/BaiDuPageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class BaiDuPageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean SEARCH_FIELD = new CommonUIBean(GetElementWay.CLASS_NAME, "android.widget.EditText", GetElementWay.CLASS_NAME, "TextField", "搜索框"); 11 | public static final CommonUIBean SEARCH_BTN = new CommonUIBean(GetElementWay.CSS, "#index-bn", GetElementWay.NAME, "百度一下", "搜索按钮"); 12 | public static final CommonUIBean USER_LOGIN_BEAN = new CommonUIBean(GetElementWay.CSS, "#login", "右上角登录按钮"); 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/HomeListPageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class HomeListPageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean LIST_VIEW = new CommonUIBean(GetElementWay.ID, "com.github.android_app_bootstrap:id/listview", GetElementWay.CLASS_NAME, "XCUIElementTypeTable", "list view"); 11 | public static final CommonUIBean CELL = new CommonUIBean(GetElementWay.CLASS_NAME, "android.widget.TextView", GetElementWay.CLASS_NAME, "Cell", "CELL"); 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/HomePageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class HomePageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean HOME_TAB = new CommonUIBean(GetElementWay.NAME, "HOME", "HOME TAB"); 11 | public static final CommonUIBean WEBVIEW_TAB = new CommonUIBean(GetElementWay.NAME, "Webview", "WEBVIEW TAB"); 12 | public static final CommonUIBean BAIDU_TAB = new CommonUIBean(GetElementWay.NAME, "Baidu", "BAIDU TAB"); 13 | public static final CommonUIBean PERSONAL_TAB = new CommonUIBean(GetElementWay.NAME, "PERSONAL", "PERSONAL TAB"); 14 | public static final CommonUIBean LIST_BTN = new CommonUIBean(GetElementWay.NAME, "list", "首页list按钮"); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/LoginPageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class LoginPageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean USER_NAME = new CommonUIBean(GetElementWay.ID, "com.github.android_app_bootstrap:id/mobileNoEditText", GetElementWay.NAME, "please input username", "登录用户名输入框"); 11 | public static final CommonUIBean PASSWORD = new CommonUIBean(GetElementWay.ID, "com.github.android_app_bootstrap:id/codeEditText", GetElementWay.NAME, "please input password", "登录密码输入框"); 12 | public static final CommonUIBean LOGIN_BTN = new CommonUIBean(GetElementWay.ID, "com.github.android_app_bootstrap:id/login_button", GetElementWay.NAME, "Login", "登录按钮"); 13 | public static final CommonUIBean KEY_BOARD = new CommonUIBean(GetElementWay.NAME, "Done", "键盘完成按钮"); 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/PersonalPageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class PersonalPageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean LOG_OUT_BTN = new CommonUIBean(GetElementWay.NAME, "Logout", "logout按钮"); 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/pageuis/WebviewPageUI.java: -------------------------------------------------------------------------------- 1 | package com.javademo.pageuis; 2 | 3 | import macaca.java.biz.BasePageUI; 4 | import macaca.java.biz.CommonUIBean; 5 | 6 | import macaca.client.common.GetElementWay; 7 | 8 | public class WebviewPageUI extends BasePageUI { 9 | 10 | public static final CommonUIBean WEB_VIEW_TITLE = new CommonUIBean(GetElementWay.ID, "com.github.android_app_bootstrap:id/title_text", GetElementWay.NAME, "pushView", "webViewTitle"); 11 | public static final CommonUIBean PUSH_VIEW = new CommonUIBean(GetElementWay.ID, "pushView", GetElementWay.NAME, "pushView", "pushview"); 12 | public static final CommonUIBean POP_VIEW = new CommonUIBean(GetElementWay.ID, "popView", GetElementWay.NAME, "popView", "popView"); 13 | public static final CommonUIBean SET_TITLE = new CommonUIBean(GetElementWay.ID, "setTitle", GetElementWay.NAME, "setTitle", "setTitle"); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/javademo/utils/Config.java: -------------------------------------------------------------------------------- 1 | package com.javademo.utils; 2 | 3 | import java.io.File; 4 | 5 | 6 | public class Config { 7 | 8 | // 根目录 9 | public static final String ROOT_PATH = System.getProperty("user.dir"); 10 | //截图保存目录 11 | public static final String SCREEN_SHOT_PATH = ROOT_PATH + File.separator + "screenshot"; 12 | //结果日志保存文件 13 | public static final String RESULT_LOG_PATH = ROOT_PATH + File.separator + "result.log"; 14 | 15 | // 用户名 16 | public static final String USER_NAME = "username"; 17 | // 密码 18 | public static final String PASSWORD = "password"; 19 | // 目标平台- ios android 20 | public static final String PLATFORM = "ios"; 21 | // 是否覆盖安装 3-覆盖安装 22 | public static final int REUSE = 1; 23 | 24 | // ios平台相关信息 各参数含义参考 https://macacajs.github.io/macaca/desired-caps.html 25 | public static final String IOS_PLATFORM_NAME = "ios"; 26 | public static final String IOS_DEVICE_NAME = "iPhone 6s"; 27 | public static final String IOS_APP = "https://npmcdn.com/ios-app-bootstrap@latest/build/ios-app-bootstrap.zip"; 28 | // public static final String IOS_UDID = "3458343D-AFB1-46E1-BE33-99E04B2DE07"; 29 | 30 | // 安卓平台相关信息 31 | public static final String ADR_PLATFORM_NAME = "Android"; 32 | public static final String ADR_APP = ROOT_PATH + File.separator + "app/android-app-bootstrap.zip"; 33 | // 多台设备时,如果指定某一台设备可以在这里指定udid 34 | public static final String ADR_UDID = ""; 35 | 36 | } 37 | --------------------------------------------------------------------------------