Menu

Custom Nightwatch.js API

Browse the custom Nightwatch.js commands and assertions that were created specifically to use with Adaptive.js technology.

Official Nightwatch.js API Go to the official Nightwatch.js API to browse the original set of commands.

Custom Commands Custom Assertions

Custom Commands

The Mobify custom commands for Nightwatch.js are convenience methods that perform various operations on the page. A command usually incorporates two or more Selenium protocol actions.

get(url, callback)

The get command combines the url and waitUntilMobified functions. This command uses the browser.execute function to run code within the client browser. It then waits for the Mobify object to be present on the page.

Input
Parameter Name Parameter Type Description
url String The URL to load
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.get('http://www.test.com');
};

getMobifyEvaluatedData(callback)

The getMobifyEvaluatedData command uses the waitForCondition method to retrieve the Mobify.evaluatedData from the client browser.

Input
Parameter Name Parameter Type Description
callback Function optional A function to call after the current command finishes execution.

Output
Parameter Type Description
Object Returns the client object after waitUntilMobified executes on it with the specified parameters.

Code Sample:

this.demoTest = function (browser) {
    browser.getMobifyEvaluatedData();
};

    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-log">log(message, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The log command prints a message to the console. Use this command to add messages to test output.

Input
Parameter Name Parameter Type Description
message String The message to log on the console.
callback Function optional A function to call after the current command finishes execution.

Output
Parameter Type Description
Object Returns the client object after waitForContition runs on it.

Code Sample:

this.demoTest = function (browser) {
    browser.log('Testing submitting form');
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-navigate">navigate(selector, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The navigate command initiates a click command on the supplied selector link, navigates to the URL, and then it initiates the waitUntilMobified function before it continues the chain of tests.

Input
Parameter Name Parameter Type Description
selector String The CSS selector to click on to navigate to the new URL.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.navigate('.myLink');
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-preview">preview(url, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The preview command uses http://preview.mobify.com to open a website to preview a given bundle. The bundle and the base URL need to be set in the tests/system/site.json file. Note that if the "production" flag is set in site.json, the bundle URL will be ignored. Pass in an optional URL as an argument to this command. Upon completion, waitUntilMobified is called to ensure that the mobile site adaptation is complete.

Input
Parameter Name Parameter Type Description
url String optional The URL to preview.
callback Function optional A function to call after the current command finishes execution.

Code Samples:

this.demoTest = function (browser) {
    browser.preview();
};

or with a URL

this.demoTest = function (browser) {
    browser.preview('http://my-awesome-project.com');
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-trigger">trigger(selector, type, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The trigger command simulates a specified event type on the supplied DOM element specified by the selector parameter.

Input
Parameter Name Parameter Type Description
selector String The CSS/Xpath selector to locate the element.
type String The specified event type, for example click in the enabled JSON Wire Protocols.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.trigger('.myLink', click);
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-triggerTouch">triggerTouch(selector, type, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The triggerTouch command simulates a specified touch type event on the supplied DOM element. Use this command when Selenium's click does not register.

Input
Parameter Name Parameter Type Description
selector String The CSS/Xpath selector to locate the element.
type String The specified event type, for example click in the enabled JSON Wire Protocols.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.triggerTouch('.myLink', click);
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-waitForAjaxCompleted">waitForAjaxCompleted(callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The waitForAjaxCompleted command uses the waitForCondition function to execute code within the client browser. The command checks the value of jQuery.active to ensure that the number of active connections is 0.

Input
Parameter Name Parameter Type Description
callback Function optional A function to call after the current command finishes execution.

Output
Parameter Type Description
Object Returns the client object.

Code Sample:

this.demoTest = function (browser) {
    browser.waitForAjaxCompleted();
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-waitForAnimation">waitForAnimation(milliSeconds, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The waitForAnimation command suspends the test for the given time in milliseconds while it waits for animation to complete.

Input
Parameter Name Parameter Type Description
milliSeconds Number The number of millliseconds to wait.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.waitForAnimation();
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-waitForCondition">waitForCondition(condition, milliSeconds, timeout, message, callback)</a>

The waitForCondition command receives a condition to check for, waits for a maximum time before timing out, and polls at a specified time interval. The condition returns either as a success or a timeout.

Input
Parameter Name Parameter Type Description
condition Function The condition to check against.
milliSeconds Number optional The number of milliseconds to poll before timeout.
timeout Number optional The number of milliseconds between each poll.
message String optional The message to output.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    return browser.waitForCondition('return $.active;', 8000, function(result) {
        if (typeof callback === 'function') {
           callback.call(browser, result);
        }
    });
};


    <table border="0" width="90%" align="center">
        <tr><td><b><h3><a id="commands-waitUntilMobified">waitUntilMobified(milliSeconds, callback)</a></h3></b></td></tr>
        <tr><td></td></tr>
        <tr><td>
            <p></p>

The waitUntilMobified command will use the waitForCondition command to poll for the Mobify or Adaptive object on the page to ensure that the adaptation is complete. Use this command to browse to a page or if the page reloads.

Input
Parameter Name Parameter Type Description
milliSeconds Number optional The number of milliseconds to poll before timeout.
If not specified, the default timeout is 10,000 milliseconds.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.waitUntilMobified();
};

Custom Assertions

elementsCount(selector, expected, message, callback)

The elementsCount assertion checks if the given selector is present the number of times that is expected for that selector to appear.

Input
Parameter Name Parameter Type Description
selector String The CSS/Xpath selector to locate the element.
expected Number The expected number of times for the attribute to appear.
message String optional The message to output.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.assert.elementsCount('#x-root', 1);
};


        <table border="0" width="90%" align="center">
            <tr><td><b><h3><a id="assertions-elementsPresent">elementsPresent</a></h3></b></td></tr>
            <tr><td></td></tr>
            <tr><td>
                <p></p>

The elementsPresent assertion checks if the given selectors are present. It returns a list of the missing selectors.

Input
Parameter Name Parameter Type Description
selector String The CSS/Xpath selector to locate the element.
message String optional The message to output.
callback Function optional A function to call after the current command finishes execution.

<!-- table border="1" width="90%" align="center">
Output
Parameter Name Parameter Type Description
missingSelectors Array Outputs an array of strings. Each string is a missing selector.

Code Sample:

this.demoTest = function (browser) {
    browser.assert.elementsPresent('#x-root', '#x-header', '#x-footer');
};


        <table border="0" width="90%" align="center">
            <tr><td><b><h3><a id="assertions-elementsVisible">elementsVisible(selectors, callback)</a></h3></b></td></tr>
            <tr><td></td></tr>
            <tr><td>
                <p></p>

The elementsVisible assertion checks if one or more selectors are visible. It returns a list of one or more selectors that are not visible on the page.

Input
Parameter Name Parameter Type Description
selectors String The CSS/Xpath selector to locate the element.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (browser) {
    browser.assert.elementsVisible('#x-root', '#x-head');
};


        <table border="0" width="90%" align="center">
            <tr><td><b><h3><a id="assertions-templateName">templateName(expected, message, callback)</a>

The templateName assertion checks if the given template name is correct.

Input
Parameter Name Parameter Type Description
expected String The expected value of the attribute to check.
message String optional A log message to display in the output.
If the parameter is not specified, a default message is displayed by default.
callback Function optional A function to call after the current command finishes execution.

Code Sample:

this.demoTest = function (client) {
    browser.assert.templateName('home');
};

    </td>
</tr>

Edit in GitHub