Aneesh
Aneesh

Reputation: 599

Protractor open new window

Lets say I am working on a chat application, where a user (X) can login to the application and send a message to another user (Y).

I am now trying to automate the test to send and receive messages, with the following steps:

1. X Logs in with his username/password.
2. Selects Y from a list and sends a "Test Message" to Y.
3. X signs out.
4. Y logs in with his username/password.
5. Checks if he has received the message from X.
6. Y replies to X with "Reply to Test Message".
7. Y signs out.
8. X logs in, and checks if he got the reply.

If I were to do this manually, I would just open two windows (one of them in incognito), login as X on one and as Y on the other and verify the results.

So, few questions: 1. Does protractor allow a way in which a new window can be opened without any clicking anywhere? Just a function to spawn a new window programmatically? 2. Is it possible to have these windows not share the session of the user (kinda like incognito)?

Upvotes: 4

Views: 10916

Answers (4)

Alex Cap
Alex Cap

Reputation: 113

I'm doing it in this way:

let _2ndBrowser = browser.forkNewInstance();
let url = 'https://google.com';

browser.get(url);
_2ndBrowser.get(url);
browser.element(by.xpath(...)).click();
_2ndBrowser.element(by.xpath(...)).click();

works fine.

If you are using PageObject you can set browser as constructor argument. and set in in new:

function LoginPage(browserContext) {
  this.setUserName = function(userName){ 
    return browserContext.element(/*locator*/).sendKeys(userName)
  }
}

it('set login name', function() {
  let _2ndBrowser = browser.forkNewInstance();
  let user1LoginPage = new LoginPage(browser);
  let user2LoginPage = new LoginPage(_2ndBrowser);

  user1LoginPage.setUserName('alice');
  user2LoginPage.setUserName('bob');
})

PROFIT =)

Upvotes: 0

Jacek Góraj
Jacek Góraj

Reputation: 1023

For everyone who is looking for typescript solution.

vrachlin code with small changes will work just fine:

browser.getAllWindowHandles().then(handles => {
   browser.switchTo().window(handles[1]); // 0 or 1 to switch between the 2 open windows
});

Upvotes: -1

Ashok M A
Ashok M A

Reputation: 528

The following works fine for me.

browser.executeScript('window.open()').then(function () {
     browser.getAllWindowHandles().then(function (handles) {
            var secondWindow = handles[1];
            browser.switchTo().window(secondWindow).then(function () {
                return browser.get(newPageToOpen);
            });
     });
});

Hope this helps.

Upvotes: 5

vrachlin
vrachlin

Reputation: 817

You could open a new incognito by sending the CTRL+SHIFT+N key combo:

browser.actions()
       .sendKeys(protractor.Key.chord(protractor.Key.CONTROL, protractor.Key.SHIFT ,"n"))
       .perform();

then use window handles, and switch between windows

browser.getAllWindowHandles().then(function(handles){
  browser.switchTo.window(handles[1]); // 0 or 1 to switch between the 2 open windows
});

after you switched between the windows, all your next methods will be for the active window. if it works for you in incognito, should work here too.

Upvotes: 3

Related Questions