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

//这个脚本用于演示PageFactory的功能:链式注解@AndroidFindBys、@IOSFindBys。具体用法参考页面类的代码。

 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_Chained { private WebDriver driver;
private ApiDemosListViewScreenChaided 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 ApiDemosListViewScreenChaided();
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 androidChainSearchElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.chainElementViews.size());
} @Test
public void androidChainSearchElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.chainElementView.getAttribute("text"));
} @Test
public void androidOrIOSFindByElementsTest_ChainSearches(){
Assert.assertNotEquals(0, apiDemosPageObject.chainAndroidOrIOSUIAutomatorViews.size());
} @Test
public void androidOrIOSFindByElementTest_ChainSearches(){
Assert.assertNotEquals(null, apiDemosPageObject.chainAndroidOrIOSUIAutomatorView.getAttribute("text"));
} @Test
public void isAndroidElementTest(){
Assert.assertNotEquals(null, apiDemosPageObject.androidElementView.getAttribute("text"));
} @Test
public void areAndroidElementsTest(){
Assert.assertNotEquals(0, apiDemosPageObject.androidElementViews.size());
}
}

页面类的代码如下:

 package com.saucelabs.appium.page_object.android;

 import io.appium.java_client.android.AndroidElement;

 import java.util.List;

 import org.openqa.selenium.WebElement;

 import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AndroidFindBys;
import io.appium.java_client.pagefactory.iOSFindBy;
import io.appium.java_client.pagefactory.iOSFindBys; /**
*
* Here is the common sample shows how to use
* {@link AndroidFindBys} annotation to describe the chain of the
* searching for the target element of a native Android app content.
*
* 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 ApiDemosListViewScreenChaided { /**
* 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
*/ @AndroidFindBys({//用多个定位方法描述一组元素。
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"), //the searching
//starts here
@AndroidFindBy(className = "android.widget.TextView") //this element is nested
//and so on
})
public List<WebElement> chainElementViews; @AndroidFindBys({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/content\")"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"),
@AndroidFindBy(id = "android:id/text1")
})
@iOSFindBys({@iOSFindBy(uiAutomator = ".elements()[0]"),
@iOSFindBy(xpath = "//someElement")})
public List<WebElement> chainAndroidOrIOSUIAutomatorViews; @AndroidFindBys({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"),
@AndroidFindBy(className = "android.widget.TextView")
})
public WebElement chainElementView; @AndroidFindBys({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/content\")"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"),
@AndroidFindBy(id = "android:id/text1")
})
@iOSFindBys({@iOSFindBy(uiAutomator = ".elements()[0]"),
@iOSFindBy(xpath = "//someElement")})
public WebElement chainAndroidOrIOSUIAutomatorView; @AndroidFindBys({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/content\")"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"),
@AndroidFindBy(id = "android:id/text1")
})
public AndroidElement androidElementView; @AndroidFindBys({
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/content\")"),
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/list\")"),
@AndroidFindBy(id = "android:id/text1")
})
public List<AndroidElement> androidElementViews;
}

AndroidPageObjectTest_Chained.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. 通过jQuery Ajax提交表单数据时同时上传附件

    1.使用场景:需要使用ajax提交表单,但是提交的表单里含有附件上传 2.代码实现方式: <!-- HTML代码 --> <form method="post" ...

  2. E. Sergey and Subway

    比赛时候写复杂了…… 我写的是 计算每个节点树内所有点到某个点的距离和. #include <bits/stdc++.h> using namespace std; typedef lon ...

  3. VUE之命令行报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead 解决办法

    Failed to compile. ./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-5992 ...

  4. tomcat7.0.55配置单向和双向HTTPS连接

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...

  5. SpringBoot系列之(一)helloworld!

    要说什么最流行,现阶段肯定是Springboot和Springcloud,在Spring官方网站上第一个就是springboot,可见对springboot的重视程度.主要原因springboot集成 ...

  6. String,StringBuffer,StringBuilder源码分析

    1.类结构 String Diagrams StringBuffer Diagrams StringBuilder Diagrams 通过以上Diagrams可以看出,String,StringBuf ...

  7. 使用UltraISO为U盘或内存卡制作系统安装工具

    此软件可以为U盘制作Windows安装版的启动工具,也可以为Linux制作启动工具,尤其是Ubuntu这些.提示:推荐购买一些读取速度快一些的U盘,运行起来可以节省很多时间. 如果是内存卡要实现此功能 ...

  8. http://blog.csdn.net/zh521zh/article/details/52687922

    http://blog.csdn.net/zh521zh/article/details/52687922

  9. hive界面工具SQL Developer的安装;使用sql developer连接hive;使用sql developer连接mysql

    需要oracle帐号登录后下载 1.下载: http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/inde ...

  10. ccs 中的定位

    一.相对定位 position:relative; 作用: 相对定位 一般加给定位元素父级    特点: (1)不脱离文档流: (2)不改变元素类型: (3)参照物是元素本身: 二.绝对定位 posi ...