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

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

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

     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_ByAllPossible {
    37 
    38     private WebDriver driver;
    39     private ApiDemosListViewScreenByAllPossible 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 ApiDemosListViewScreenByAllPossible();
    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 findAllElementTest(){
    70         Assert.assertNotEquals(null, apiDemosPageObject.findAllElementView.getAttribute("text"));
    71     }    
    72     
    73     @Test
    74     public void findAllElementsTest(){
    75         Assert.assertNotEquals(0, apiDemosPageObject.findAllElementViews.size());
    76     }    
    77 }

    下面是页面类的代码:

     1 package com.saucelabs.appium.page_object.android;
     2 
     3 import io.appium.java_client.MobileElement;
     4 
     5 import java.util.List;
     6 
     7 import io.appium.java_client.android.AndroidElement;
     8 import io.appium.java_client.pagefactory.AndroidFindAll;
     9 import io.appium.java_client.pagefactory.AndroidFindBy;
    10 
    11 /**
    12  * 
    13  * Here is the common sample shows how to use
    14  * {@link AndroidFindAll} annotation to describe all possible
    15  * locators of a target element. This feature should help to
    16  * reduce the number of page PageObject-classes
    17  * 
    18  * It demonstrates how to declare screen elements using Appium
    19  * page objects facilities.
    20  * 
    21  * About Page Object design pattern read here:
    22  * https://code.google.com/p/selenium/wiki/PageObjects
    23  *
    24  */
    25 public class ApiDemosListViewScreenByAllPossible {
    26     
    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     @AndroidFindAll({
    35         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/Fakecontent")"),
    36         @AndroidFindBy(id = "android:id/Faketext1"),
    37         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"), //by this locator element is found
    38         @AndroidFindBy(id = "android:id/FakeId")
    39         }) //If here is a list then it will be filled by all elements
    40     //which are found using all declared locators
    41     public List<MobileElement> findAllElementViews;//若这里是一个列表,那么列表由全部定位方法指定的全部元素组成。
    42     
    43     @AndroidFindAll({
    44         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/Fakecontent")"),
    45         @AndroidFindBy(id = "android:id/Faketext1"),
    46         @AndroidFindBy(uiAutomator = "new UiSelector().resourceId("android:id/list")"), //by this locator element is found
    47         @AndroidFindBy(id = "android:id/FakeId")
    48         }) //If here is a single element then it will find the first 
    49     //element that is found by any of declared locators
    50     public AndroidElement findAllElementView;//如果这里不是列表,那么变量值都将是第一个被发现的元素无论是使用哪种定位方法
    51 }
  • 相关阅读:
    MySQL -- 表联结
    Unittest方法 -- 项目实现自动发送邮件
    Linux工作中常用命令
    Requests方法 -- Token获取操作
    Requests方法 -- 关联用例执行
    Requests方法 -- Blog流程类进行关联
    Requests方法 -- 参数化
    defer和async的区别
    js函数收藏:获取cookie值
    XSHELL使用技巧总结
  • 原文地址:https://www.cnblogs.com/superbaby11/p/6146158.html
Copyright © 2011-2022 走看看