Opening new window in WebDriver using C#

1,974 views
Skip to first unread message

Abu Hamzah

unread,
Nov 5, 2012, 11:35:35 PM11/5/12
to seleniu...@googlegroups.com

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.

Abu Hamzah

unread,
Nov 6, 2012, 10:03:13 AM11/6/12
to seleniu...@googlegroups.com
walikum assalam, 

I am getting count = 1 but i see that i have two window open (parent and a child)
and when i check to see the count of WindowHandles (c#) and getWindowHandles(java),

int handlesCount = driver.WindowHandles.Count; //it shows count = 1


here is what i have done so far:

          //get the current window handles 
           string currentWindow = driver.CurrentWindowHandle;
            string popupHandle = string.Empty;
            ReadOnlyCollection<string> windowHandles = driver.WindowHandles;

            foreach (string handle in windowHandles)
            {
                if (handle != currentWindow)
                {
                    popupHandle = handle; break;
                }
            }

            //switch to new window 
            driver.SwitchTo().Window(popupHandle); //child handle

            string _childTitle = driver.Title;


any thoughts?


On Tuesday, November 6, 2012 2:39:37 AM UTC-5, musaffi wrote:
Hello Abu,
Assalamu alaaikkum...

This is very much possible in Selenium Webdriver...One thing you need to know here is when your selenium webdriver starts a session and opens a browser loading your URL, that browser window will be the parent window... so driver control/focus will be always on that parent window...
Now as you mentioned ,when you click on a link on this parent browser it results in opening a new window ... so this newly opened window we can call it as a child window... And remember the Selenium webdriver control is still in your parent window and not on this newly opened child window... so you wont be able to do any action on this child window... this is the problem you facing here...

This can be solved by switching the driver control to your child window.. and this can be achieved as follows :

WebDriver provides a function called .getWindowHandles();... this gets the window handles for all the window getting opened by your test actions .... and this function returns a Set of Strings... and by code it will look like

set <String > allwindowhandles= driver.getWindowHandles();

allwindowhandles is just a variable here ...



Use a java logic of Iterator to capture all the windowhandles , like here parent and child window handles....
Iterator<String> itr = allwindowhandles.Iterator();
String parentwindow = itr.next();
String childwindow = itr.next();

now use the function called .SwitchtoWindow(childwindow).. to move the driver focus to the second opened window...

and you can do the action on that window,like verifying the tile and all.... as your testing needs
 
Now if you want to close the second opened window and move back to the parent window.. you should do similat steps ... by close command and switching the driver control.... :)

Hope this helps ... and you may have to convert the logic to C#...(i have no clue on C#)/.... :)


~Musaffir

musaffi

unread,
Nov 7, 2012, 2:24:19 AM11/7/12
to seleniu...@googlegroups.com
Hi Abu,


You don't have to get the count of the handlers here... You have to get the window handlers it self , which are nothing but strings ....
and save these 2 window handlers in 2 variables.... window handlers are usually some alpha numeric values....

pass these window handlers in driver.switchto().window () function to make the driver control in it .... and do the action on that browser...

~ Musaffir

Shaba K

unread,
Nov 7, 2012, 6:33:01 AM11/7/12
to seleniu...@googlegroups.com
Approach i've followed is .

Stored the initial string WindowHandler as parent

& new one as child.

Then used switchTo.Window("string");

Will do the magic.

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/8bHiKSZvXmoJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages