简体   繁体   中英

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

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/

If I gather correctly your application will produce the window on it's own without further userintervention. You should be able to wait for the page to load and then you can call your asserts as normal.

Selenium already has your browser-session, so a new window is not an issue for selenium, it is just new content.

foreach (string defwindow in driver.WindowHandles)
{
    driver.SwitchTo().Window(defwindow);
    if(driver.Title == "")
    {
     selenium.WindowFocus();
     selenium.SelectWindow("");
    }

}      

"" - indicates your window Title

I've got some code you might like. The quickest solution is to use Popup Finder, but I've made my own method as well. I would never rely on the order the Window Handles are in to select the appropriate window. Popup Window Finder:

PopupWindowFinder finder = new PopupWindowFinder(driver);
driver.SwitchTo().Window(newWin); 

My Custom method. Basically you pass it the element you want to click, your webdriver, and optionally the time to wait before searching after you click the element.

It takes all of your current handles and makes a list. It uses that list to eliminate the previously existing windows from accidentally getting switched to. Then it clicks the element that launches the new window. There should always be some sort of a delay after the click, as nothing happens instantly. And then it makes a new list and compares that against the old one until it finds a new window or the loop expires. If it fails to find a new window it returns null, so if you have an iffy webelement that doesn't always work, you can do a null check to see if the switch worked.

public static string ClickAndSwitchWindow(IWebElement elementToBeClicked,
IWebDriver driver, int timer = 2000)
        {
            System.Collections.Generic.List<string> previousHandles = new 
System.Collections.Generic.List<string>();
            System.Collections.Generic.List<string> currentHandles = new 
System.Collections.Generic.List<string>();
            previousHandles.AddRange(driver.WindowHandles);
            elementToBeClicked.Click();

            Thread.Sleep(timer);
            for (int i = 0; i < 20; i++)
            {
                currentHandles.Clear();
                currentHandles.AddRange(driver.WindowHandles);
                foreach (string s in previousHandles)
                {
                    currentHandles.RemoveAll(p => p == s);
                }
                if (currentHandles.Count == 1)
                 {
                    driver.SwitchTo().Window(currentHandles[0]);
                    Thread.Sleep(100);
                    return currentHandles[0];
                }
                else
                {
                    Thread.Sleep(500);
                }
            }
            return null;
        }

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" .

        IWebDriver _driver = new FirefoxDriver();
        _driver.Navigate().GoToUrl("https://www.google.com");

        ReadOnlyCollection<string> WindowHandles = _driver.WindowHandles;
        foreach (string item in WindowHandles)
        {
            _driver.SwitchTo().Window(item);
            string browserTitle = _driver.Title;
            string browserPageSource = _driver.PageSource;
            string browserURL = _driver.Url;
        }

Use ReadOnlyCollection and handle browser, get the title of your window and compare and get focus on your desire browser window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM