需要在桌面浏览器上使用移动设备模拟来自动化测试。
如果通过ChromeOptions()启用模拟,则触摸屏的模拟不起作用(按钮响应光标等)。这就是为什么我使用 java.awt.Robot (F12 > Ctrl + Shift + M > iPad) 来打开设备模拟。但触摸不起作用。
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.touch.TouchActions;
//Set browser type and platform
DriverSettings.newSettings("https://staging.myev.com", DriverType.CHROME, EmulationType.MOBILE);
WebDriver driver = DriverSettings.getDriver();
driver.get("https://google.com");
WebElement element = driver.findElement(By.xpath("//a[@class='gb_x gb_Ub']"));
TouchActions touchActions = new TouchActions(driver);
touchActions.singleTap(element).perform();
未按下按钮且没有错误 - “进程已完成,退出代码为 0”
也许还有另一种方法来启用移动模拟或使用 TouchActions?
最佳答案
根据官网规范:http://chromedriver.chromium.org/mobile-emulation ,要正确使用 ChromeDriver 的移动模拟,您应该使用:
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions)
*模拟设备也可以自定义,为此,您需要使用存储的自定义设备属性声明自定义 session 存储。之所以需要这样做,是因为如果您没有专门声明 chrome session 存储,则每次启动新的测试 session 时,都会创建一个具有默认设置的临时 session 。
在这里您将找到启动 chromedriver 的有用命令列表: https://peter.sh/experiments/chromium-command-line-switches/#user-data-dir
关于java - Selenium TouchEvent 在设备模拟中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238330/