Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver selecting new window c#

Trying to write some test cases using selenium webdriver in c# and have a scenario which i'm unsure of how to resolve

user scenario is searching a table for a patient, select a patient then a new window opens and then assert various items on the window

my issue is i'm unable to select the new window to assert anything from, it's not a pop-up window, it's a full new browser window but it has no window title/name to identify it by, how would I be able to switch driver focus to this window?

thanks in advance

like image 274
jim7 Avatar asked Mar 13 '15 09:03

jim7


2 Answers

It is really easy in Selenium WebDriver. By using SwitchTo method

driver.SwitchTo().Window(driver.WindowHandles.Last());

See this blog post as well

http://binaryclips.com/2015/03/13/selenium-webdriver-in-c-switch-to-new-window/

like image 136
joinsaad Avatar answered Nov 01 '22 13:11

joinsaad


This code worked for me. In my case the new window/tab is a PDF that have some weight, so I make some custom waits while it loads.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
int previousWinCount = driver.WindowHandles.Count;
// Perform the action to open a new Window
wait.Until(driver => driver.WindowHandles.Count == (previousWinCount + 1));
driver.SwitchTo().Window(driver.WindowHandles.Last());
wait.Until(driver => driver.Url.Contains("desired_url_or_a_substring_of_it"));

Note that the driver.Url when the PDF is loading is "about:blank".

like image 33
x7BiT Avatar answered Nov 01 '22 13:11

x7BiT