Fetching values from email in protractor test case
This is something I've solved recently. Hope the solution would also apply for your use-case.
Prerequisites:
mail-listener2
package- understanding of the concept of
promises
Step by step instructions:
Install
mail-listener2
:npm install mail-listener2 --save-dev
In your protractor config initialize Mail Listener and make it available globally:
onPrepare: function () {
var MailListener = require("mail-listener2");
// here goes your email connection configuration
var mailListener = new MailListener({
username: "imap-username",
password: "imap-password",
host: "imap-host",
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();
},Create a helper
getLastEmail()
function which would wait for anemail
to be retrieved:function getLastEmail() {
var deferred = protractor.promise.defer();
console.log("Waiting for an email...");
mailListener.on("mail", function(mail){
deferred.fulfill(mail);
});
return deferred.promise;
};Example test case:
describe("Sample test case", function () {
beforeEach(function () {
browser.get("/#login");
browser.waitForAngular();
});
it("should login with a registration code sent to an email", function () {
element(by.id("username")).sendKeys("MyUserName");
element(by.id("password")).sendKeys("MyPassword");
element(by.id("loginButton")).click();
browser.controlFlow().await(getLastEmail()).then(function (email) {
expect(email.subject).toEqual("New Registration Code");
expect(email.headers.to).toEqual("myemail@email.com");
// extract registration code from the email message
var pattern = /Registration code is: (\w+)/g;
var regCode = pattern.exec(email.text)[1];
console.log(regCode);
});
});
});
Unable to retrieve email information in protractor
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(...)
Protractor E2E testing; Verifying account activation email
Try to use browser.wait(getLastEmail)
instead of browser.controlFlow().await(getLastEmail()
How to get the newest email with mail-listener2
I think this may help you, I implemented mail-listener2 using the same post you followed and it works great me for me. I just added a few extra parameters:
Under my config's onPrepare
, I create a date:
var emailDate = new Date().getTime();
Then under my mailListener
initialization:
var mailListener = new MailListener({
username: ...
password: ...
...
searchFilter: ["NEW", "UNSEEN", ["SINCE", emailDate]]
});
This should configure mailListener to only look for emails delivered after the time of emailDate
, which is created when your test is started. You can also specify an exact date, i.e. ['SINCE', 'May 20, 2010']
More info on the node-imap docs (which mailListener2 utilizes)
Related Topics
How to Pass Arguments to Event Handlers in Jquery
When Should I Use a Semicolon After Curly Braces
Javascript: Find Longest Word in a String
Differences Between Contenttype and Datatype in Jquery Ajax Function
How to Create a Session Using JavaScript
How to Select a Value in a Select Dropdown with JavaScript
Get Keys of JSON-Object in JavaScript
Ajax Post and Plus Sign ( + ) -- How to Encode
How to Resolve Typeerror: Cannot Convert Undefined or Null to Object
How to Display Binary Data as Image - Extjs 4
What Does the at Symbol (@) Do in Es6 JavaScript? (Ecmascript 2015)
React.Js Replace Img Src Onerror
[Vue Warn]: Property or Method Is Not Defined on the Instance But Referenced During Render
Alert When Browser Window Closed Accidentally
How to Make an Angularjs Directive to Stoppropagation
JavaScript Ie Detection, Why Not Use Simple Conditional Comments