Skip to content

Instantly share code, notes, and snippets.

@uknick
Created August 7, 2017 10:38
Show Gist options
  • Save uknick/d103ad6444ca2a1113e6343218c3edc3 to your computer and use it in GitHub Desktop.
Save uknick/d103ad6444ca2a1113e6343218c3edc3 to your computer and use it in GitHub Desktop.

JS Automation testing review

Nightwatch

http://nightwatchjs.org/

Requirements

e2e testing using the Selenium web driver. You can also specify other webdrivers for different browsers. Either run tests from 1 browser, or all browsers as defined in the test_settings object, for example, ie,chrome,safari

nightwatch -t nightwatch-test.js -e chrome

or

nightwatch -t nightwatch-test.js -e ie,chrome,firefox

to run the tests in multiple browser environments.

Example of a Nightwatch.json file:

Config

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",

  "selenium" : {
    "start_process" : true,
    "server_path" : "bin/selenium-server-standalone-3.2.0.jar",
    "log_path" : "",
    "port" : 4444,

    "cli_args" : {
      "webdriver.chrome.driver" : "bin/chromedriver.exe",
      "webdriver.ie.driver" : "bin/IEDriverServer.exe",
      "webdriver.gecko.driver" : "bin/geckodriver.exe"
    }
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost/",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent" : true,
      "screenshots" : {
        "enabled" : true,
        "path" : "./screenshots/"
      },
      "desiredCapabilities": {
        "browserName": "internet explorer",
        "elementScrollBehavior": 1,
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },
    "ie" : {
      "desiredCapabilities": {
        "browserName": "internet explorer",
        "elementScrollBehavior": 1,
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },
    "safari" : {
      "desiredCapabilities": {
        "browserName": "safari"
      }
    },
    "chrome" : {
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions": {
          "args":["disable-web-security", "ignore-certificate-errors"]
        }
      }
    },
    "firefox" : {
      "desiredCapabilities": {
        "browserName": "firefox"
      }
    }
  }
}

After running the tests:

  Example Test

module.exports = {

  'Homepage' : function (client) {
    client
      .url('http://test.com')
      .waitForElementVisible('body', 1000)
      .assert.title("My website title")
      
      .click('a[href="/home/"]')
      .pause(500)
      .assert.title("Home")

      .assert.cssClassPresent('nav ul li:nth-child(2)', "active")

      // Get number of elements.
      .elements('css selector', '.case-study', function (elements) {
        this.assert.equal(elements.value.length, 3, 'There should be 3 case-study divs.');
      })

      // Get number of elements.
      .elements('css selector', 'nav ul li', function (elements) {
        this.assert.equal(elements.value.length, 6, 'There should be 6 navigation items');
      })

      .end();
  },

  'Blank Test' : function(client) {
    client
    .end();
  }

};

Run with

nightwatch -t nightwatch-test.js -e chrome

CasperJS

Site: casperjs.org

Wrapper for Phantom JS

Defining a function for testing 1 particular page:
function HomepageTests(test){
  casper.start(URLS.HOME, function() {

    this.test.info('Current location is ' + this.getCurrentUrl());

    test.assertTitle('My Website Title', 'has the correct title');

    test.assertEvalEquals(function () { 
        return $('nav ul li').length 
    }, 6, '6 navigation links');

    test.assertEvalEquals(function () { 
        return $('.case-study').length 
    }, 3, '3 case studies are listed.');

    test.assertEvalEquals(function () { 
        return $('.section-box ').length 
    }, 2, '2 section boxes.');

  });

  casper.run(function() {
    test.done();
  });

}

Then in the same file, the test runner:

/*
--------------------------------------------------
Run the Tests
--------------------------------------------------
*/


casper.test.begin('Testing the homepage', 4, function suite(test) {

    HomepageTests(test);

    casper.run(function() {
        test.done();
    });

    casper.echo(lines);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment