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

问题描述 投票:0回答:1

基于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上启用触摸手势的信息。

c# selenium selenium-webdriver selenium-chromedriver gesture
1个回答
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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.