如何将 Selenium TouchActions 与 RemoteWebDriver 一起使用

How to use Selenium TouchActions with a RemoteWebDriver

我用 touchstarttouchmove 事件编写了一些 Javascript 代码。我想用 Selenium 测试它。我刚刚发现 TouchActions class 和 move 方法似乎正是我想要的。

我的测试是 运行 RemoteWebDriver

RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

driver 将是 ChromeDriver 或最终是 FirefoxDriver,而不是 AndroidDriver

当我尝试初始化操作时:

TouchActions builder = new TouchActions(remoteWebDriver);

我得到一个转换错误:

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen

有人知道我应该做什么吗?我需要添加功能吗?

因此,要做到这一点,首先需要向驱动程序添加移动功能(参见 Mobile Emulation):

Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Galaxy S5"); // Choose a device available in your version of chromimum
Map<String, Object> options = new HashMap<>();
options.put("mobileEmulation", mobileEmulation);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

那么此时你需要触摸动作,你需要"augment"驱动程序,才能施放它:

TouchActions builder = new TouchActions(new Augmenter().augment(remoteWebDriver));

然后从那个构建器你可以做任何你需要的builder.down(), move(), scroll(), up()...