+7 votes
2.8k views
in Software Testing by (920 points)

In my test case a new window is opening and i have to perform action on that window then have to come back to orignal window. 

driver.switchTo().window("winname")

Using this but dont know the sindow name which is opening after click on a button.

Plese help me how can i manage new window.

closed

1 Answer

+3 votes
by Expert (4.6k points)
selected by
 
Best answer

Its a good question if we dont know the name of window so how can we switch to another window, For this we can use this code 

//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
 
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
  driver.switchTo().window(winHandle);
}
 
//Perform the actions on new window
 
//Close the new window, if that window no more required
//driver.close();
 
//Switch back to original browser (first window)
 
//driver.switchTo().window(winHandleBefore);
in this code threw window handler we are all avalable window then we are performing some action on that new window and coming back to orignal window ,i hope you got the point.
by (120 points)
@pankaj see this for(String winHandle : driver.getWindowHandles()){
  driver.switchTo().window(winHandle);
}
this loop will count your all open window of web driver and then perform action any window in which you want. try this once.
by (920 points)
yes @Maan is right its working. i have used it.
by
you can use its index instead of the name, another way to grab window name is using windows functiobs like getwindowname....

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated