Wednesday 16 July 2014

Common Selenium Challenges & How to Solve them



In any software test automation, we may often run in to some challenges w.r.t usage of the automation tool or the support it does provide for the Application under test (AUT).

We have listed some of the challenges we generally face in the automation phase.

1. Handling multiple browser windows

Handling the multiple browser windows is always been a challenge. This includes:

New popup window for date selection in the application.


Here is the sample code to handle the date popup
// Tp open calender 
webdriver.findElement(By.xpath("//img[@alt='calendar']")).click();
Store the main window in a variable
String mainwindow = webdriver.getWindowHandle();   
//switch from main/parent window to new window  using the windowhandles
for (String newwindow : webdriver.getWindowHandles()) 
 webdriver.switchTo().window(newwindow); 
 // perform action on new calendar window opened webdriver.findElement(By.id("calendar_month_txt")).click(); 
}
Again, we need to switch back to main window which has been initially opened to continue the test steps execution.

We will be using the SwitchTo command to again select the main application window.
webdriver.switchTo().window(mainwindow); 

Every link clicked on the application will open a new window with dynamic title.
To switch between the window having the dynamic title
a.       Having the date and time stamp in the title
                Eg: “Test Application 2014-05-07 04:05PM”

b.      Having the version number of the application release
                Eg: “Test Application Ver: 14.02 release”

In order to work with these we need to use the regular expression to switch between the windows by using the title names
       
Eg: assuming some part of the title is constant, then
webdriver.switchTo().window("Test Application *");
& if there is any random string before and after the required title

webdriver.switchTo().window(".*Test Application.*");

2. Handling Frames

Now a days, Requirements are very rich in nature where the developer are expected to handle the every functionality in one single page with different frames in place.

Iframe is used to embed the one document with in the other html document by which one can handle new document or DOM with in the main webpage.

But it will be obvious challenge to handle the different frames and switching between them. And often the frames will be given under the <frameset>  </frameset>

Sample frames structure 1:

<frameset>
                <iframe id=”frm1”>
                                <body>
                                                <input id=”txt1” type=”text”>FirstName</input>
                                </body>
</iframe>
                <iframe id=” frm2”>
                                <body>
                                                <input id=”txt2” type=”text”>LastName</input>
                                </body>
</iframe>

</frameset>

In the above case,


1.       Switch to frame1
a.       webdriver.switchTo().frame("frm1");
2.       Perform the action on the input box
a.       Webdriver.findElement(By.Id(“txt1”)).sendKeys(“user name”);
3.       Then switch back to the main content using the method called “defaultContent”
a.       webdriver.switchTo. defaultContent();
4.       Again switch to another frame2 to interact with other textbox
a.       webdriver.switchTo().frame("frm2");

b.      Webdriver.findElement(By.Id(“txt2”)).sendKeys(“password”);

Sample 2:

<frameset>
                <iframe id=”frame1”>
                                <body>
                                                <input id=”txt1” type=”text”>FirstName</input>
                                </body>

<iframe id=” frame2”>
                                                <body>
                                                                <input id=”txt2” type=”text”>LastName</input>
                                                </body>
</iframe>
</iframe>          
</frameset>

1.       Switch to frame1
a.       webdriver.switchTo().frame("frm1");
2.       Perform the action on the input box
a.       Webdriver.findElement(By.Id(“txt1”)).sendKeys(“user name”);
3.       Again switch to another frame2 to interact with other textbox
a.       webdriver.switchTo().frame("frm2");
b.      Webdriver.findElement(By.Id(“txt2”)).sendKeys(“password”);
4.       Then switch back to the main content using the method called “defaultContent”

a.       webdriver.switchTo. defaultContent();

3. Interacting with the hidden elements in a webpage

Like we see some of the applications are the webelements overlapped on top of the actual elements like text box, combox box etc. Refer the image below.


We have the combo box with Yes/No values, but actually “select” html tag is hidden and added a span over it with anchor <a> tag to select the items in it.

Using Webdriver, we are having the challenge to invoke the elements which are hidden. To handle such elements we need to rely on java script executor as shown below.

String javascript = " javascript:var s = document.getElementById('testobject');"
+ ln + "for (i = 0; i< s.options.length; i++){"
+ ln + " if (s.options[i].text.trim().toUpperCase() == '"+valuetomatch+"') {"
+  ln + " s.options[i].selected = true;"
+  ln + " s.click();"
+  ln + " if (s.onchange) {"
+  ln + " s.onchange();"
+ ln + " }"
+  ln + " break;"
+  ln + " }"
+   ln + "}";                                                                                                          ((JavascriptExecutor) webdriver).executeScript(javascript);

To click on any hidden element
String Script = "javascript:document.getElementById('testobject').click();";
((JavascriptExecutor) webdriver).executeScript(Script);

Here the java script command is formed as a string and being executed by the javascriptExecutor. As every browser we use has in built capability of executing the java script commands.