Protractor closing current tab

为君一笑 提交于 2020-01-13 13:12:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!