如何使用 ChromeDriver 使用 Selenium 测试手势?
How to test gestures with Selenium using ChromeDriver?
基于移动仿真
文档 和 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
上启用触摸手势的信息。
问题是ChromeDriver
默认没有TouchScreen
。
我们需要创建一个新的 class 来支持它,只需要实现 IHasTouchScreen
接口,用 RemoteTouchScreen
class.
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();
}
}
}
基于移动仿真
文档 和 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
上启用触摸手势的信息。
问题是ChromeDriver
默认没有TouchScreen
。
我们需要创建一个新的 class 来支持它,只需要实现 IHasTouchScreen
接口,用 RemoteTouchScreen
class.
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();
}
}
}