Saturday 18 August 2012

Handling Popup Windows in Selenium/WebDriver

Hello Everyone, 
            Today i will be blogging about THE most asked question about selenium/webdriver. That is, switching to popup window using web driver. I heard this question from many people, saw it as a question/explanation in many blogs. 
       I found it difficult while implementing Selenium in my company also. The situation is gets worse when there is more than 1 popup at a particular time and window names are same -:(. This is bad, isn't it?
         Ok let us see the solution now. CODE it DOWN!! Before that create a test scenario. You have a application, you login to the application, Home page shows up. Click on any link, it opens up a popup window,You have to do some operation in that window.

Below is the code to for the above scenario. Let me explain one by one.



See the second line of code, 
String sWinHndl=wd.getWindowHandle();

In the above line of code, we are fetching the current window handle. This you need to take for the home page in the above test scenario.

Why are we doing this?
Here goes the explanation, this is really required for you to understand. When you do have main window and popup window title same then there is only one thing left which is different i.e. Window Handle. a unique number generated for each and every window. We need to play with that id in the code.

Next line is for your test code to do some operation to open up the pop up window. Your pop up window is open now. Now there are 2 window handles currently present.
Explanation for next line of code is like -:  You already have X(sWinHndl) and now you have X & Y (lisOfWinds). Compare if it is not X then switch i.e. switch to Y.

This piece of code will only work if there are 2 windows opened at a particular time. But how do you handle it for more than 2 windows, for each window you open, do you want to keep the window handle?? Think and let me hear from you also. Trick is still in the window handles, see how you can use it effectively.

Please feel free to drop a comment if you found this information is helpful for you. If you have any comment/suggestion/issue with anything mentioned above, then also please share it with us.

Thanks!!

Sunday 12 August 2012

Select Option from Drop down box using WebDriver

It has been quite some time, added any post related to Selenium/WebDriver. Today i will be going to write about selecting a option value from drop down box. Selenium provides a Select class to do that, it is pretty easy to use. Ok lets have one sample html for our demonstration-:






We will see now how to select options using selenium/webdriver's Select class. Below code explains how to use select class to select option from drop down box.



 Apart from the above 3 methods, there are other methods which can be used to perform other actions also.
Here is the link for Select Class methods.

Please feel free to drop a comment if you have any suggestion/comment/Issue related to the above post.

Thanks!!!

Related Posts

Related Posts...