如何使用ChromeDriver使用Selenium测试手势?

时间:2018-03-06 16:23:00

标签: c# selenium selenium-webdriver selenium-chromedriver gesture

根据Mobile Emulation documentationTouchActions 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启用触摸手势的信息。

1 个答案:

答案 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();
        }
    }
}