【问题标题】:Emulating mobile in ChromeDriver在 ChromeDriver 中模拟移动设备
【发布时间】:2014-08-19 22:41:18
【问题描述】:

如果您将 WebDriver 与 Chrome 一起使用(通过 Chromedriver),您可能希望模拟移动视口特性。同样,您可能希望在桌面上自动执行测试,而无需在 Android 上使用正确的 Chrome 设置。

你是怎么做到的?

【问题讨论】:

    标签: mobile webdriver selenium-chromedriver


    【解决方案1】:

    mobile_emulation 功能已在 2.11 中添加到 ChromeDriver

    完整文档:https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

    我的笔记如下:

    使用 mobile_emulation 功能选项在 Python 中创建驱动程序:

     driver = self.CreateDriver(
            mobile_emulation = {
                'deviceMetrics': {'width': 360, 'height': 640, 'pixelRatio': 3}})
    

    目前您可以模拟 devicepixelratio、useragent、视口高度和宽度。

    Possible properties 用于 mobile_emulation 字典:

    • deviceName :如果使用,必须是唯一的属性。匹配 Chrome 中的 device preset(例如 'Google Nexus 5')。
    • deviceMetrics:一个字典,可以包含宽度(int)、高度(int)、pixelRatio(double),如上图所示。
    • userAgent:用于欺骗请求标头和导航器对象的字符串。

    【讨论】:

    【解决方案2】:

    这是最新的官方 chromedriver 版本 (2.11)。

    java 中的示例:

    final DesiredCapabilities dc = DesiredCapabilities.chrome();
    dc.setCapability(ChromeOptions.CAPABILITY, new ChromeOptions() {
    {
        setExperimentalOption("mobileEmulation", new HashMap<String, Object>() {
                {
                    put("deviceName", "Google Nexus 5");
                }
            });
        }
    });
    
    ChromeDriver driver = new ChromeDriver(dc);
    

    【讨论】:

      【解决方案3】:

      mobileEmulation 选项在上一个 ChromeDriver 版本 (v 2.11) 中实现。使用 WebDriverJs,您必须将其作为属性添加到功能对象。

      var webdriver = require('selenium-webdriver');
      var capabilities = {
        browserName: 'chrome',
        chromeOptions: {
          mobileEmulation: {
            deviceName: 'Apple iPhone 5'
          }
        }
      };
      var
        driver = new webdriver
        .Builder()
        .withCapabilities(capabilities)
        .build();
      
      
      driver.get('http://google.com');
      
      var bool = false;
      setTimeout(function () {
        bool = true;
      }, 9000);
      driver.wait(function() {
       return bool;
      }, 10000);
      
      driver.quit();
      

      【讨论】:

      • 谢谢!这很有用,因为官方文档不提供 webdriver JS 的信息
      猜你喜欢
      • 2020-07-10
      • 2010-09-25
      • 2014-12-08
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多