Script Examples

Script Examples #

Locating elements by using CSS selectors #

client.useCss();

This command sets element locator strategy to CSS. CSS is also the default strategy used by Nightwatch.

function(client) {
    // Example of locating elements using CSS selectors
    client
        // Navigate to website google.com
        .url('https://www.google.com')
        // Wait up to 10 seconds until 'body' element is visible)
        .waitForElementVisible('body', 10 * 1000)
        // Type "Loadero" in the search bar
        .setValue('input[type=text]', 'Loadero')
         // Trigger search by sending "Enter" key event in the search bar
        .setValue('input[type=text]', client.Keys.ENTER);
}

Locating elements by using xPath selectors #

client.useXpath();

Sets the locate strategy for selectors to XPath. It is possible to use both XPath and CSS strategies by switching back and forth.

function(client) {
    // Example of locating elements using xPath selectors
    client
        .useXpath()
        // Navigate to Google website
        .url('https://www.google.com')
        // Wait up to 10 seconds until '//body' element is visible
        .waitForElementVisible('//body', 10 * 1000)
        // Type "charizard evolution chart" in the search bar
        .setValue('//input[@type="text"]', 'charizard evolution chart')
         // Trigger search by sending 'Enter' key event in the search bar
        .setValue('//input[@type="text"]', client.Keys.ENTER);
}

Using Nightwatch assertions #

Nightwatch native assertions are called by two methods, depending on the expected result after error is asserted: .assert and .verify

Once this assertion fails, test is ended and any assertions further in the script are skipped:

client.assert.visible(".non-existing-element-class");

This assertion will log the failure and script execution continues on.

client.verify.visible(".non-existing-element-class");
function(client) {
    // Example of using Nightwatch assertions
    client
        .url('https://www.testdevlab.com')
        .waitForElementVisible('body', 10 * 1000)
        // Check if the page url contains given value, otherwise stop the test
        .assert.urlEquals('https://www.testdevlab.com/')
        // Verify if the page title contains given value, otherwise log error
        .verify.title(
            'TestDevLab - We are experts in Software Quality Assurance'
        )
        // Check if the "Company" element is visible
        // Wait up to defined timeout for "Company" element to be visible
        .waitForElementVisible('.sections div:nth-child(4)', 10 * 1000)
        .click('.sections div:nth-child(4)')
        // Check if the element is visible
        // Wait up to defined timeout for "Careers" to be visible
        .waitForElementVisible('a[href^="/careers"]', 10 * 1000)
        .click('a[href^="/careers"]')
        .moveToElement('.container .job-position-intro:nth-child(3)', 0, 0)
        // Check if career position is visible, otherwise log error
        .verify.visible('.container .job-position-intro:nth-child(3)')
        .takeScreenshot('Position.png');
}