以下代码使用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. Python Challenge 第六关

    第六关只有一张图和一个 PayPal 的链接,右键源代码注释中写着 PayPal 是作者要赞助的,跟题目没关系,其他的提示只有注释中写的个 zip.试过下图片,改图片扩展名等等都失败了,最后乱试改了下 ...

  2. js-判断移动端用户是横屏放的还是竖屏放的

    在一些移动端页面中,页面的样式我们就支持竖屏播放,这时我们就需要判断是横屏还是竖屏了,不同的方向显示冉的样式. //判断手机横屏竖屏 var html = ""; window.a ...

  3. Java中将List转成逗号数组的方案

    说明:逗号字符串转成数组或者List都是可以的,反过来依然可行:但是如果是List<String>转List<Integer>,基本误解,在Java 7只能for循环,如果在J ...

  4. SQL 列转行与行转列

    假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/ -------------- ...

  5. DevExpress右键菜单使用 z

    添加BarManager和popupMenu组建: 设置popupMenu组件的Manager属性: 右键点击popupMenu主键,点击Customize选项添加菜单:    然后便可添加到鼠标事件 ...

  6. 分布式服务框架选型:面对Dubbo,阿里巴巴为什么选择了HSF?

    转载:http://www.sohu.com/a/141490021_268033 阿里巴巴集团内部使用的分布式服务框架 HSF(High Speed Framework,也有人戏称“好舒服”)已经被 ...

  7. eclipse中文凝视字体太小解决方法

    新安装的eclipse中文凝视字体太小.解决方法例如以下: 打开Elcipse-->点击菜单条上的"Windows"-->点击"Preferences&quo ...

  8. 百科知识 DMG文件如何打开

    1 DMG格式是在MAC系统上的一个镜象文件,也可以说是压缩文件,如果你是使用苹果机或在PC上装了MAC OS X for X86,在MAC系统上双击这个文件就可以解开了:现在在Windows平台,我 ...

  9. 自己定义一个Dialog样式的Activity窗体,切换到Dialog的方法

    首先定义一个style 在style里面加入 <style name="MyDialog" parent="@android:Theme.Dialog"& ...

  10. table 设置边框

    本文引自:https://www.cnblogs.com/leona-d/p/6125896.html 示例代码: <!DOCTYPE html> <html lang=" ...