执行点击Selenium ChromeEmulation

时间:2017-12-21 15:49:47

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

我正在工作,我正在运行Selenium的Chromedriver并启用了MobileEmulation。我使用以下代码启用了它:

ChromeMobileEmulationDeviceSettings deviceSettings = new ChromeMobileEmulationDeviceSettings();
    deviceSettings.Width = 360;
    deviceSettings.Height = 640;
    deviceSettings.PixelRatio = 3;
    deviceSettings.UserAgent = "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
    deviceSettings.EnableTouchEvents = true;

    DesiredCapabilities capabilities = DesiredCapabilities.Chrome();

    ChromeOptions options = new ChromeOptions();
    options.AddArguments("incognito");
    options.EnableMobileEmulation(deviceSettings);
    driver = new ChromeDriver(options);

除非我无法执行.click()函数,否则它工作正常。所以我尝试在这个函数中实现TouchActions:

ElementFinder search = new ElementFinder();
public void mobileClick(IWebDriver driver, string type, string value)
{
   // IPerformsTouchActions mdriver = (IPerformsTouchActions) driver;
    IWebElement element = search.getElement(driver, type, value);
    if (element != null)
    {       
        TouchActions TA = new TouchActions(driver);
        TA.SingleTap(element);
        Speak.elementIsClicked(value);
    }
}

此代码给出(ofcourse)错误,因为chromedriver没有实现IHasTouchscreen。 “附加信息:IWebDriver对象必须实现或包装实现IHasTouchScreen的驱动程序。”

驱动程序必须保留为IWebDriver,因为他应用程序的其余部分。 有没有人知道如何使用给定的数据类型执行“单击/触摸/点击”功能或启用TouchActions到此功能?

1 个答案:

答案 0 :(得分:0)

创建一个像这样的类:

public class ChromeDriverMobile : ChromeDriver, IHasTouchScreen
{
        public ITouchScreen TouchScreen => new RemoteTouchScreen(this);

        public ChromeDriverMobile(Uri uri, ChromeOptions options) : base(uri, options)
        {

        }
}