在咱们学习Page Object Model以前,咱们先了解一下Page Object Model(如下简称POM).java
用UiAutomator启动UI自动化测试不是一件困难的任务。你只须要查找元素,对元素操做就能够了。来看一个登陆APP的简单的脚本。android
1 package com.gqou.testdemo; 2 3 import android.support.test.InstrumentationRegistry; 4 import android.support.test.runner.AndroidJUnit4; 5 import android.support.test.uiautomator.UiDevice; 6 import android.support.test.uiautomator.UiObject; 7 import android.support.test.uiautomator.UiSelector; 8 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import static junit.framework.TestCase.assertEquals; 13 14 /** 15 * Created by guangqian on 2016/4/22. 16 */ 17 @RunWith(AndroidJUnit4.class) 18 public class SimpleTest { 19 20 @Test 21 public void 测试登陆成功() throws Exception{ 22 // 查找图标并点击启动app 23 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 24 uiDevice.findObject(new UiSelector().text("TestDemo")).clickAndWaitForNewWindow(); 25 // 输入用户名,密码并点击Login 26 UiObject usernameObj = uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/name_txt")); 27 UiObject pwdObj = uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/pwd_txt")); 28 UiObject loginBtn = uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/login_btn")); 29 UiObject welcomeTip = uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/welcome_tip")); 30 31 usernameObj.click(); 32 usernameObj.setText("username"); 33 pwdObj.click(); 34 pwdObj.setText("password"); 35 36 loginBtn.clickAndWaitForNewWindow(); 37 38 // 校验登陆成功信息 39 assertEquals("congratulations", welcomeTip.getText()); 40 } 41 }
1 package com.gqou.testdemo.pages; 2 3 import android.support.test.uiautomator.UiDevice; 4 import android.support.test.uiautomator.UiObject; 5 import android.support.test.uiautomator.UiSelector; 6 7 /** 8 * Created by guangqian on 2016/4/22. 9 */ 10 public class LoginActivity { 11 12 UiDevice uiDevice; 13 UiObject mUserName; 14 UiObject mPasswd; 15 UiObject mLogin; 16 17 public LoginActivity(UiDevice uiDevice){ 18 this.uiDevice = uiDevice; 19 mUserName = this.uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/name_txt")); 20 mPasswd = this.uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/pwd_txt")); 21 mLogin = this.uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/login_btn")); 22 } 23 24 /** 25 * 输入用户名 26 * @param userName 27 * @throws Exception 28 */ 29 private void setmUserName(String userName) throws Exception{ 30 mUserName.click(); 31 mUserName.setText(userName); 32 } 33 34 /** 35 * 输入密码 36 * @param pwd 37 * @throws Exception 38 */ 39 private void setmPasswd(String pwd) throws Exception{ 40 mPasswd.click(); 41 mPasswd.setText(pwd); 42 } 43 44 /** 45 * 点击登陆按钮 46 * @throws Exception 47 */ 48 private void clickLogin()throws Exception{ 49 mLogin.clickAndWaitForNewWindow(); 50 } 51 52 /** 53 * 封装登陆App操做 54 * @param username 55 * @param pwd 56 * @throws Exception 57 */ 58 public void loginApp(String username, String pwd) throws Exception{ 59 this.setmUserName(username); 60 this.setmPasswd(pwd); 61 this.clickLogin(); 62 } 63 }
1 package com.gqou.testdemo.pages; 2 3 import android.support.test.uiautomator.UiDevice; 4 import android.support.test.uiautomator.UiObject; 5 import android.support.test.uiautomator.UiSelector; 6 7 /** 8 * Created by guangqian on 2016/4/22. 9 */ 10 public class WelcomeActivity { 11 UiDevice uiDevice; 12 UiObject welcomeTip; 13 14 public WelcomeActivity(UiDevice uiDevice){ 15 this.uiDevice = uiDevice; 16 welcomeTip = this.uiDevice.findObject(new UiSelector().resourceId("com.gqou.testdemo:id/welcome_tip")); 17 } 18 19 /** 20 * 获取欢迎提示语内容 21 * @return 22 * @throws Exception 23 */ 24 public String getWelcomeTip() throws Exception{ 25 return welcomeTip.getText(); 26 } 27 28 }
1 package com.gqou.testdemo.testcases; 2 3 import android.support.test.InstrumentationRegistry; 4 import android.support.test.runner.AndroidJUnit4; 5 import android.support.test.uiautomator.UiDevice; 6 import android.support.test.uiautomator.UiSelector; 7 8 import com.gqou.testdemo.pages.LoginActivity; 9 import com.gqou.testdemo.pages.WelcomeActivity; 10 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 15 import static junit.framework.TestCase.assertEquals; 16 /** 17 * Created by guangqian on 2016/4/22. 18 */ 19 @RunWith(AndroidJUnit4.class) 20 public class POMForLoginTest { 21 22 public static UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 23 private static LoginActivity objLogin; 24 private static WelcomeActivity objWelcome; 25 26 27 @Before 28 public void setUp() throws Exception { 29 // 打开应用 30 uiDevice.findObject(new UiSelector().text("TestDemo")).clickAndWaitForNewWindow(); 31 // 建立界面实例对象 32 objLogin = new LoginActivity(uiDevice); 33 objWelcome = new WelcomeActivity(uiDevice); 34 35 } 36 37 38 39 @Test 40 public void 测试App登陆成功() throws Exception { 41 // 登陆app 42 objLogin.loginApp("Tom", "123456"); 43 // 获取主界面提示语 44 String tips = objWelcome.getWelcomeTip(); 45 // 判断提示语是否为预期 46 assertEquals("congratulations", tips); 47 } 48 }
Good Luck!设计模式
水平有限,欢迎各位大牛点评,多谢支持!app