AndroidPageObjectTest_Simple.java

【AndroidPageObjectTest_Simple.java】男儿欲遂平生志,五经勤向窗前读。这篇文章主要讲述AndroidPageObjectTest_Simple.java相关的知识,希望能为你提供帮助。
以下代码使用ApiDemos-debug.apk进行测试//这个脚本用于演示PageFactory的功能:使用注解定位元素。

1 package com.saucelabs.appium; 2 3 import io.appium.java_client.MobileElement; 4 import io.appium.java_client.android.AndroidDriver; 5 import io.appium.java_client.pagefactory.AppiumFieldDecorator; 6 import io.appium.java_client.remote.MobileCapabilityType; 7 8 import java.io.File; 9 import java.net.URL; 10 import java.util.NoSuchElementException; 11 import java.util.concurrent.TimeUnit; 12 13 import org.junit.After; 14 import org.junit.Assert; 15 import org.junit.Before; 16 import org.junit.Test; 17 import org.openqa.selenium.WebDriver; 18 import org.openqa.selenium.remote.DesiredCapabilities; 19 import org.openqa.selenium.support.PageFactory; 20 21 import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenSimple; //页面类 22 23 public class AndroidPageObjectTest_Simple { 24 25private WebDriver driver; 26private ApiDemosListViewScreenSimple apiDemosPageObject; 27 28@Before 29public void setUp() throws Exception { 30//File classpathRoot = new File(System.getProperty("user.dir")); 31File appDir = new File("E:/package"); 32File app = new File(appDir, "ApiDemos-debug.apk"); 33DesiredCapabilities capabilities = new DesiredCapabilities(); 34capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); 35capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); 36driver = new AndroidDriver< MobileElement> (new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 37 38apiDemosPageObject = new ApiDemosListViewScreenSimple(); 39//This time out is set because test can be run on slow Android SDK emulator 40PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), 41apiDemosPageObject); 42} 43 44@After 45public void tearDown() throws Exception { 46driver.quit(); 47} 48 49@Test 50public void findByElementsTest() { 51Assert.assertNotEquals(0, apiDemosPageObject.textVieWs.size()); 52} 53 54@Test 55public void findByElementTest() { 56Assert.assertNotEquals(null, apiDemosPageObject.textView.getAttribute("text")); 57} 58 59 60@Test 61public void androidFindByElementsTest(){ 62Assert.assertNotEquals(0, apiDemosPageObject.androidTextViews.size()); 63} 64 65@Test 66public void androidFindByElementTest(){ 67Assert.assertNotEquals(null, apiDemosPageObject.androidTextView.getAttribute("text")); 68} 69 70@Test 71public void checkThatElementsWereNotFoundByiosUIAutomator(){ 72Assert.assertEquals(0, apiDemosPageObject.iosTextViews.size()); 73} 74 75@Test 76public void checkThatElementWasNotFoundByIOSUIAutomator(){ 77String nsee = null; 78try{ 79apiDemosPageObject.iosTextView.getAttribute("text"); 80} 81catch (Exception e){ 82nsee =e.getClass().getName(); 83} 84Assert.assertEquals(nsee,"org.openqa.selenium.NoSuchElementException"); 85} 86 87@Test 88public void androidOrIOSFindByElementsTest(){ 89Assert.assertNotEquals(0, apiDemosPageObject.androidOriOsTextViews.size()); 90} 91 92@Test 93public void androidOrIOSFindByElementTest(){ 94Assert.assertNotEquals(null, apiDemosPageObject.androidOriOsTextView.getAttribute("text")); 95} 96 97@Test 98public void androidFindByUIAutomatorElementsTest(){ 99Assert.assertNotEquals(0, apiDemosPageObject.androidUIAutomatorViews.size()); 100} 101 102@Test 103public void androidFindByUIAutomatorElementTest(){ 104Assert.assertNotEquals(null, apiDemosPageObject.androidUIAutomatorView.getAttribute("text")); 105} 106 107@Test 108public void areMobileElementsTest(){ 109Assert.assertNotEquals(0, apiDemosPageObject.mobileElementViews.size()); 110} 111 112@Test 113public void isMobileElementTest(){ 114Assert.assertNotEquals(null, apiDemosPageObject.mobileElementView.getAttribute("text")); 115} 116 117@Test 118public void areMobileElements_FindByTest(){ 119Assert.assertNotEquals(0, apiDemosPageObject.mobiletextVieWs.size()); 120} 121 122@Test 123public void isMobileElement_FindByTest(){ 124Assert.assertNotEquals(null, apiDemosPageObject.mobiletextVieW.getAttribute("text")); 125} 126 127@Test 128public void areRemoteElementsTest(){ 129Assert.assertNotEquals(0, apiDemosPageObject.remoteElementViews.size()); 130} 131 132@Test 133public void isRemoteElementTest(){ 134Assert.assertNotEquals(null, apiDemosPageObject.remotetextVieW.getAttribute("text")); 135} 136 }

 
页面类的代码:
1 package com.saucelabs.appium.page_object.android; 2 3 import io.appium.java_client.MobileElement; 4 import io.appium.java_client.pagefactory.AndroidFindBy; 5 import io.appium.java_client.pagefactory.iOSFindBy; 6 7 import java.util.List; 8 9 import org.openqa.selenium.WebElement; 10 import org.openqa.selenium.remote.RemoteWebElement; 11 import org.openqa.selenium.support.FindBy; 12 13 /** 14* 15* Here is the common sample shows how to use 16* {@link FindBy}, {@link AndroidFindBy} and {@link iOSFindBy} 17* annotations. 18* 19* Also it demonstrates how to declare screen elements using Appium 20* page objects facilities. 21* 22* About Page Object design pattern read here: 23* https://code.google.com/p/selenium/wiki/PageObjects 24* 25*/ 26 public class ApiDemosListViewScreenSimple { 27/** 28* Page Object best practice is to describe interactions with target 29* elements by methods. This methods describe business logic of the page/screen. 30* Here lazy instantiated elements are public. 31* It was done so just for obviousness 32*/ 33 34 35//Common Selenium @FindBy annotations are effective 36//against browser apps and web views. They can be used against native 37//content. But it is useful to know that By.css, By.link, By.partialLinkText 38//are invalid at this case. 39@FindBy(className = "android.widget.TextView") 40public List< WebElement> textVieWs; 41 42//@AndroidFindBy annotation is designed to be used for Android native content 43//description. 44@AndroidFindBy(className = "android.widget.TextView") 45public List< WebElement> androidTextViews; 46 47@iOSFindBy(uiAutomator = ".elements()[0]") 48public List< WebElement> iosTextViews; 49 50//if it is necessary to use the same Page Object 51//in the browser and cross platform mobile app testing 52//then it is possible to combine different annotations 53@FindBy(css = "someBrowserCss") //this locator is used when here is browser (desktop or mobile) 54@iOSFindBy(uiAutomator = ".elements()[0]") //this locator is used when here is iOS native content 55@AndroidFindBy(className = "android.widget.TextView") //this locator is used when here is Android 56//native content 57public List< WebElement> androidOriOsTextViews; 58 59@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 60public List< WebElement> androidUIAutomatorViews; 61 62@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 63public List< MobileElement> mobileElementViews; //Also with Appium page object tools it is 64//possible to declare RemoteWebElement or any MobileElement subclass 65 66@FindBy(className = "android.widget.TextView") 67public List< MobileElement> mobiletextVieWs; 68 69@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 70public List< RemoteWebElement> remoteElementViews; 71 72@FindBy(id = "android:id/text1") 73public WebElement textView; 74 75@AndroidFindBy(className = "android.widget.TextView") 76public WebElement androidTextView; 77 78@iOSFindBy(uiAutomator = ".elements()[0]") 79public WebElement iosTextView; 80 81@AndroidFindBy(className = "android.widget.TextView") 82@iOSFindBy(uiAutomator = ".elements()[0]") 83public WebElement androidOriOsTextView; 84 85@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 86public WebElement androidUIAutomatorView; 87 88@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 89public MobileElement mobileElementView; 90 91@FindBy(className = "android.widget.TextView") 92public MobileElement mobiletextVieW; 93 94@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")") 95public RemoteWebElement remotetextVieW; 96 97 }

 

    推荐阅读