背景:App某些页面元素无法通过Appium或其他方式抓取到,无法通过传统方式定位到元素。
原理:获取到当前屏幕的尺寸,在要操作的元素的屏幕位置构造像素点位,进而执行点击或滑动操作。
注:坐标定位时采用相对定位方式,可适配其他分辨率。
1、坐标点击屏幕元素
<!– https://mvnrepository.com/artifact/io.appium/java-client –>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.1.0</version>
</dependency><dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
前提准备:连接驱动设备
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;}
示例:
@Testpublic void clickElement_1() {//arr[0]:当前屏幕上元素的坐标;arr[1]:当前屏幕的分辨率int[][] keyboardArr = { { 980, 2050 }, { 1080, 2098 } }; //手机键盘的搜索按钮坐标AndroidDriver driver = this.getDriver();this.clickElement_OverScreen(driver, keyboardArr); //屏幕构造坐标点位并点击}
2、滑动屏幕
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();//按压一秒——移动——松开释放}@Testpublic 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);}