zoukankan      html  css  js  c++  java
  • AndroidPageObjectTest_Simple.java

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

    //这个脚本用于演示PageFactory的功能:使用注解@FindBy、@AndroidFindBy、@IOSFindBy定位元素。注解用法参考页面类代码。

      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"));//即使apiDemosPageObject.remotetextVieW不存在,该语句也不会抛出异常,应该是被PageFactory模式处理了。
    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 }
  • 相关阅读:
    微信开发SDK使用教程--手机微信群聊新增通知服务端
    微信开发SDK使用教程--手机微信联系人信息上传服务端
    微信开发SDK使用教程--手机端接收发送朋友圈任务指令后数据回传服务端
    微信二次开发SDK使用教程--手机端向服务端发送回复朋友圈评论任务反馈服务端
    js 自执行匿名函数(转载)
    js继承中,原型属性的继承探究
    搭建邮件服务器和使用压测工具的总结
    window.onload和window.document.readystate的探究
    读艾伦的jQuery的无new构建,疑惑分析——jquery源码学习一
    WPF页面 全球化和本地化
  • 原文地址:https://www.cnblogs.com/superbaby11/p/6142447.html
Copyright © 2011-2022 走看看