appium :java-client8.5.1
本人单屏幕滑动代码如:
//获取手机屏幕尺寸
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();
Actions finger = new Actions(driver).setActivePointer(Kind.TOUCH, "finger");
finger.moveByOffset(width/4, height/2)
.clickAndHold().pause(Duration.ofMillis(2000))
.moveByOffset(width3/4, height/2).release()
.perform();
运用以上代码,报错如下:action"finger" should be preceded with at least one item containing absolute coordinates
求解 求解 求解
在Java-Client 8.5.1中,TouchAction
类已被弃用,官方推荐使用W3C Actions类进行操作。以下是使用W3C Actions类进行单屏幕滑动的示例代码:
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
// 获取手机屏幕尺寸
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence swipe = new Sequence(finger, 0);
swipe.addAction(finger.createPointerMove(Duration.ofMillis(0),
PointerInput.Origin.viewport(), width / 4, height / 2));
swipe.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
swipe.addAction(finger.createPointerMove(Duration.ofMillis(2000),
PointerInput.Origin.viewport(), width * 3 / 4, height / 2));
swipe.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(swipe));
在这个示例中,我们首先创建一个PointerInput
对象,指定其类型为TOUCH
,名称为"finger"。然后,我们创建了一个Sequence
对象,用于定义一系列的操作。通过addAction
方法,我们依次添加了滑动的起始位置、按下、滑动到结束位置以及释放的操作。最后,我们将Sequence
对象传递给perform
方法执行。
感谢