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 badges4 Answers
Reset to default 5The 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
版权声明:本文标题:javascript - Protractor open new window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741230653a2362047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论