AndroidPageObjectTest_Chained.java

以下代码使用ApiDemos-debug.apk进行测试

//这个脚本用于演示PageFactory的功能:链式注解@AndroidFindBys、@IOSFindBys。具体用法参考页面类的代码。

 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.concurrent.TimeUnit;
11 
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.openqa.selenium.WebDriver;
17 import org.openqa.selenium.remote.DesiredCapabilities;
18 import org.openqa.selenium.support.PageFactory;
19 
20 import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenByAllPossible;
21 import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenChaided;
22 import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenSimple;
23 import com.saucelabs.appium.page_object.ios.TestAppScreenSimple;
24 
25 /**
26  * Please read about Page Object design pattern here:
27  *  https://code.google.com/p/selenium/wiki/PageObjects
28  *  
29  *  Please look at:
30  *  {@link ApiDemosListViewScreenSimple}
31  *  {@link ApiDemosListViewScreenChaided}
32  *  {@link ApiDemosListViewScreenByAllPossible}
33  *  {@link TestAppScreenSimple}
34  *
35  */
36 public class AndroidPageObjectTest_Chained {
37 
38     private WebDriver driver;
39     private ApiDemosListViewScreenChaided apiDemosPageObject;
40     
41     @Before
42     public void setUp() throws Exception {
43         //File classpathRoot = new File(System.getProperty("user.dir"));
44         File appDir = new File("E:/package");
45         File app = new File(appDir, "ApiDemos-debug.apk");
46         DesiredCapabilities capabilities = new DesiredCapabilities();
47         capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
48         capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
49         driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
50         
51         apiDemosPageObject = new ApiDemosListViewScreenChaided();
52         PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), 
53                 apiDemosPageObject);
54     }
55     
56     @After
57     public void tearDown() throws Exception {
58         driver.quit();
59     }
60     
61     /**
62      * Page Object best practice is to describe interactions with target 
63      * elements by methods. These methods describe business logic of the page/screen.
64      * Here test interacts with lazy instantiated elements directly.
65      * It was done so just for obviousness
66      */
67     
68     @Test
69     public void androidChainSearchElementsTest(){
70         Assert.assertNotEquals(0, apiDemosPageObject.chainElementViews.size());
71     }
72 
73     @Test
74     public void androidChainSearchElementTest(){
75         Assert.assertNotEquals(null, apiDemosPageObject.chainElementView.getAttribute("text"));
76     }
77 
78     @Test
79     public void androidOrIOSFindByElementsTest_ChainSearches(){
80         Assert.assertNotEquals(0, apiDemosPageObject.chainAndroidOrIOSUIAutomatorViews.size());
81     }
82 
83     @Test
84     public void androidOrIOSFindByElementTest_ChainSearches(){
85         Assert.assertNotEquals(null, apiDemosPageObject.chainAndroidOrIOSUIAutomatorView.getAttribute("text"));
86     }    
87     
88     @Test
89     public void isAndroidElementTest(){
90         Assert.assertNotEquals(null, apiDemosPageObject.androidElementView.getAttribute("text"));
91     }    
92     
93     @Test
94     public void areAndroidElementsTest(){
95         Assert.assertNotEquals(0, apiDemosPageObject.androidElementViews.size());
96     }    
97 }

页面类的代码如下:

 1 package com.saucelabs.appium.page_object.android;
 2 
 3 import io.appium.java_client.android.AndroidElement;
 4 
 5 import java.util.List;
 6 
 7 import org.openqa.selenium.WebElement;
 8 
 9 import io.appium.java_client.pagefactory.AndroidFindBy;
10 import io.appium.java_client.pagefactory.AndroidFindBys;
11 import io.appium.java_client.pagefactory.iOSFindBy;
12 import io.appium.java_client.pagefactory.iOSFindBys;
13 
14 /**
15  * 
16  * Here is the common sample shows how to use
17  * {@link AndroidFindBys} annotation to describe the chain of the 
18  * searching for the target element of a native Android app content.
19  * 
20  * It demonstrates how to declare screen elements using Appium
21  * page objects facilities.
22  * 
23  * About Page Object design pattern read here:
24  * https://code.google.com/p/selenium/wiki/PageObjects
25  *
26  */
27 public class ApiDemosListViewScreenChaided {
28     
29     /**
30      * Page Object best practice is to describe interactions with target 
31      * elements by methods. This methods describe business logic of the page/screen.
32      * Here lazy instantiated elements are public.
33      * It was done so just for obviousness
34      */
35 
36     @AndroidFindBys({//用多个定位方法描述一组元素。
37         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"), //the searching
38         //starts here
39         @AndroidFindBy(className = "android.widget.TextView") //this element is nested
40         //and so on
41         })
42     public List<WebElement> chainElementViews;
43 
44     @AndroidFindBys({
45         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/content")"),
46         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"),
47         @AndroidFindBy(id = "android:id/text1")
48         })
49     @iOSFindBys({@iOSFindBy(uiAutomator = ".elements()[0]"),
50         @iOSFindBy(xpath = "//someElement")})
51     public List<WebElement> chainAndroidOrIOSUIAutomatorViews;
52 
53 
54     @AndroidFindBys({
55         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"),
56         @AndroidFindBy(className = "android.widget.TextView")
57         })
58     public WebElement chainElementView;
59 
60 
61     @AndroidFindBys({
62         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/content")"),
63         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"),
64         @AndroidFindBy(id = "android:id/text1")
65         })
66     @iOSFindBys({@iOSFindBy(uiAutomator = ".elements()[0]"),
67         @iOSFindBy(xpath = "//someElement")})
68     public WebElement chainAndroidOrIOSUIAutomatorView;
69     
70     @AndroidFindBys({
71         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/content")"),
72         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"),
73         @AndroidFindBy(id = "android:id/text1")
74         })
75     public AndroidElement androidElementView;
76     
77     @AndroidFindBys({
78         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/content")"),
79         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"),
80         @AndroidFindBy(id = "android:id/text1")
81         })
82     public List<AndroidElement> androidElementViews;
83 }
原文地址:https://www.cnblogs.com/superbaby11/p/6146067.html