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

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

 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.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.ApiDemosListViewScreenByAllPossible;
import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenChaided;
import com.saucelabs.appium.page_object.android.ApiDemosListViewScreenSimple;
import com.saucelabs.appium.page_object.ios.TestAppScreenSimple; /**
* Please read about Page Object design pattern here:
* https://code.google.com/p/selenium/wiki/PageObjects
*
* Please look at:
* {@link ApiDemosListViewScreenSimple}
* {@link ApiDemosListViewScreenChaided}
* {@link ApiDemosListViewScreenByAllPossible}
* {@link TestAppScreenSimple}
*
*/
public class AndroidPageObjectTest_ByAllPossible { private WebDriver driver;
private ApiDemosListViewScreenByAllPossible 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 ApiDemosListViewScreenByAllPossible();
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS),
apiDemosPageObject);
} @After
public void tearDown() throws Exception {
driver.quit();
} /**
* Page Object best practice is to describe interactions with target
* elements by methods. These methods describe business logic of the page/screen.
* Here test interacts with lazy instantiated elements directly.
* It was done so just for obviousness
*/ @Test
public void findAllElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.findAllElementView.getAttribute("text"));
} @Test
public void findAllElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.findAllElementViews.size());
}
}

下面是页面类的代码:

 package com.saucelabs.appium.page_object.android;

 import io.appium.java_client.MobileElement;

 import java.util.List;

 import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindAll;
import io.appium.java_client.pagefactory.AndroidFindBy; /**
*
* Here is the common sample shows how to use
* {@link AndroidFindAll} annotation to describe all possible
* locators of a target element. This feature should help to
* reduce the number of page PageObject-classes
*
* 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 ApiDemosListViewScreenByAllPossible { /**
* 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
*/ @AndroidFindAll({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")"),
@AndroidFindBy(id = "android:id/Faketext1"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"), //by this locator element is found
@AndroidFindBy(id = "android:id/FakeId")
}) //If here is a list then it will be filled by all elements
//which are found using all declared locators
public List<MobileElement> findAllElementViews;//若这里是一个列表,那么列表由全部定位方法指定的全部元素组成。 @AndroidFindAll({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/Fakecontent\")"),
@AndroidFindBy(id = "android:id/Faketext1"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"), //by this locator element is found
@AndroidFindBy(id = "android:id/FakeId")
}) //If here is a single element then it will find the first
//element that is found by any of declared locators
public AndroidElement findAllElementView;//如果这里不是列表,那么变量值都将是第一个被发现的元素无论是使用哪种定位方法。
}

AndroidPageObjectTest_ByAllPossible.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. Java 经典笔试题

    这些题目对我的笔试帮助很大,有需要的朋友都可以来看看,在笔试中能遇到的题目基本上下面都会出现,虽然形式不同,当考察的基本的知识点还是相同的. 在分析中肯定有不足和谬误的地方还请大虾们能够给予及时的纠正 ...

  2. GitHub 上受欢迎的 Android UI Library 整理二

    通知 https://github.com/Tapadoo/Alerter ★2528 - 克服Toast和Snackbar的限制https://github.com/wenmingvs/Notify ...

  3. 洛谷——P1294 高手去散步

    P1294 高手去散步 题目背景 高手最近谈恋爱了.不过是单相思.“即使是单相思,也是完整的爱情”,高手从未放弃对它的追求.今天,这个阳光明媚的早晨,太阳从西边缓缓升起.于是它找到高手,希望在晨读开始 ...

  4. Java调用Python程序

    最近,需要在应用中,需要使用Java程序去调用Python代码,主要有两种实现方式. 一.使用Jython架包 因为调用的Python代码中需要使用Json对象,开始使用jython2.5.2,发现不 ...

  5. 第十讲_图像检索 Image Retrieval

    第十讲_图像检索 Image Retrieval 刚要 主要是图像预处理和特征提取+相似度计算 相似颜色检索 算法结构 颜色特征提取:统计图片的颜色成分 颜色特征相似度计算 色差距离 发展:欧式距离- ...

  6. 随想录(fatfs的学习)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上学的时候就对文件系统非常有兴趣.可是苦于没有合适的fs代码能够学习.市面上的fs代码,要么太 ...

  7. visual c++ 2013进行MySQL编程(ODBC) -- (一) 套装安装

    最近写了有些技术类文章了,只因为最近研究多了些东西,有一些项目用到了,所以自己记录一下,怕自己忘记,如果有哪位同学有自己的见解,可以邮件或者回复,技术类的探讨,不管对否,都是欢迎的. 操作之前,必须安 ...

  8. ionic 修改应用名称 、启动页出现黑白屏 及 修改百度离线地图 点聚合 图标

    1.ionic 修改应用名称 2.启动页打开后会在图片消失会出现一小段黑屏的时间 解决方法: 首先,启动页的图片消失时间默认是在config.xml配置的 <preference name=&q ...

  9. 菜鸟调错(十)——启动Tomcat报错“Unsupported major.minor version xxx ”

    近期项目更新JDK,由之前的1.7更新到1.8. 下载并安装好新的JDK以后,编译.启动Tomcat的时候,报错了.提示:"Unsupported major.minor version x ...

  10. Odoo电子数据交换(EDI)

    Odoo EDI功能能在odoo实例之间交换数据,可以交换哪些数据呢? 默认支持: account.invoice 发票,含发票行 res.currency res.partner purchase. ...