如何在RemoteWebDriver中使用Selenium TouchActions [英] How to use Selenium TouchActions with a RemoteWebDriver
问题描述
我用touchstart
和touchmove
事件编写了一些Javascript代码.我想用硒测试.我刚刚发现了 TouchActions 类与move
方法似乎正是我想要的.
I wrote some Javascript code with the touchstart
and touchmove
event. I want to test it using Selenium. I just discovered the TouchActions class with the move
method which appears to be exactly what I want.
我的测试使用 RemoteWebDriver :
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
驱动程序将是ChromeDriver
或最终是FirefoxDriver
,而不是AndroidDriver
.
The driver will be a ChromeDriver
or eventually a FirefoxDriver
, not an AndroidDriver
.
当我尝试使用以下方法初始化动作时:
When I try to initialize the actions with:
TouchActions builder = new TouchActions(remoteWebDriver);
我遇到强制转换错误:
java.lang.ClassCastException:org.openqa.selenium.remote.RemoteWebDriver无法转换为org.openqa.selenium.interactions.HasTouchScreen
java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
有人知道我应该做什么吗?我需要添加一项功能吗?
Does anybody know what I am supposed to do? Is there a capability I need to add?
推荐答案
因此,要做到这一点,首先需要将移动功能添加到驱动程序中(请参阅
So, to be able to do that, one needs to first add the mobile capability to the driver (see 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);
然后,当您需要触摸操作时,就需要增强"驱动程序,以便对其进行投射:
Then at the moment you need the touch actions, you need to "augment" the driver, to be able to cast it:
TouchActions builder = new TouchActions(new Augmenter().augment(remoteWebDriver));
然后您可以从该生成器执行builder.down(), move(), scroll(), up()...
所需的任何操作.
Then from that builder you can do builder.down(), move(), scroll(), up()...
whatever you need.
这篇关于如何在RemoteWebDriver中使用Selenium TouchActions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!