如何在启动 webdriver 实例之前通过命令行在 conf.js 中传递 deviceName

how to pass deviceName in conf.js through command line before starting webdriver instance

目前我正在尝试在移动设备(例如 Apple iPad\Samsung Galaxy)上模拟 chrome 浏览器。当在 conf.js 中传递硬编码的设备名称时,它工作正常。

但我想即时更换设备。我试过在命令行中传递 deivceName 参数,但没有成功。它没有更新 conf.js 中的值,但在启动 webdriver 实例后,我看到更新的参数值:

protractor mobiledevice.js --params.device="Apple iPhone 5"

-

"use strict";

var config = require('./conf.js').config;

config.params = {
    device: 'Google Nexus 6'
}


config.capabilities = {
    'chromeOptions': {
        'mobileEmulation': {
            'deviceName': config.params.device
        }
    }
};

exports.config = config;

问题:如何模拟通过命令行作为参数传递的设备?

您可以定义 getMultiCapabilities function:

exports.config = {
    getMultiCapabilities: function () {
        return [{
            chromeOptions: {
                mobileEmulation: {
                    deviceName: this.params.device
                }
            }
        }];
    },

    // ...
};

然后传递device参数:

protractor mobiledevice.js --params.device="Apple iPhone 5"