码迷,mamicode.com
首页 > 移动开发 > 详细

AndroidPageObjectTest_Simple.java

时间:2016-12-07 20:50:54      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:down   demo   div   tcap   pps   val   ant   select   ota   

以下代码使用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 
 25     private WebDriver driver;
 26     private ApiDemosListViewScreenSimple apiDemosPageObject;
 27     
 28     @Before
 29     public void setUp() throws Exception {
 30         //File classpathRoot = new File(System.getProperty("user.dir"));
 31         File appDir = new File("E:/package");
 32         File app = new File(appDir, "ApiDemos-debug.apk");
 33         DesiredCapabilities capabilities = new DesiredCapabilities();
 34         capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
 35         capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
 36         driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 37         
 38         apiDemosPageObject = new ApiDemosListViewScreenSimple();
 39         //This time out is set because test can be run on slow Android SDK emulator
 40         PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), 
 41                 apiDemosPageObject);
 42     }
 43     
 44     @After
 45     public void tearDown() throws Exception {
 46         driver.quit();
 47     }
 48     
 49     @Test
 50     public void findByElementsTest() {
 51         Assert.assertNotEquals(0, apiDemosPageObject.textVieWs.size());
 52     }
 53 
 54     @Test
 55     public void findByElementTest() {
 56         Assert.assertNotEquals(null, apiDemosPageObject.textView.getAttribute("text"));
 57     }
 58 
 59 
 60     @Test
 61     public void androidFindByElementsTest(){
 62         Assert.assertNotEquals(0, apiDemosPageObject.androidTextViews.size());
 63     }
 64 
 65     @Test
 66     public void androidFindByElementTest(){
 67         Assert.assertNotEquals(null, apiDemosPageObject.androidTextView.getAttribute("text"));
 68     }
 69 
 70     @Test
 71     public void checkThatElementsWereNotFoundByIOSUIAutomator(){
 72         Assert.assertEquals(0, apiDemosPageObject.iosTextViews.size());
 73     }
 74 
 75     @Test
 76     public void checkThatElementWasNotFoundByIOSUIAutomator(){
 77         String nsee = null;
 78         try{
 79             apiDemosPageObject.iosTextView.getAttribute("text");
 80         }
 81         catch (Exception e){
 82             nsee =  e.getClass().getName();
 83         }
 84         Assert.assertEquals(nsee,"org.openqa.selenium.NoSuchElementException");
 85     }
 86 
 87     @Test
 88     public void androidOrIOSFindByElementsTest(){
 89         Assert.assertNotEquals(0, apiDemosPageObject.androidOriOsTextViews.size());
 90     }
 91 
 92     @Test
 93     public void androidOrIOSFindByElementTest(){
 94         Assert.assertNotEquals(null, apiDemosPageObject.androidOriOsTextView.getAttribute("text"));
 95     }
 96 
 97     @Test
 98     public void androidFindByUIAutomatorElementsTest(){
 99         Assert.assertNotEquals(0, apiDemosPageObject.androidUIAutomatorViews.size());
100     }
101 
102     @Test
103     public void androidFindByUIAutomatorElementTest(){
104         Assert.assertNotEquals(null, apiDemosPageObject.androidUIAutomatorView.getAttribute("text"));
105     }
106 
107     @Test
108     public void areMobileElementsTest(){
109         Assert.assertNotEquals(0, apiDemosPageObject.mobileElementViews.size());
110     }
111 
112     @Test
113     public void isMobileElementTest(){
114         Assert.assertNotEquals(null, apiDemosPageObject.mobileElementView.getAttribute("text"));
115     }
116 
117     @Test
118     public void areMobileElements_FindByTest(){
119         Assert.assertNotEquals(0, apiDemosPageObject.mobiletextVieWs.size());
120     }
121 
122     @Test
123     public void isMobileElement_FindByTest(){
124         Assert.assertNotEquals(null, apiDemosPageObject.mobiletextVieW.getAttribute("text"));
125     }
126 
127     @Test
128     public void areRemoteElementsTest(){
129         Assert.assertNotEquals(0, apiDemosPageObject.remoteElementViews.size());
130     }
131 
132     @Test
133     public void isRemoteElementTest(){
134         Assert.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")
40     public List<WebElement> textVieWs;
41 
42     //@AndroidFindBy annotation is designed to be used for Android native content 
43     //description.
44     @AndroidFindBy(className = "android.widget.TextView")
45     public List<WebElement> androidTextViews;
46 
47     @iOSFindBy(uiAutomator = ".elements()[0]")
48     public 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
57     public List<WebElement> androidOriOsTextViews;
58 
59     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
60     public List<WebElement> androidUIAutomatorViews;
61 
62     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
63     public 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")
67     public List<MobileElement> mobiletextVieWs;
68 
69     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
70     public List<RemoteWebElement> remoteElementViews;
71 
72     @FindBy(id = "android:id/text1")
73     public WebElement textView;
74 
75     @AndroidFindBy(className = "android.widget.TextView")
76     public WebElement androidTextView;
77 
78     @iOSFindBy(uiAutomator = ".elements()[0]")
79     public WebElement iosTextView;
80 
81     @AndroidFindBy(className = "android.widget.TextView")
82     @iOSFindBy(uiAutomator = ".elements()[0]")
83     public WebElement androidOriOsTextView;
84 
85     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
86     public WebElement androidUIAutomatorView;
87 
88     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
89     public MobileElement mobileElementView;
90 
91     @FindBy(className = "android.widget.TextView")
92     public MobileElement mobiletextVieW;
93 
94     @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
95     public RemoteWebElement remotetextVieW;
96 
97 }

 

AndroidPageObjectTest_Simple.java

标签:down   demo   div   tcap   pps   val   ant   select   ota   

原文地址:http://www.cnblogs.com/superbaby11/p/6142447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!