如何优化nightwatch.json文件?

时间:2017-01-09 09:09:48

标签: json nightwatch.js browserstack

我正在使用nightwatchJS为Browserstack编写测试。

我的nightwatch.json看起来像那样:

{
    "src_folders": [
        "..."
    ],
    "tests_output": "test/tests_output/",
    "detailed_output": true,
    "selenium": {
        "start_process": false,
        "host": "hub.browserstack.com",
        "port": 80
    },
    "test_workers": {
        "enabled": true,
        "workers": 2
    },
    "test_settings": {
        "ie7": {
            "selenium_port": 80,
            "selenium_host": "hub.browserstack.com",
            "silent": true,
            "desiredCapabilities": {
                "os": "Windows",
                "os_version": "XP",
                "browserName": "IE",
                "version": "7",
                "resolution": "1024x768",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "browserstack.local": "true",
                "browserstack.video": "true",
                "browserstack.debug": "true",
                "browserstack.localIdentifier": "<localIdentifier>",
                "browserstack.user": "<userName>",
                "browserstack.key": "<userkey>"
            }
        },
        "ie8": {
            "selenium_port": 80,
            "selenium_host": "hub.browserstack.com",
            "silent": true,
            "desiredCapabilities": {
                "os": "Windows",
                "os_version": "7",
                "browserName": "IE",
                "version": "8",
                "resolution": "1024x768",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "browserstack.local": "true",
                "browserstack.video": "true",
                "browserstack.debug": "true",
                "browserstack.localIdentifier": "<localIdentifier>",
                "browserstack.user": "<userName>",
                "browserstack.key": "<userkey>"
            }
        },
        "chrome": {
            "selenium_port": 80,
            "selenium_host": "hub.browserstack.com",
            "silent": true,
            "desiredCapabilities": {
                "os": "Windows",
                "os_version": "10",
                "browserName": "chrome",
                "resolution": "1024x768",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "browserstack.local": "true",
                "browserstack.video": "true",
                "browserstack.debug": "true",
                "browserstack.localIdentifier": "<localIdentifier>",
                "browserstack.user": "<userName>",
                "browserstack.key": "<userkey>"
            }
        },
        //Similar blocks for other platforms
    }
}

这是定义配置文件的经典方法。 您可以注意到,每个平台都有大量冗余信息: localIdentifier userName userkey ......等等

我的问题:有没有办法优化配置文件?因此,当我想要更改我的 userKey browserstack.debug 时,我只在一个地方更改它并避免错误?

1 个答案:

答案 0 :(得分:0)

感谢@ Mukesh-Tiwari的评论,我能够优化我的nightwatch.json文件。我在分享这个想法:

for(var i in nightwatch_config.test_settings){
  var config = nightwatch_config.test_settings[i];
  config['selenium_host'] = nightwatch_config.selenium.host;
  config['selenium_port'] = nightwatch_config.selenium.port;
  config['desiredCapabilities'] = config['desiredCapabilities'] || {};
  for(var j in nightwatch_config.common_capabilities){
    config['desiredCapabilities'][j] = config['desiredCapabilities'][j] || nightwatch_config.common_capabilities[j];
  }
}

在上面的例子中,我在1个地方定义selenium_hostselenium_port,所以如果我需要更改它们,我只需触摸1行代码。

再次感谢 Mukesh-Tiwari

<强> Imprtant

要使其正常工作,您需要将配置文件从nightwatch.json更改为nightwatch.js,因为我们使用的是JS代码。