1.移动端地图放大缩小,需要2个手指同时操作,多点触摸,如何实现呢?,
MultiTouchAction类可以模拟用户多点触摸操作,包含add(),perform()两个方法,结合TouchAction模拟多根手指滑动的效果
举例:
2.下载:百度地图,先确定其包名和入口
package: com.baidu.BaiduMap
入口launchable-activity: com.baidu.baidumaps.WelcomeScreen
3.新建MultiTest.class
脚本如下:
package com.test.firstAppium;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
public class MultiTouchTest {
// 声明全局变量
public static AndroidDriver<WebElement> androidDriver;
public static AndroidDriver<WebElement> androidDriver;
@BeforeTest
public void setup() throws MalformedURLException {
// 1.創建配置對象
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
// 2.添加配置
// deviceName:可以找到我们测试的设备
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
// platformName:测试平台android or IOS
desiredCapabilities.setCapability("platformName", "Android");
// appPackage:找到测试 APP:aapt dump badging.....
desiredCapabilities.setCapability("appPackage", "com.baidu.BaiduMap");
// automationName:uiautomator2来解决输入框输入不了数据
// 自动化引擎
// desiredCapabilities.setCapability("automationName", "uiautomator2");
// 不清理应用的数据,默认是清理的
desiredCapabilities.setCapability("noReset", true);
// appActivity:测试app入口
desiredCapabilities.setCapability("appActivity", "com.baidu.baidumaps.WelcomeScreen");
// 创建驱动
// 传入2个参数,第一个参数:Appium通讯地址,第二个参数:配置对象
androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
// 隐式等待
androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
public void setup() throws MalformedURLException {
// 1.創建配置對象
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
// 2.添加配置
// deviceName:可以找到我们测试的设备
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
// platformName:测试平台android or IOS
desiredCapabilities.setCapability("platformName", "Android");
// appPackage:找到测试 APP:aapt dump badging.....
desiredCapabilities.setCapability("appPackage", "com.baidu.BaiduMap");
// automationName:uiautomator2来解决输入框输入不了数据
// 自动化引擎
// desiredCapabilities.setCapability("automationName", "uiautomator2");
// 不清理应用的数据,默认是清理的
desiredCapabilities.setCapability("noReset", true);
// appActivity:测试app入口
desiredCapabilities.setCapability("appActivity", "com.baidu.baidumaps.WelcomeScreen");
// 创建驱动
// 传入2个参数,第一个参数:Appium通讯地址,第二个参数:配置对象
androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
// 隐式等待
androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
// 自定义方法
@Test
public void TestZoom() throws InterruptedException {
Thread.sleep(6000);
//1.实例化multiTouchAction对象
MultiTouchAction multiTouchAction =new MultiTouchAction(androidDriver);
//2.实例化2个TouchAction
TouchAction touchAction1= new TouchAction<>(androidDriver);
TouchAction touchAction2= new TouchAction<>(androidDriver);
//3.点击放大+
androidDriver.findElementById("com.baidu.BaiduMap:id/zoom_in").click();
//点击缩小 -
androidDriver.findElementById("com.baidu.BaiduMap:id/zoom_out").click();
//4.得到当前屏幕得高度和宽度
int x=androidDriver.manage().window().getSize().getWidth();
int y=androidDriver.manage().window().getSize().getHeight();
//第一根手指从B到A点滑动,放大,从小到上
touchAction1.press(PointOption.point(x*4/10,y*4/10))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.moveTo(PointOption.point(x*2/10,y*2/10)).release();
//第二根手指从B到A点滑动,放大,从小到上
touchAction2.press(PointOption.point(x*6/10,y*6/10))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.moveTo(PointOption.point(x*2/10,y*2/10)).release();
//把2手指的动作添加到MultiTouchAction里面
multiTouchAction.add(touchAction1).add(touchAction2);
multiTouchAction.perform();
}
@Test
public void TestZoom() throws InterruptedException {
Thread.sleep(6000);
//1.实例化multiTouchAction对象
MultiTouchAction multiTouchAction =new MultiTouchAction(androidDriver);
//2.实例化2个TouchAction
TouchAction touchAction1= new TouchAction<>(androidDriver);
TouchAction touchAction2= new TouchAction<>(androidDriver);
//3.点击放大+
androidDriver.findElementById("com.baidu.BaiduMap:id/zoom_in").click();
//点击缩小 -
androidDriver.findElementById("com.baidu.BaiduMap:id/zoom_out").click();
//4.得到当前屏幕得高度和宽度
int x=androidDriver.manage().window().getSize().getWidth();
int y=androidDriver.manage().window().getSize().getHeight();
//第一根手指从B到A点滑动,放大,从小到上
touchAction1.press(PointOption.point(x*4/10,y*4/10))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.moveTo(PointOption.point(x*2/10,y*2/10)).release();
//第二根手指从B到A点滑动,放大,从小到上
touchAction2.press(PointOption.point(x*6/10,y*6/10))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.moveTo(PointOption.point(x*2/10,y*2/10)).release();
//把2手指的动作添加到MultiTouchAction里面
multiTouchAction.add(touchAction1).add(touchAction2);
multiTouchAction.perform();
}
@AfterTest
public void teardown() {
androidDriver.quit();
}
}
}
这里举例是2手指向外滑动,放大的脚本
如果是缩小的动作,就是把x和y的数值交换一下