Nightwatch.js with Disposable Email

Registration and Login automation using Nightwatch.js with Disposable Email


Overview

The greater part of the web application takes a unique email ID for Registration and sends a verification link on the email ID to continue utilizing the application. Likewise, to reset the password using forgot password link also send to the registered email Id where the user clicks on that link to reset the password.

The QA teams leave such tasks for manual testing instead of writing automation tests for these scenarios. But, all communication from the email can be automated by the Mail7.io service. This article will describe how to use Mail7.io service within the automation suite using Nightwatch.js.

Use Cases (E-to-E test cases)

  • Registration

  • Reset Password

    In case, QA wants to perform the forgot password operation then they can get the reset password link on that user email's public inbox.

How to Use?

We can use the Mail7.io service for disposable email. For this, we need to take any email Id ending with @mail7.io e.g. [email protected].

To check the email sent to this email ID, navigate to https://console.mail7.io/admin/inbox/inbox?username=test. Here, You will see all the emails.

Once we get the emails, we can use it as we want.

How to automate using Nightwatch.js

Nightwatch.js is an integrated, easy to use End-to-End testing solution for web applications and websites, written in Node.js. It uses the W3C WebDriver API to drive browsers to perform commands and assertions on DOM elements.

By traversing in Mail7.io

module.exports = {
    tags: ['mail7'],
    '@disabled': false,
    'Get the verification mail on mail7': function (client) {
        // Register user code will be added here
        // e.g.User's email: [email protected]
        client.url('https://mail7.io')
        client.pause(1000);
        client.expect.element('#username').to.be.present;
        client.setValue('#username', ['test', client.Keys.ENTER]);
        // wait for element of next page
        client.waitForElementVisible('#PageTitle > div > div > button > i', 10000);
        // Open the first mail
        client.click('#depublicemailinbox > li:nth-child(1) > div.mail-col.mail-col-1 > p');
        // Add the selector of element which you want to get
        client.getText('<selector>', function (response) {
            console.log(response.value); // outputs the current url
            client.url(response.value);
            client.end();
        });
    }
};

Get Inbox by navigating to URL

 module.exports = {
        tags: ['mail7'],
        '@disabled': false,
        'Get the verification mail on mail7': function (client) {
            // Register user code will be added here
            // e.g.User's email: [email protected]
            client.url('https://mail7.io')
            client.pause(1000);
            client.expect.element('#username').to.be.present;
            client.setValue('#username', ['test', client.Keys.ENTER]);
            // wait for elemet of next page
            client.waitForElementVisible('#PageTitle > div > div > button > i', 10000);
            // Open the first mail
            client.click('#depublicemailinbox > li:nth-child(1) > div.mail-col.mail-col-1 > p');
            // Add the selector of element which you want to get
            client.getText('<selector>', function (response) {
                console.log(response.value); // outputs the current url
                client.url(response.value);
                client.end();
            });
        }
    };

Get Inbox by Mail7.io service API

var request = require('request');
module.exports = function (a, b, e, cb) {
    /**
     * @param: 'a' {String} key: mail7 key
     *
     * @param: 'b' {String} key: mail7 secret
     *
     * @param: 'e' {String} email: email([email protected] or zyx), you want get data from...
     *
     * @param: 'cb' {function} callback: a callback function to handle response
     *  mail7 key and secret will be available with pro version
     */

    e = e.indexOf('@mail7') > -1 ? e.replace('@mail7.io', '') : e;
    request('https://api.mail7.io/inbox?apikey=' + a + '&apisecret=' + b + '&to=' + e, function (err, res) {
        var output = {};
        if (!err) {
            var body = JSON.parse(res.body);
            var subjects = [];
            if (body.status == 'success') {
                if (body.data.length > 0) {
                    for (var i in body.data) {
                        subjects.push(body.data[i].mail_source.subject);
                    }
                    var html = body.data[0].mail_source.html;
                    output.status = 'success';
                    output.subjects = subjects; //array of all subjects
                    output.state = 0;
                    output.text = html.replace(/<\/?[^>]+(>|$)/g, " "); // simple text body
                }
                else {
                    output.status = 'error';
                    output.state = -1;
                    output.message = "mail_source is not found for this username...";
                    output.body = res.body;
                }
            }
            else {
                output.status = 'error';
                output.state = -1;
                output.message = body.message;
            }
        }
        else {
            output.status = 'error';
            output.state = -1;
            output.body = err;
        }
        cb(output)
    })
};

Conclusion

By using the Mail7.io service, you can verify all mail communication using Nightwatch.js within the browser itself. Mail7.io also provides API to test the same functionality without launching it in the browser. but there are some limitations on the free account.