java - Selenium TouchEvent 在设备模拟中不起作用

标签 java selenium

需要在桌面浏览器上使用移动设备模拟来自动化测试。

如果通过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/

相关文章:

java - 如何在 Eclipse 中配置代码格式化程序以在枚举中的每个元素之后添加新行?

java - 使用JSch Java分离通过PTY执行的命令(sudo)的标准输出和错误输出

java - 替换字符串中的单词,同时保留原始单词的大小写

java - Webdriver - HTTP 身份验证对话框

linux - WebDriverException : Message: 'chromedriver.exe' executable needs to be in PATH. - Selenium

java - HTTP POST查询如何计算内容长度

java - 使用 jar 中的@entity 时,Spring Data Jpa Repository 不工作

python - 使用 selenium Web 驱动器 python 单击 href=javascript 时出现问题

ruby-on-rails - Rails + RSpec + 在运行 RSpec 测试套件时设置测试用例文件的顺序

selenium - 如何验证复选框在选定和未选定的情况下 xpath 是否相同