以下代码使用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. js中加“var”和不加“var”的区别,看完觉得这么多年js白学了

    Javascript声明变量的时候,虽然用var关键字声明和不用关键字声明,很多时候运行并没有问题,但是这两种方式还是有区别的.可以正常运行的代码并不代表是合适的代码. var num = 1: 是在 ...

  2. (49)C# npoi-word

    //新建段落 XWPFParagraph p1 = doc.CreateParagraph(); //对齐方式 p1.SetAlignment(ParagraphAlignment.LEFT); p1 ...

  3. 虚拟机centos 同一个tomcat、不同端口访问不同的项目

    在tomcat中复制webapps目录,并重命名:

  4. Xcode文件名后的字母含义

    Xcode文件名后的字母含义   在Xcode中,左侧的dock区域显示项目结构.很多时候,文件名后方会出现一个字母提示,如M.A之类的.这实际是一种提示符号.如果项目中使用SVN.Git等版本控制工 ...

  5. maven命令行创建project

    创建普通java project: mvn archetype:generate -DgroupId=com.vincent -DartifactId=Java_Project -DpackageNa ...

  6. js获得checkbox选中值及input后面的文本

    原文:http://blog.csdn.net/u014079773/article/details/51865596 js如何获得多个checkbox选中的值及input后面所跟的文本 <!d ...

  7. iOS--实时监控网络状态的改变

    在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...

  8. curl如何发送json数据?如何发送form数据?python的restfull又该如何获取这些数据?

    1.python使用flask+flask_restfull框架写的api接口,做为服务 2.curl 做为客户端发送数据 from flask import request curl发送json的方 ...

  9. Celery 启动报错 can_read() got an unexpected keyword argument timeout

    问题: Celery 启动报错 can_read() got an unexpected keyword argument timeout 方案:更改redis版本和celery版本,我使用下面的ce ...

  10. [反汇编练习] 160个CrackMe之035

    [反汇编练习] 160个CrackMe之035. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...