Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with custom command #74

Closed
thefuriouscoder opened this issue Mar 3, 2014 · 0 comments
Closed

Help with custom command #74

thefuriouscoder opened this issue Mar 3, 2014 · 0 comments

Comments

@thefuriouscoder
Copy link

Hi there,

I'm trying to test a whole signup functionality, the typical two steps signup, you need to fill small form, get an email with a link and fill the second big form with more personal data. I'm building a very very very custom command that connects to a gmail inbox, retrieves a message, extract the link to the second form and starts the test for the second form.

The code for my command is as follows:

exports.command = function(callback) {

    var
        self = this,
        Imap = require("imap"),
        MailParser = require("mailparser").MailParser,
        inspect = require("util").inspect,
        jsdom = require("jsdom");

    var imap = new Imap({
        user: 'XXXXXXXXXX',
        password: 'XXXXXXXXXX',
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
        tlsOptions: { rejectUnauthorized: false }
    });

    function openInbox(cb) 
    {
        imap.openBox('INBOX', true, cb);
    }

    function getConfirmationMessage() 
    {

        imap.once('ready',function(){
            openInbox(function(err, box) {
                if (err) throw err;

                imap.search(['ALL',['FROM','no-reply@mdirector.com'],['SUBJECT','Activa tu cuenta de email marketing gratis']], function(err,results) {

                    if (err) throw err;

                    var fetch = imap.fetch(results, { bodies: '' });

                    fetch.on('message', function(msg) {

                        var mailparser = new MailParser({ defaultCharset: 'utf-8'});

                        mailparser.on("end", function(mail_obj) {
                            var html = mail_obj.html;

                            jsdom.env(html,["http://code.jquery.com/jquery.js"], function(errors,window) {

                                var hash_link = window.$('a').attr("href");

                                if(typeof callback === "function") {
                                    callback.call(self,hash_link);
                                }

                            });

                        });

                        msg.on('body',function(stream,info) {

                            stream.on("data",function(chunk) {
                                mailparser.write(chunk.toString('utf-8'));
                            })

                        });

                        msg.on("end",function() {
                           mailparser.end();
                        });

                    });

                    fetch.once('error', function(err) {
                        console.log('Fetch error: ' + err);
                    });

                    fetch.once('end', function() {
                        imap.end();

                    });

                });

            });
        });

        imap.once('error', function(err) {
            console.log(err);
        });

        imap.connect();

    }

    getConfirmationMessage();

    return this;

}

and my test looks like this:

module.exports = {
    'MDirector - Signup - Step 1': function(browser) {
        browser
            .url('http://2013.mdirector.dev.antevenio.com/')
            .waitForElementVisible('body', 1000)
            .waitForElementVisible('a[id=signupBtn]', 1000)
            .click('a[id=signupBtn]').pause(1000)
            .waitForElementVisible('input[name=name]',1000)
            .waitForElementVisible('input[name=email]',1000)
            .waitForElementVisible('button[id=submit]',1000)
            .setValue('input[name=name]','MDirector Testing Account')
            .setValue('input[name=email]','mdirector.test@gmail.com')
            .click('input[name=terms')
            .click('button[id=submit]')
            .waitForElementVisible('div[id=success]',5000)
            .assert.containsText('#success','Su registro se ha completado con exito')
            .end();
    },
    'MDirector - Signup - Step 2': function(browser) {
        browser
            .checkImapInbox(function(url) {

            })
    }
};

Can I use the returned url in the callback to feed the browser.url and start a new test suite inside or outside the callback?

Besides this, I'm getting an error from nightwatch when I try to use the command, my command file is called checkImapInbox.js inside the mw_custom_commands directory, that is in my project root directory. I'm using a grunt task, via grunt-nightwatch to run my tests, my Gruntfile.js section for nightwatch looks like follow:

        nightwatch: {
            options: {
                test_settings: {
                    chrome_driver: "/usr/local/bin/chromedriver",
                    silent: true,
                    desiredCapabilities: {
                        browserName: "chrome",
                        platform: "LINUX",

                    }
                },
                settings: {
                    src_folders: ["tests/integration/frontend"],
                    output_folder: "public/test/integration/reports",
                    custom_commands_path: "./nw_custom_commands",
                    selenium: {
                        start_process: true,
                        server_path: "/usr/local/bin/selenium-server-standalone-2.40.0.jar",
                        log_path: false,
                        host: '127.0.0.1',
                        port: 4444
                    }
                },
                standalone: true,
                jar_path: "/usr/local/bin/selenium-server-standalone-2.40.0.jar"
            }   
        }

and the error I'm getting from nightwatch is:

An error occured while running the test:
TypeError: Object #<Nightwatch> has no method 'checkImapInbox'
    at Object.module.exports.MDirector - Signup - Step 2 (/home/diego/Code/mdirector.backend/tests/integration/frontend/signup-step1.js:22:14)
    at clientFn (/home/diego/Code/mdirector.backend/node_modules/grunt-nightwatch/node_modules/nightwatch/runner/run.js:149:19)
    at Object.setUp (/home/diego/Code/mdirector.backend/node_modules/grunt-nightwatch/node_modules/nightwatch/runner/run.js:52:35)
    at /home/diego/Code/mdirector.backend/node_modules/grunt-nightwatch/node_modules/nightwatch/runner/run.js:152:13
    at next [as _onTimeout] (/home/diego/Code/mdirector.backend/node_modules/grunt-nightwatch/node_modules/nightwatch/runner/run.js:103:11)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Hope you can help me.

Thanks in advance.

Diego.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants