问题
I have a non-angular page where I need to click on 2 links. When clicking on one of the link that automatically opens in a new tab. Now I switch to new tab and set the browser.ignoreSynchronization = false
because the newly opened tab is a angular window; and call one of my test. Once verified. I want to close the current tab and go back to the non-angular page to click on link #2 which would again open in a new tab. I tried window.close
but i am getting window is undefined. I used browser.window and even that is not working. Please advise
element(by.id('trMyUrl'));
browser.getAllWindowHandles().then(function (handles) {
var secondWindowHandle = handles[1];
var firstWindowHandle = handles[0];
browser.switchTo().window(secondWindowHandle).then(function () { //the focus moves on new tab\
browser.ignoreSynchronization = false;
empLogin.test();
window(secondWindowHandle).close()
// window.close()
// browser.close()
// browser.window.close()
})
});
回答1:
Close your second window that is currently focused on and then switchTo()
previous tab first to perform operations on it. Here's how -
browser.switchTo().window(secondWindowHandle)
.then(function () {
browser.ignoreSynchronization = false;
empLogin.test();
}).then(function(){
browser.close(); //close the current browser
}).then(function(){
browser.switchTo().window(firstWindowHandle) //Switch to previous tab
.then(function(){
//Perform your operations on the first tab
});
});
Hope it helps.
来源:https://stackoverflow.com/questions/32512059/protractor-closing-current-tab