LMP
LMP

Reputation: 53

Perform Click on Selenium ChromeEmulation

I'm working i'm running Selenium's Chromedriver with MobileEmulation enabled. I enabled the it with the following code:

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);

It worked fine except i can't perform the .click() function. So i tried implementing the TouchActions in this function:

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);
    }
}

This codes gives (ofcourse) an error because the chromedriver doesn't implement the IHasTouchscreen. "Additional information: The IWebDriver object must implement or wrap a driver that implements IHasTouchScreen."

The driver must remain an IWebDriver because of he rest of the application. Does anyone know a way to either perform the "click/touch/tap" function or enable the TouchActions into this function with the given datatypes?

Upvotes: 2

Views: 633

Answers (1)

Apalabrados
Apalabrados

Reputation: 1146

Create a class like this one:

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

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

        }
}

Upvotes: 1

Related Questions