0
function toWindow(tab){
    browser.getAllWindowHandles().then(function (handles) {
            browser.switchTo().window(handles[tab]); 
        return handles.length; 
    });
}

console.log(toWindow(1));

My expected output: should display the total count of opened tabs in a browser.

Solutions I have tried:

  • make a global variable to receive the handles.length value - still undefined
  • add return before browser.getAllWindowHandles()... still undefined
glinda93
  • 7,659
  • 5
  • 40
  • 78
JPLet
  • 1
  • 1
  • 1. You don't return anything from the `toWindow` function. 2. `browser.getAllWindowHandles` returns a promise, you need to handle it as one via `async/await` or the promise API. – VLAZ Aug 17 '20 at 05:48
  • I'm new to javascript and protractor, how to handle a promise with async/await? I really need some examples. – JPLet Aug 17 '20 at 05:59
  • i just want to use handles.length outside of this block of code: browser.getAllWindowHandles().then(function (handles) { browser.switchTo().window(handles[tab]); return handles.length; }); – JPLet Aug 17 '20 at 06:00

0 Answers0