How to optimize a nightwatch.json file?

1k views Asked by At

I'm writing tests for Browserstack using nightwatchJS.

My nightwatch.json looks like that:

{
    "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
    }
}

It's the classic way to define a configuration file. As you can remark there are a lot of redundant information in each platform: localIdentifier, userName, userkey...etc

My question: Is there a way to optimize the configuration file? So when I want for example to change my userKey or browserstack.debug, I change it only in one place and avoid mistakes?

1

There are 1 answers

0
Ala Eddine JEBALI On BEST ANSWER

Thanks to the comment of @Mukesh-Tiwari I was able to optimize my nightwatch.json file. I'm sharing the idea:

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];
  }
}

In the example above, I'm defining selenium_host, selenium_port at 1 place so if I need to change them I just touch only 1 line of code.

Thanks again for Mukesh-Tiwari.

Imprtant

To make it works, you need to change your configuration file from nightwatch.json to nightwatch.js because we are using a JS code.