以下代码使用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. 在 NetBeans 中开发一般 Java 应用程序时配置 Allatori 进行代码混淆

    要在 NetBeans 中开发一般 Java 应用程序时利用 Allatori 进行代码混淆,设置比 IntelliJ IDEA 稍微简单一点,首先在 NetBeans 项目所在硬盘目录内创建一个名为 ...

  2. AC日记——[网络流24题]骑士共存 cogs 746

    746. [网络流24题] 骑士共存 ★★☆   输入文件:knight.in   输出文件:knight.out   简单对比时间限制:1 s   内存限制:128 MB 骑士共存问题 «问题描述: ...

  3. Codeforces Round #482 (Div. 2)

    D. Kuro and GCD and XOR and SUM 字典树真好玩... 牛老板提供的思路:建1e5个 字典树,每个数插入到以它的因子为根所在的字典树中,这样就实现了整除,当然gcd(k, ...

  4. 利用github搭建个人网站

    1.注册一个github  https://github.com/ 2.新建一个仓库  仓库名 用 Owner.github.io 的格式,然后点击创建 3.源码上传至github 安装github桌 ...

  5. Object 转 String

    做项目中 : map 为Map<String,Object> a.setmoney(new BigDecimal((String)map.get("money"))); ...

  6. Hystrix的介绍和简单使用

    这周在看项目的相关代码时,接触到了Hystrix,因此查询了相关资料学习了下. 一.什么是Hystrix Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微 ...

  7. 将压缩包文件(rar/zip)伪装成图片(jpg/gif/png/ico)

    1.在Windows上使用copy命令,缺点是只能是jpg文件,gif不支持,命令如下: copy in1.jpg+in2.rar out.jpg 2.网上说使用UEdit方式可以制作gif,但是测试 ...

  8. 【mybatis】mybatis中insert操作,返回自增id

    需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...

  9. Android --修改arr文件

    1. 改为zip文件 2. 修改 3. 改后缀

  10. 使用sqlalchemy查询并删除数据表的唯一性索引

    简单描述表结构,字段类型 desc tabl_name 删除索引:alter table `db`.`table_name` drop index `index_name`  注意里面的特殊符号: ` ...