AndroidPageObjectTest_Simple.java
以下代码使用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的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- python pyd 加密相关
Dockerfile RUN 同时执行多条命令 Dokcerfile中的命令每执行一条即产生一个新的镜像,当前命令总是在最新的镜像上执行.如下Dockerfile: RUN cd /usr/share ...
- 浅谈DPCHookSSDT和RemoveDPC
最近学了DPC这一对,把Win7 32位和64位都做了,查阅了大量的资料,并且进行了大量调试,理一下思路,为了后面更好的学习. 转载请注明出处:http://www.cnblogs.com/littl ...
- js-控制浏览器和移动端的后退按钮 . popstate
//控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...
- RabbitMQ 延迟队列,消息延迟推送
目录 应用场景 消息延迟推送的实现 测试结果 应用场景 目前常见的应用软件都有消息的延迟推送的影子,应用也极为广泛,例如: 淘宝七天自动确认收货.在我们签收商品后,物流系统会在七天后延时发送一个消息给 ...
- commons-lang3工具类学习--------ObjectUtils
Object工具类 allNotNull(Object... values) 检查所有元素是否为空,返回一个boolean 如果有一个元素为空返回false,所有元素不为空或元素为empty返回tru ...
- 【maven】Maven将中央仓库修改为阿里云的仓库地址
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...
- ubuntu安装常用软件
安装unzip sudo apt-get install unzip
- CNN网络--VGGNet
Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image ...
- POJ2503字典树
此代码原始出处:http://blog.csdn.net/cnyali/article/details/47367403 #include<stdio.h> #include<str ...
- 网页Tab控件
网页Tab控件 找到:http://www.open-open.com/ajax/2_Tabs.htm 页面,查看了若干Tab控件, 找到了:http://www.open-open.com/ajax ...