TimDoesAutomatedQA
TimDoesAutomatedQA

Reputation: 41

Confused on how to specify a Platform and Version while using ChromeOptions for RemoteWebDriver with Selenium donNet since recent updates

Using c# with Selenium 2.39 and before, you were able to interact with ChromeOptions and DesiredCapabilities directly. So, if you wanted to emulate an android device on a modern version of Windows, your code snippet might look like this:

        // Define the ChromeOptions to make Chrome act like a mobile device
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--user-agent=Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
        options.AddArgument("--disable-sync-passwords");

        Capabilities = DesiredCapabilities.Chrome();

        //Needed to find Win7 VM rather than Linux or Mac
        Capabilities.SetCapability("platform", "VISTA");  

        //CEF apps are also tested using Selenium2 and Grid.  
        //A version of "real" has been created in my Grid config to ensure 
        //I target current Chrome and not CEF.
        Capabilities.SetCapability("version", "real");  

        Capabilities.SetCapability(ChromeOptions.Capability, options);

        //Get a new RemoteWebDriver that thinks it is an Android device
        Driver = new RemoteWebDriver(new Uri(Settings.RemoteWebDriverSettings.GridLocation), Capabilities);

        //Resize for mobile
        Driver.Manage().Window.Position = new System.Drawing.Point(0, 0);
        Driver.Manage().Window.Size = new System.Drawing.Size(400, 680);

However, In Selenium 2.40 and above, there has been a change made that breaks this approach for .net. The best explanation of what changed that I can find is from this exchange here that states:

The .NET bindings are moving toward a pattern where DesiredCapabilites should not be used directly, even with RemoteWebDriver. To facilitate that, the ChromeOptions class has a ToCapabilities() method.

Try as I might, I cannot find a good example of how to set a targeted Platform and Version while using ChromeOptions, anymore.

At some point, I'd like to take advantage of the new MobileEmulation that was introduced with recent versions of ChromeDriver, but I have to get over this hurdle, first.

Any help would be greatly appreciated.

Upvotes: 2

Views: 4313

Answers (1)

TimDoesAutomatedQA
TimDoesAutomatedQA

Reputation: 41

Got an answer back from a Selenium project member that can be seen here.

My snippet from above would become something like this, instead:

    // Define the ChromeOptions to make Chrome act like a mobile device
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--user-agent=Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
    options.AddArgument("--disable-sync-passwords");

    //You can cast the ICapabilities object returned by ToCapabilities() as DesiredCapabilities
    capabilities = options.ToCapabilities() as DesiredCapabilities;

    //Needed to find Win7 VM rather than Linux or Mac
    capabilities.SetCapability("platform", "VISTA");  

    //CEF apps are also tested using Selenium2 and Grid.  
    //A version of "real" has been created in my Grid config to ensure 
    //I target current Chrome and not CEF.
    capabilities.SetCapability("version", "real");  

    //Get a new RemoteWebDriver that thinks it is an Android device
    Driver = new RemoteWebDriver(new Uri(Settings.RemoteWebDriverSettings.GridLocation), capabilities);

    //Resize for mobile
    Driver.Manage().Window.Position = new System.Drawing.Point(0, 0);
    Driver.Manage().Window.Size = new System.Drawing.Size(400, 680);

Upvotes: 2

Related Questions