c# - 自最近更新以来,对如何在将 ChromeOptions for RemoteWebDriver 与 Selenium donNet 结合使用时指定平台和版本感到困惑

标签 c# selenium selenium-webdriver selenium-grid

在 Selenium 2.39 及之前的版本中使用 c#,您可以直接与 ChromeOptions 和 DesiredCapability 交互。因此,如果您想在现代版本的 Windows 上模拟 Android 设备,您的代码片段可能如下所示:

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

但是,在 Selenium 2.40 及更高版本中,进行了一项更改,打破了 .net 的这种方法。我能找到的对变化的最好解释来自 this exchange here其中指出:

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.

尽我所能,我再也找不到如何在使用 ChromeOptions 时设置目标平台和版本的好示例。

在某些时候,我想利用最新版本的 ChromeDriver 中引入的新 MobileEmulation,但我必须首先克服这个障碍。

任何帮助将不胜感激。

最佳答案

从 Selenium 项目成员那里得到的答复是 can be seen here .

我上面的代码片段会变成这样:

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

关于c# - 自最近更新以来,对如何在将 ChromeOptions for RemoteWebDriver 与 Selenium donNet 结合使用时指定平台和版本感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589560/

相关文章:

c# - 在 .NET 中逐个节点地比较两个 XML 文件

c# - 您使用或建议使用什么 DAL 策略?

c# - 使用索引将成员添加到 C# List

ruby - (Watir webdriver) 在 OSX 10.9 中使用多显示器时如何将浏览器移动到另一个屏幕

java - 是否有任何注释生成每个步骤从自动化测试执行所需的时间

python - 运行脚本时无法消除 "stale element"错误

c# - Trace.WriteLine 在使用 CSharpCodeProvider 编译的动态加载程序集中不起作用

python - 在 Chrome Web 浏览器的 Selenium Python 代码中向 send_keys 发送 UTF-8 消息

java - Selenium WebDriver 中的自定义隐式等待加载页面消失

python/selenium/chromedriver 超时异常