Search code examples
javascriptprotractore2e-testingemail-verificationangular-e2e

Protractor E2E testing; Verifying account activation email


I am trying to create a script for email verification. I mean when we create an account on the site, then a verification mail will come to the given mail address and then we have to go to that mail and verify it(clicking on the link/or fetching the code). I tried this solution. But I have got stuck on the specific error.

This is code which I am trying.

describe("Sample test case", function () {

    function getLastEmail() {
    let deferred = protractor.promise.defer();
    console.log("Waiting for an email...");

    mailListener.on("mail", function(mail){
        deferred.fulfill(mail);
    });
    return deferred.promise;
}


beforeAll(function () {
    browser.waitForAngularEnabled(false);
    browser.get("https://mail.google.com/mail/");
    isAngularSite(false);
    browser.sleep(3000);
});

it("should login with a registration code sent to an email", function () {
    element(by.id("username")).sendKeys("MyemailID");
    element(by.id("password")).sendKeys("mypassword");
    element(by.id("loginButton")).click();

    browser.controlFlow().await(getLastEmail()).then(function (email) {
        expect(email.subject).toEqual("BillPledge Email Verification");
        expect(email.headers.to).toEqual("support@billpledge.com");

        // extract registration code from the email message
        let pattern = /Registration code is: (\w+)/g;
        let regCode = pattern.exec(email.text)[1];

        console.log(regCode);

    });
});

});

and this is my conf file.

// An example configuration file.

exports.config = {
// The address of a running selenium server.
// seleniumAddress: 'http://localhost:4444/wd/hub',

// if we are using protractor's webdriver-manager locally, you cannot use selenium Address
// If the webdriver-manager needs to start a local server, you can use
selenium: 'http://localhost:4445/wd/hub',
seleniumPort: 4445, // Port matches the port above

// Capabilities to be passed to the webdriver instance.
capabilities: {
    'browserName': 'chrome'
},

// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['./e2eTest/GmailTest.js'],

// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 300000
},

allScriptsTimeout: 200000,


onPrepare: function () {
    global.isAngularSite = function (flag) {
        browser.ignoreSynchronization = !flag;
    };

    browser.driver.manage().window().maximize();

    //To generate the report.
    let HtmlReporter = require('protractor-beautiful-reporter');
    jasmine.getEnv().addReporter(new HtmlReporter({
        baseDirectory: 'Results/Report'
    }).getJasmine2Reporter());

    let reporter = new HtmlReporter({
        baseDirectory: 'Results/Report'
    });

    let path = require('path');
    new HtmlReporter({
        baseDirectory: 'Results/Report'
        , preserveDirectory: false
        , cssOverrideFile: 'css/style.css'
        , takeScreenShotsForSkippedSpecs: true
        , screenshotsSubfolder: 'images'
        , jsonsSubfolder: 'jsons'
        , takeScreenShotsOnlyForFailedSpecs: false
        , gatherBrowserLogs: true
        , pathBuilder: function pathBuilder(spec, descriptions, results, capabilities) {
            // Return '<browser>/<specname>' as path for screenshots:
            // Example: 'firefox/list-should work'.
            return path.join(capabilities.caps_.browser, descriptions.join('-'));
        }
        , metaDataBuilder: function metaDataBuilder(spec, descriptions, results, capabilities) {
            // Return the description of the spec and if it has passed or not:
            return {
                description: descriptions.join(' ')
                , passed: results.passed()
            };
        }
    });

    let MailListener = require("mail-listener2");

    // here goes your email connection configuration
    let mailListener = new MailListener({
        username: "myemailid",
        password: "mypassword",
        host: "imap.gmail.com",
        port: 993, // imap port
        tls: true,
        tlsOptions: {rejectUnauthorized: false},
        mailbox: "INBOX", // mailbox to monitor
        searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
        markSeen: true, // all fetched email willbe marked as seen and not fetched next time
        fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
        mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
        attachments: true, // download attachments as they are encountered to the project directory
        attachmentOptions: {directory: "attachments/"} // specify a download directory for attachments
    });

    mailListener.start();

    mailListener.on("server:connected", function () {
        console.log("Mail listener initialized");
    });

    global.mailListener = mailListener;
},

onCleanUp: function () {
    mailListener.stop();


},

};

But while executing the script I am getting following error. The error is:

Error: Please log in via your web browser: 
https://support.google.com/mail/accounts/answer/78754 (Failure)

and

Failed: browser.controlFlow(...).await is not a function

I know I am doing some mistake but I am not able to figure out. So can anyone can help me in pointing out and solve it, not solve but at least some suggestion which can help in running this script perfectly.

Thanks


Solution

  • Try to use browser.wait(getLastEmail) instead of browser.controlFlow().await(getLastEmail()