Nick Kahn
Nick Kahn

Reputation: 20078

Opening new window in WebDriver using C#

EDIT 4:

enter image description here EDIT 3 enter image description here

EDIT 2

    string currentWindow = driver.CurrentWindowHandle;

    driver.SwitchTo().Window("");
    string childTitle = driver.Title;

    driver.SwitchTo().Window(currentWindow);
    string parentTitle = driver.Title;

the above code gives me the same title for parent window or child window.

EDIT:

<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay  Around</a>

how to verify the title of a newly window open and once i verified then close the opened new window?

so in my page I have a link and click on the link and it opens a new window and now I am not sure how to verify the title of that window.

here is what i have done so far.

GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

//it opens a new window

now i want to switch focus on the new window and verify the title and close the new window back to the previous window.

Upvotes: 2

Views: 14158

Answers (2)

JimEvans
JimEvans

Reputation: 27486

The piece that most people miss when dealing with popup windows in IE is that a click on an element is asynchronous. That is to say, if you check the .WindowHandles property immediately after a click, you may lose the race condition, because you're checking for the existence of a new window before IE has had the chance to create it, and the driver has had a chance to register it exists.

Here's the C# code I would use to perform the same operation:

string foundHandle = null;
string originalWindowHandle = driver.CurrentWindowHandle;

// Get the list of existing window handles.
IList<string> existingHandles = driver.WindowHandles;
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

// Use a timeout. Alternatively, you could use a WebDriverWait
// for this operation.
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
while(DateTime.Now < timeout)
{
    // This method uses LINQ, so it presupposes you are running on
    // .NET 3.5 or above. Alternatively, it's possible to do this
    // without LINQ, but the code is more verbose.
    IList<string> currentHandles = driver.WindowHandles;
    IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
    if (differentHandles.Count > 0)
    {
        // There will ordinarily only be one handle in this list,
        // so it should be safe to return the first one here.
        foundHandle = differentHandles[0];
        break;
    }

    // Sleep for a very short period of time to prevent starving the driver thread.
    System.Threading.Thread.Sleep(250);
}

if (string.IsNullOrEmpty(foundHandle))
{
    throw new Exception("didn't find popup window within timeout");
}

driver.SwitchToWindow(foundHandle);

// Do whatever verification on the popup window you need to, then...
driver.Close();

// And switch back to the original window handle.
driver.SwitchToWindow(originalWindowHandle);

Incidentally, if you're using the .NET bindings, you have access to a PopupWindowFinder class in the WebDriver.Support.dll assembly, which uses a very similar approach to the locating popup windows. You may find that class meets your needs exactly, and can use it without modification.

Upvotes: 2

Pani Kumar
Pani Kumar

Reputation: 315

GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

// Post above operation a new window would open as described in problem

// Get hold of Main window's handle

string  currentWindow = Driver.CurrentWindowHandle;

// Switch to the newly opened window

Driver.SwitchTo().Window("Your Window Name");

// Perform required Actions/Assertions here and close the window

// Switch to Main window

Driver.SwitchTo().Window(currentWindow);

Upvotes: 0

Related Questions