由于Appium簡(jiǎn)單的定位語(yǔ)句無(wú)法實(shí)現(xiàn)滾動(dòng)屏幕到指定控件的元素,如一個(gè)text內(nèi)容為“Display”的控件不在當(dāng)前屏幕疮方,需要向下滾動(dòng)才能查看時(shí),我們就需要使用findElementByAndroidUIAutomator方法双饥,通過(guò)嵌入U(xiǎn)iAutomator語(yǔ)句才能實(shí)現(xiàn)向下滾動(dòng)到“Display”控件處
完整代碼如下
package com.hq.prodreamer;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
/**
*
* 項(xiàng)目名稱(chēng):Display
* 類(lèi)名稱(chēng):Display
*/
public class Display
{
private AndroidDriver driver;
@Before
public void setUp()
throws Exception
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Devices");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "7.0");
capabilities.setCapability("udid", "0123456789ABCDEF");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
capabilities.setCapability("NoReset", true);
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("啟動(dòng)應(yīng)用叫潦!");
}
@After
public void tearDown()
throws Exception
{
driver.quit();
System.out.println("退出driver!");
}
@Test
public void test()
{
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement webElement = driver.findElementByAndroidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text(\"Display\"))");
//使用scrollIntoView方法實(shí)現(xiàn)滾動(dòng)到指定控件元素
System.out.println("scroll!");
webElement.click();
System.out.println("click!");
}
}
嵌入的語(yǔ)句里爸邢,我們用到的是UiScrollable對(duì)象的scrollIntoView方法巫员,scrollIntoView 是一個(gè)特例,會(huì)返回滾動(dòng)到指定控件的元素甲棍。
scrollIntoView 對(duì)任何的 UiSelector 都可以執(zhí)行滾動(dòng)操作简识。
由于這個(gè)語(yǔ)句過(guò)長(zhǎng)赶掖,并且需要經(jīng)常使用,索性就把UiAutomator常用的幾種定位方式寫(xiě)成一個(gè)工具類(lèi)Appium工具類(lèi)方法七扰,如下
/**
* 文件名:AppiumUtil.java
*
* 版本信息:
* 日期:2018年4月10日
* Copyright 足下 Corporation 2018
* 版權(quán)所有
*
*/
package com.hq.prodreamer;
/**
*
* 項(xiàng)目名稱(chēng):適用于所有項(xiàng)目
* 類(lèi)名稱(chēng):AppiumUtil
* 類(lèi)描述:
* 創(chuàng)建時(shí)間:2018年4月10日 下午4:16:52
* 修改時(shí)間:2018年4月10日 下午4:16:52
* 修改備注:
* @version
*
*/
public class AppiumUtil
{
// find element by name
public static final String NAME = "NAME";
// find element by id
public static final String ID = "ID";
// find element by classname
public static final String CLASSNAME = "CLASSNAME";
// find element by AccessibilityId
public static final String ACCESSIBILITYID = "AccessibilityId";
public static final String INDEX = "INDEX";
public static final String INSTENCE = "INSTENCE";
// name,id,AccessibilityId method
public String scrollTo(String content, String type)
{
String uiautomatorStr = null;
if (type == "NAME")
{
uiautomatorStr =
"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text(\"" + content
+ "\"))";
}
else if (type == "ID")
{
uiautomatorStr =
"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().resourceId(\""
+ content + "\"))";
}
else if (type == "AccessibilityId")
{
uiautomatorStr =
"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().description(\""
+ content + "\"))";
}
return uiautomatorStr;
}
// className method
public String scrollTo(String content, String className, String type, int number)
{
String uiautomatorStr = null;
// find element by classname && index
if (className == "CLASSNAME" && type == "INDEX")
{
uiautomatorStr = "new UiScrollable(new UiSelector().scrollable(true).index(" + number
+ ")).getChildByText(new UiSelector().className(\"" + content + "\")";
}
// find element by classname && instance
else if (className == "CLASSNAME" && type == "INSTENCE")
{
uiautomatorStr = "new UiScrollable(new UiSelector().scrollable(true).instance(" + number
+ ")).getChildByText(new UiSelector().className(\"" + content + "\")";
}
return uiautomatorStr;
}
}
AppiumUtil是本人針對(duì)常用的方法奢赂、屬性編寫(xiě)的工具類(lèi),以上代碼只是初期代碼颈走,后續(xù)會(huì)根據(jù)個(gè)人需要進(jìn)行修改膳灶。
文章屬于個(gè)人見(jiàn)解,如有錯(cuò)誤立由,歡迎指出轧钓。