由于Appium简单的定位语句无法实现滚动屏幕到指定控件的元素,如一个text内容为“Display”的控件不在当前屏幕,需要向下滚动才能查看时,我们就需要使用findElementByAndroidUIAutomator方法,通过嵌入UiAutomator语句才能实现向下滚动到“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;
/**
*
* 项目名称:Display
* 类名称: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("启动应用!");
}
@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方法实现滚动到指定控件元素
System.out.println("scroll!");
webElement.click();
System.out.println("click!");
}
}
嵌入的语句里,我们用到的是UiScrollable对象的scrollIntoView方法,scrollIntoView 是一个特例,会返回滚动到指定控件的元素。
scrollIntoView 对任何的 UiSelector 都可以执行滚动操作。
由于这个语句过长,并且需要经常使用,索性就把UiAutomator常用的几种定位方式写成一个工具类Appium工具类方法,如下
/**
* 文件名:AppiumUtil.java
*
* 版本信息:
* 日期:2018年4月10日
* Copyright 足下 Corporation 2018
* 版权所有
*
*/
package com.hq.prodreamer;
/**
*
* 项目名称:适用于所有项目
* 类名称:AppiumUtil
* 类描述:
* 创建时间:2018年4月10日 下午4:16:52
* 修改时间: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是本人针对常用的方法、属性编写的工具类,以上代码只是初期代码,后续会根据个人需要进行修改。
文章属于个人见解,如有错误,欢迎指出。