背景:App某些页面元素无法通过Appium或其他方式抓取到,无法通过传统方式定位到元素。
原理:获取到当前屏幕的尺寸,在要操作的元素的屏幕位置构造像素点位,进而执行点击或滑动操作。
注:坐标定位时采用相对定位方式,可适配其他分辨率。
io.appium
java-client
7.1.0
org.testng
testng
6.10
test
前提准备:连接驱动设备
public DesiredCapabilities getCapabilities() {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("platformVersion", "9");
desiredCapabilities.setCapability("deviceName", "923QEDUK22434");
desiredCapabilities.setCapability("appPackage", "com.leke.lekechat");
desiredCapabilities.setCapability("appActivity", "com.leke.lekechat.ui.SplashFirstActivity");
desiredCapabilities.setCapability("automationName", "UiAutomator2");
desiredCapabilities.setCapability("noReset", "True");
return desiredCapabilities;
}
public AndroidDriver getDriver() {
private static final String APPIUM_SERVER_REMOTE = "http://localhost:4723/wd/hub";
DesiredCapabilities desiredCapabilities =this.getCapabilities();
URL remoteUrl = null;
try {
remoteUrl = new URL(APPIUM_SERVER_REMOTE);
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return driver;
}
实现点击:
public void clickElement_OverScreen( AndroidDriver driver,int [ ][ ] arr) {
TouchAction ta = new TouchAction(driver);
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int P_base_X=arr[0][0]; //元素基准位置坐标
int P_base_y=arr[0][1];
int P_base_Screen_X=arr[1][0];//元素所在页的屏幕基准尺寸
int P_base_Screen_Y=arr[1][1];
int point_X=this.formatNumber(P_base_X, P_base_Screen_X, width);
int point_Y=this.formatNumber(P_base_y, P_base_Screen_Y, height);
PointOption point_Concat=PointOption.point(point_X,point_Y);
ta.press(point_Concat).release().perform();
}
public int formatNumber(int P_1,int P_2,int P_3) {
float dd=(float)P_1/(float)P_2;
DecimalFormat df = new DecimalFormat("0.00000000");//格式化小数,不足的补0
String rat = df.format((double)dd);
float ff=Float.parseFloat(rat);
int formatNum =(int) (ff*P_3);
return formatNum;
}
示例:
@Test
public void clickElement_1() {
//arr[0]:当前屏幕上元素的坐标;arr[1]:当前屏幕的分辨率
int[][] keyboardArr = { { 980, 2050 }, { 1080, 2098 } }; //手机键盘的搜索按钮坐标
AndroidDriver driver = this.getDriver();
this.clickElement_OverScreen(driver, keyboardArr); //屏幕构造坐标点位并点击
}
public void executeSlide(AndroidDriver driver,int [ ][ ] arr) {
TouchAction ta = new TouchAction(driver);
int width = driver.manage().window().getSize().width;//当前屏幕的宽度
int height = driver.manage().window().getSize().height; //当前屏幕的高度
//new一个TouchAction对象,调用其按压press()方法,输入坐标点,moveTo移动到下一个坐标点,之后调用release()和perform()方法执行
PointOption P_B= PointOption.point(width*arr[0][0]/arr[0][1], height*arr[1][0]/arr[1][1]);
PointOption P_N= PointOption.point(width*arr[2][0]/arr[2][1], height*arr[3][0]/arr[3][1]);
WaitOptions waitOption = WaitOptions.waitOptions(Duration.ofSeconds(1));//设置动作持续时间:按压一秒
ta.press(P_B).waitAction(waitOption).moveTo(P_N).release().perform();//按压一秒——移动——松开释放
}
@Test
public void clickElement_1() {
//表示沿X轴平移——左滑(0.9X-->0.3X) Y轴坐标为屏幕height的0.8倍
int[][] arr = { { 9, 10 }, { 4, 5 }, { 3, 10 }, { 4, 5 } };
AndroidDriver driver = this.getDriver();
this.executeSlide(driver, arr);
}