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

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

 package com.saucelabs.appium;

 import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.remote.MobileCapabilityType; import java.io.File;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit; import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory; import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenSimple;//页面类 public class AndroidPageObjectTest_Simple { private WebDriver driver;
private ApiDemosListViewScreenSimple apiDemosPageObject; @Before
public void setUp() throws Exception {
//File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File("E:/package");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); apiDemosPageObject = new ApiDemosListViewScreenSimple();
//This time out is set because test can be run on slow Android SDK emulator
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS),
apiDemosPageObject);
} @After
public void tearDown() throws Exception {
driver.quit();
} @Test
public void findByElementsTest() {
Assert.assertNotEquals(0, apiDemosPageObject.textVieWs.size());
} @Test
public void findByElementTest() {
Assert.assertNotEquals(null, apiDemosPageObject.textView.getAttribute("text"));
} @Test
public void androidFindByElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.androidTextViews.size());
} @Test
public void androidFindByElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.androidTextView.getAttribute("text"));
} @Test
public void checkThatElementsWereNotFoundByIOSUIAutomator(){
Assert.assertEquals(0, apiDemosPageObject.iosTextViews.size());
} @Test
public void checkThatElementWasNotFoundByIOSUIAutomator(){
String nsee = null;
try{
apiDemosPageObject.iosTextView.getAttribute("text");
}
catch (Exception e){
nsee = e.getClass().getName();//获取异常类的名字,用于断言特定类别的异常发生了。
}
Assert.assertEquals(nsee,"org.openqa.selenium.NoSuchElementException");
} @Test
public void androidOrIOSFindByElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.androidOriOsTextViews.size());
} @Test
public void androidOrIOSFindByElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.androidOriOsTextView.getAttribute("text"));
} @Test
public void androidFindByUIAutomatorElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.androidUIAutomatorViews.size());
} @Test
public void androidFindByUIAutomatorElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.androidUIAutomatorView.getAttribute("text"));
} @Test
public void areMobileElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.mobileElementViews.size());
} @Test
public void isMobileElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.mobileElementView.getAttribute("text"));
} @Test
public void areMobileElements_FindByTest(){
Assert.assertNotEquals(0, apiDemosPageObject.mobiletextVieWs.size());
} @Test
public void isMobileElement_FindByTest(){
Assert.assertNotEquals(null, apiDemosPageObject.mobiletextVieW.getAttribute("text"));
} @Test
public void areRemoteElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.remoteElementViews.size());
} @Test
public void isRemoteElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.remotetextVieW.getAttribute("text"));//即使apiDemosPageObject.remotetextVieW不存在,该语句也不会抛出异常,应该是被PageFactory模式处理了。
}
}

页面类的代码:

 package com.saucelabs.appium.page_object.android;

 import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSFindBy; import java.util.List; import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.FindBy; /**
*
* Here is the common sample shows how to use
* {@link FindBy}, {@link AndroidFindBy} and {@link iOSFindBy}
* annotations.
*
* Also it demonstrates how to declare screen elements using Appium
* page objects facilities.
*
* About Page Object design pattern read here:
* https://code.google.com/p/selenium/wiki/PageObjects
*
*/
public class ApiDemosListViewScreenSimple {
/**
* Page Object best practice is to describe interactions with target
* elements by methods. This methods describe business logic of the page/screen.
* Here lazy instantiated elements are public.
* It was done so just for obviousness
*/ //Common Selenium @FindBy annotations are effective
//against browser apps and web views. They can be used against native
//content. But it is useful to know that By.css, By.link, By.partialLinkText
//are invalid at this case.
@FindBy(className = "android.widget.TextView")
public List<WebElement> textVieWs; //@AndroidFindBy annotation is designed to be used for Android native content
//description.
@AndroidFindBy(className = "android.widget.TextView")
public List<WebElement> androidTextViews; @iOSFindBy(uiAutomator = ".elements()[0]")
public List<WebElement> iosTextViews; //if it is necessary to use the same Page Object
//in the browser and cross platform mobile app testing
//then it is possible to combine different annotations
@FindBy(css = "someBrowserCss") //this locator is used when here is browser (desktop or mobile)
@iOSFindBy(uiAutomator = ".elements()[0]") //this locator is used when here is iOS native content
@AndroidFindBy(className = "android.widget.TextView") //this locator is used when here is Android
//native content
public List<WebElement> androidOriOsTextViews; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public List<WebElement> androidUIAutomatorViews; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public List<MobileElement> mobileElementViews; //Also with Appium page object tools it is
//possible to declare RemoteWebElement or any MobileElement subclass @FindBy(className = "android.widget.TextView")
public List<MobileElement> mobiletextVieWs; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public List<RemoteWebElement> remoteElementViews; @FindBy(id = "android:id/text1")
public WebElement textView; @AndroidFindBy(className = "android.widget.TextView")
public WebElement androidTextView; @iOSFindBy(uiAutomator = ".elements()[0]")
public WebElement iosTextView; @AndroidFindBy(className = "android.widget.TextView")
@iOSFindBy(uiAutomator = ".elements()[0]")
public WebElement androidOriOsTextView; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public WebElement androidUIAutomatorView; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public MobileElement mobileElementView; @FindBy(className = "android.widget.TextView")
public MobileElement mobiletextVieW; @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/text1\")")
public RemoteWebElement remotetextVieW; }

AndroidPageObjectTest_Simple.java的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. LeetCode OJ--Rotate Image

    http://oj.leetcode.com/problems/rotate-image/ 将矩阵顺时针旋转90度.要求为原地算法. 注意对输入的测试,矩阵为空,长度为1,为2时…… #include ...

  2. IOS-input元素光标偏移乱跑,是什么原因

    这中情况只会出现在一些ios手机上,是兼容性的问题,当然我没有认真的确认过是出现在那些ios系统上的: 之所以出现这种情况,肯定是在input上或TA的父元素上用到了position:fixed; 目 ...

  3. 网络数据注入工具HexInject

    网络数据注入工具HexInject   对于Kali Linux提供的工具HexInject来说,数据注入才是其最重要的功能.它可以直接向网络注入渗透人员构造的数据包,也可以篡改网络传输的数据.为了避 ...

  4. spring mvc 编写处理带参数的Controller

    在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeControl ...

  5. javascript 函数初探 (三)--- javascript 变量的作用域

    javascript 变量的作用域: 这是一个至关重要的问题.特别是当我们从别的语言转向javascript时,必须要明白一点,即在javascript中,变量的定义并不是以代码块作为作用域的,而是以 ...

  6. iOS真机测试,为Provisioning添加设备

    ------------添加设备到provisioning------------- 1,登陆https://developer.apple.com/devcenter/ios/index.actio ...

  7. Linux 设备驱动开发 —— platform设备驱动应用实例解析

    前面我们已经学习了platform设备的理论知识Linux 设备驱动开发 —— platform 设备驱动 ,下面将通过一个实例来深入我们的学习. 一.platform 驱动的工作过程 platfor ...

  8. HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  10. ActiveX控件打包成Cab置于网页中自动下载安装

    [背景] http://www.360doc.com/content/13/1120/20/10159093_330853247.shtml 做过ActiveX控件的朋友都知道,要想把自己做的Acti ...