In one of earlier post, we have how to handle multiple windows or popups in selenium, this post will explain how to handle popups or new windows in protractor using typescript
Protractor gives a method getAllWindowHandles()
which returns all the windows currently browser is holding.
and then we can access each window by using index, for e.g – index 0 determines the main browser windows and then rest follows
Implementation
We will take a demo site , when you click on “New Browser” button, another window pops up.
so to test the scenario, we will 1st capture the main browser title, then navigate to child window and perform some action like get the child window title, you can navigate by using below line
browser.switchTo().window(childHandle)
then, we will return back to main window and get the title, so we can compare 1st title and last title, it should be same
Code – MultiWindows.ts
import { browser, element, by } from "protractor"; describe("Handle multiple windows", function(){ beforeEach(function(){ browser.ignoreSynchronization = true; browser.get("https://skpatro.github.io/demo/links/"); }) it("Multiple windows handle", function(){ browser.getTitle().then(function(txt){ console.log("Main browser title :- "+ txt); }) browser.findElement(by.name("NewWindow")).click(); let windowHandles = browser.getAllWindowHandles(); let parentHandle, childHandle; windowHandles.then(function(handles){ parentHandle = handles[0]; childHandle = handles[1]; console.log("Total Handles :- " + handles.length); browser.switchTo().window(childHandle).then(function(){ browser.getTitle().then(function(txt){ console.log("Child browser title :- " + txt); browser.close(); }) }) browser.switchTo().window(parentHandle).then(function(){ console.log("Returning to main windows..."); browser.getTitle().then(function(txt){ console.log("Main browser title :- "+ txt); }) }) }) }) })
Change the conf.ts file specs to MultiWindow.js and run the test to see how we can navigate to different windows
I AM getting below Error
Total Handles :- 2
[20:29:57] E/launcher – Unable to get browser (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 47 milliseconds
I am using IE 11 as browser. Also I am writing code directly in JS file.
For IE, make sure you have proper browser driver setup, to know more follow link – https://stackoverflow.com/questions/26395216/with-protractor-how-to-setup-internet-explorer-configuration
Hi Sunil, i am getting ‘Failed: element not interactable’ error when i click on the pop-up lick. But this open the pop-up and script fails not allowing me to get any information from the pop-up. Please help!