如何优化 nightwatch.json 文件?

How to optimize a nightwatch.json file?

我正在使用 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
    }
}

这是定义配置文件的经典方式。 正如您所注意到的,每个平台中都有很多冗余信息:localIdentifieruserNameuserkey ...等等

我的问题:有没有优化配置文件的方法?所以当我想改变我的 userKeybrowserstack.debug 时,我只在一个地方改变它并避免错误?

感谢@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

重要

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