AndroidPageObjectTest_Simple.java
2024-09-07 19:04:09
以下代码使用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; }
最新文章
- Windows7安装 .net framework 4.0
- .net Global.asax文件使用
- ICP 算法
- RSA加密(C语言)
- Monkey测试4——Monkey命令行可用的全部选项
- 深入学习 memset 函数
- poj 1511(spfa)
- SVN的忽略和只读使用方法学习记录
- 两个有序数组中查找第K大数
- 5.4 String
- android 获取IMEI号
- DataTable Javascript Link not working on 2nd page
- Netty中的Channel之数据冲刷与线程安全(writeAndFlush)
- Android通过Chrome Inspect调试WebView的H5 App出现空白页面的解决方法(不需要FQ)
- Confluence 6 用户提交的备份和恢复脚本
- 10LaTeX学习系列之---Latex的文档结构
- nginx反向代理与Real-IP和X-Forwarded-For.txt
- PEP 8 - Python代码样式指南
- spring boot 使用视图modelandview
- python实现根据文件关键字进行切分为多个文件
热门文章
- LeetCode OJ——Validate Binary Search Tree
- react状态
- MongoDB状态查询详解:db.serverStatus()
- AC日记——Dishonest Sellers Codeforces 779c
- [Python Cookbook] IPython: An Interactive Computing Environment
- layDate 日期与时间组件 入门
- javascript --- 构造器借用
- 几种支持动作模型格式的比较(MD2,MD5,sea3d) 【转】
- iOS经常使用设计模式——单例模式
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树