4

Referencing the information from the question Fetching values from email in protractor test case, I am still unable to reference the emails. In my test case, the 'expect' is not getting executed for some unknown reason to me.

Also if I use the line,

browser.controlFlow().await(getLastEmail()).then(...)

There is a 'browser.controlFlow(...).await is not a function error'

conf.js

var MailListener = require("mail-listener2")

exports.config = {
framework: 'jasmine2',
specs: ['./test.js'],
jasmineNodeOpts: { defaultTimeoutInterval: 360000 },
allScriptsTimeout: 60000,

onPrepare: function () {

    var mailListener = new MailListener({
        username: "username",
        password: "password",
        host: "imapPort",
        port: 993, // imap port
        secure: true,
        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")
    })

    mailListener.on("error", function(err){
      console.log(err)
    })

    mailListener.on("server:disconnected", function(){
      console.log("imapDisconnected")
    })

    global.mailListener = mailListener
},

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

The test case:

describe('Email Testing', function () {

 it('should login with a registration code sent to an email', function () {
   //this line causes a 'browser.controlFlow(...).await is not a function' error
  // browser.controlFlow().await(getLastEmail()).then(function (email) {
  getLastEmail().then(function (email) {
    // The expect does not get executed as it should fail
    expect(email.subject).toEqual('My Subject')
   })
 })
})

 function getLastEmail () {
   var deferred = protractor.promise.defer()
   console.log('Waiting for an email...')

   mailListener.on('mail', function (mail) {
     console.log('No Console Log Here!')
     deferred.fulfill(mail)
   })
  return deferred.promise
 }

I am not certain what I am missing in my test case to be able to read the subject or body of the email?

Community
  • 1
  • 1

2 Answers2

1

Ran into the same issue today. Turns out the API for the webdriver and ControlFlow has been updated and await has been changed to wait. Yeah, subtle difference. See the new API reference here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise_exports_ControlFlow.html

To schedule a task for the wait condition, change your code to look like this:

browser.controlFlow().wait(getLastEmail()).then(...)
mattm
  • 276
  • 2
  • 8
0

you would basically have to wrap that asynchronous code inside of a promise and the pass that promise/function into the flow.execute()

var flow = protractor.promise.controlFlow();
flow.execute( getLastEmail() ).then(function(email){
     text = email.text
});
user683742
  • 80
  • 2
  • 8