How to use Selenium TouchActions with a RemoteWebD

2020-05-05 17:27发布

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.

My tests are run with a RemoteWebDriver:

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

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);

I get a cast error:

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?

1条回答
Melony?
2楼-- · 2020-05-05 18:11

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));

Then from that builder you can do builder.down(), move(), scroll(), up()... whatever you need.

查看更多
登录 后发表回答