目前我正在网络视图中加载图像。我希望能够点击图像,但当我这样做时,它似乎并没有真正在 Sauce Lab 中注册。
IOSDriver driver = (IOSDriver) contextObj.getValue("driver");
final JSONObject params = new JSONObject(parameters);
Set<String> contextHandles = driver.getContextHandles();
ArrayList<String> contexts = new ArrayList<String>();
for (String context : contextHandles) {
contexts.add(context);
System.out.println("Available Context: " + context);
}
String newContext = contexts.get(1);
driver.context(newContext);
seleniumSteps.driverWait("15000");
System.out.println(driver.getPageSource());
WebElement firstDivElement = driver.findElement(By.xpath("(//div)[1]"));
Point imageLocation = firstDivElement.getLocation();
Dimension size = firstDivElement.getSize();
int elementCenterX = imageLocation.getX() + Math.round(size.getWidth() / 2);
int elementCenterY = imageLocation.getY() + Math.round(size.getHeight() / 2);
System.out.println("X Coordinate: " + elementCenterX);
System.out.println("Y Coordinate: " + elementCenterY);
driver.context("NATIVE_APP");
seleniumSteps.driverWait("5000");
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence tap = new Sequence(finger, 1);
tap.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(), elementCenterX, elementCenterY));
tap.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
tap.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(tap));
我尝试使用已弃用的 touchActions 类并获得相同的结果。
经过大量挖掘,我发现您需要在启动时启用一项功能以允许点击发生。
这对我有用:
caps.setCapability("nativeWebTap",true);