0

I have a weird scenario:

In my automation, i have to create a user. On creation, a password wil be generated and sent to his/her email id. How can i automate to retrieve the password in his/her email(NOTE:I have to open a new window to go to his/her email and enter his/her credentials and open the particular mail which has the sent password and copy it) and come back to my application and login with his/her username and password? Pretty confusing rite

And also how i can i switch to another tab(supposingly it is not an angular-app), and execute actions to enter username and password of his email)

radio_head
  • 1,306
  • 4
  • 13
  • 28
  • Updated my question to follow your updates. Not sure why you need to actually open the email in the browser, the mail-listener is connecting to the imap server that your email would be using... it's the exact same thing. Anyway, if you must do it, the email page is going to be structured with HTML so you can use element locators as your normally would to parse the email, but I'd say that will be very difficult to consistently traverse in a stable manner. – Gunderson Sep 23 '16 at 11:50
  • actually i didn't know about these mailCatcher and mailTrap.. After encountering the problem only you get to know the solutions rite :P – radio_head Sep 26 '16 at 07:03

2 Answers2

1

For the emails, you can use a package called mail-listener. You can follow the instructions on this answer, I've done it myself and it was relatively easy to setup. Then you can get the password from the parsed email.

And as far as switching to other tabs, you can use getAllWindowHandles(), something like this:

browser.getAllWindowHandles().then(function (handles) {
   newWindowHandle = handles[1];
   browser.switchTo().window(newWindowHandle).then(function () {
       // do stuff
   });
});

Since it's non-Angular page, you'll need to set browser.ignoreSynchronization = true. You can do this in the function when you switch window handles, or you may even want to do it right before that step. You'll need to use some Expected Conditions to control test flow. Find the necessary elements you need to interact with on the non-Angular page, i.e.:

var EC = protractor.ExpectedConditions;     
var el = element(by.id('loginForm'));
browser.wait(EC.visibilityOf(el));
// do stuff

Edit: To open a new tab yourself via Protractor, you can use executeScript(). i.e.

browser.executeScript("window.open('http://www.google.com')");

Edit 2: For setting up mail-listener, the code provided in the linked question was pretty much copy/paste for me with the exception of username, password, and host. The username/password will be the same user you are trying to login as. For host, it varies by email server. You should be able to find it by googling "imap host for microsoft exchange (or whatever server you are using)" or going to the settings of that email server.

Only other code I really added to that function was logging errors which might help identify your problem and why you can't connect. You can put this in the onPrepare() function of your config file:

mailListener.on("error", function(err){
    console.log(err);
});
Community
  • 1
  • 1
Gunderson
  • 3,263
  • 2
  • 16
  • 34
  • I followed the instructions given in the mail-listener link you have given.But i am not getting anywhere. Can you explain what is happening in the code. I tried to run the same mail-listener code but its not working – radio_head Sep 27 '16 at 15:42
  • @Danny I updated my answer a little. It's a lot to answer but the `mail-listener` package is responsible for handling most of the code, all email servers use imap or smtp. This package is simply connecting to the actual email server without a UI, it's the same server that actual users use to access their emails. So this code is really just passing credentials and some configuration options (search filters, which inbox to open etc), while the helpers are responsible for actually parsing the email. – Gunderson Sep 27 '16 at 18:30
0

After a lot of searching and unserstanding, it turns out that you have to mock your smtp server and send mails to that server, to test the mailing functionality of your application. In order to make life easy there are some softwares available like mailCatcher, mailLisetner.

But my preference would be mailHog. It is really very easy to setup(actually just download the .exe) and make your application to send mails to it. It doesn't require any fancy installation steps( like mailCatcher , guys its really hectic to install it ) or any coding(like mailListener).

MailHog, it is...!

radio_head
  • 1,306
  • 4
  • 13
  • 28