根据Mobile Emulation
documentation和TouchActions Class,我提出了以下代码,但我得到了例外:The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen.
namespace Example
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
[TestClass]
public class ExampleTest
{
IWebDriver chromeDriver;
[TestInitialize]
public void Init()
{
var chromeOptions = new ChromeOptions();
chromeOptions.EnableMobileEmulation("Nexus 6P"); // Allows the Chrome browser to emulate a mobile device.
chromeDriver = new ChromeDriver(chromeOptions);
}
[TestMethod]
public void ExampleTestMethod()
{
chromeDriver.Navigate().GoToUrl("https://example.com");
IWebElement link = chromeDriver.FindElement(By.CssSelector("a"));
// Threw exception:
// The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen.
var touchActions = new OpenQA.Selenium.Interactions.TouchActions(chromeDriver);
touchActions
.SingleTap(link)
.Build()
.Perform();
}
[TestCleanup]
public void Cleanup()
{
chromeDriver.Quit();
}
}
}
我查看了文档,但没有关于如何在ChromeDriver
启用触摸手势的信息。
答案 0 :(得分:0)
问题是ChromeDriver
默认没有TouchScreen
我们需要创建一个新类来支持它,只需要用IHasTouchScreen
类实现RemoteTouchScreen
接口。
namespace Example
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
public class ChromeDriverWithTouchScreen : ChromeDriver, IHasTouchScreen
{
public ITouchScreen TouchScreen => new RemoteTouchScreen(this);
public ChromeDriverWithTouchScreen(ChromeOptions options) : base(options)
{
}
}
[TestClass]
public class ExampleTest
{
IWebDriver chromeDriver;
[TestInitialize]
public void Init()
{
var chromeOptions = new ChromeOptions();
chromeOptions.EnableMobileEmulation("Nexus 6P"); // Allows the Chrome browser to emulate a mobile device.
chromeDriver = new ChromeDriverWithTouchScreen(chromeOptions);
}
[TestMethod]
public void ExampleTestMethod()
{
chromeDriver.Navigate().GoToUrl("https://example.com");
IWebElement link = chromeDriver.FindElement(By.CssSelector("a"));
// Threw exception:
// The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen.
var touchActions = new OpenQA.Selenium.Interactions.TouchActions(chromeDriver);
touchActions
.SingleTap(link)
.Build()
.Perform();
}
[TestCleanup]
public void Cleanup()
{
chromeDriver.Quit();
}
}
}