Nightwatch – automatic website testing using Selenium server

Nightwatch – automatic website testing using Selenium server

About front-end website testing

Website integration tests are extremely useful for front-end development. Automatic website testing makes you sure that your website works properly, that you haven’t broken something working on recent updates. I write front-end integration tests using Selenium technology and Nightwatch (written in NodeJS) or JUnit (written in Java).

Setup Nightwatch
npm install -g nightwatch
Run Selenium Server

Download selenium server and run it from command line:

java -jar selenium-server-standalone-3.0.1.jar
Selenium server started
Selenium server started

Verify that the selenium server runs using the command

netstat -na
Selenium server listen 4444 port
Selenium server listens 4444 port
Configure

Go to your website project directory. Create folders there:

./tests
./tests/reports/
./tests/screenshots/

Then in project root directory place nightwatch.json file with this content:

{
  "src_folders" : ["tests"],
  "output_folder" : "./tests/reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "",
  "globals_path" : "",

  "selenium" : {
    "start_process" : false,
    "server_path" : "",
    "log_path" : "",
    "host" : "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "",
      "webdriver.ie.driver" : ""
    }
  },
  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {  <---- this option enables screenshot savin when error occurs
        "enabled" : true,
        "path" : "./tests/screenshots",
        "on_failure" : true,
        "on_error" : true
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }
  }
}

Create tests/ directory in the project root folder.

Create tests/main.js file:

module.exports = {
    'Checking page default layout' : function (browser) {
        
        browser
            .url('http://url-to-your-project-dev-site')
            .waitForElementVisible('body', 1000);
        
        browser
            .useXpath()
            .getValue('xpath','//*[@id="sale"]/div/input[1]', function(result) {
                this.assert.equal(result.value,'1510');
            })
            .elements('xpath','//*[@id="sale"]/descendant::div[@class="row"]', function(result) {
                this.assert.equal(result.value.length,1);
            })
            .end();
    }
};
Run tests from command line
nightwatch

 

Author: vlad