admin管理员组

文章数量:1278919

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)?

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)?

Share Improve this question asked Sep 24, 2015 at 11:22 AneeshAneesh 5992 gold badges5 silver badges21 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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.

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

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.

I'm doing it in this way:

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

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 =)

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
});

本文标签: javascriptProtractor open new windowStack Overflow