Tuesday, January 12, 2016

Several Lessons Learned with Selenium

Today we have many choices of open-source software tools for automation testing such as Jmeter, OpenSTA, WebLoad and Selenium. Among these, Selenium offers an excellent set of best practices, and it is the tool worth investigating.

​Selenium is a web-based application testing software initially developed by Jason Huggins in 2004. It is an open-source software that works on all three major platforms – Windows, Mac and Linux. Selenium supports a wide range of languages, including Java, Python, Ruby, Perl, C#, PHP and others.

For a quick start with Selenium, we use Selenium IDE which has the Record and Playback tool to record and playback scripts. We use Selenium RC to write and run test scripts for testing web application UI’sSelenium WebDriver allows driving browsers locally or remotely as a userAnd, the other tool is Selenium Grid which provides a test execution mechanism for executing test cases on different machines.

Selenium has many advantages, including:
  • Simple and powerful testing of DOM (Document Object Model)
  • Supporting continuous integration, suitable for agile projects
  • Supporting multiple platforms and browsers
  • Allowing distributed testing through Selenium Grid
  • Able to integrate with other tools such as TestNG, Sikuli and AutoIt
There are a number of limitations of Selenium, however. One limitation is that it does not provide logs and other details on test results after executing test cases. Thus, it is difficult for us to debug test scripts. Another is that, we do not have any option to verify the location of images when needed. And it does not support automating non-web application elements such as windows for uploading, downloading and required authentication
When working with Selenium, I have faced a number of problems which I have resolved successfully. I am sharing below several lessons that I followed to overcome limitations of Selenium.

Lession 1: Integration Selenium with Sikuli to verify images

There are certain objects on web pages which may not be detected by Selenium such as those of Silverlight, Java Applets, canvas objects, JavaScript and jQuery. So it is hard to verify them in Selenium. To overcome this problem, we can use another open source tool called Sikuli and integrate it with the existing Selenium’s automation framework. Sikuli is a robust and powerful tool to automate and test graphical user interfaces (GUI). Sikuli scripts automate anything we see as a user on screens. The core part of Sikuli Script is written in Java, which means that we can use Sikuli Script as a standard JAVA library in the Seleminum framework.

  • Capture screenshots of objects.
  • Create a folder for storing screenshots and place screenshots in this folder


  • Write testing scripts by importing classes from Sikuli libraries org.sikuli.script.*

           Import org.openqa.Selenium.WebDriver;
           Import org.openqa.Selenium.firefox.FirefoxDriver;
           Import org.sikuli.script.*;

           public class sikuliTest {

              @Test
              public void Test1 throws FindFailed {

              // Create a new instance of the Firefox driver
              WebDriver driver = new FirefoxDriver();

              // Navigate to the website http://www.google.com.vn
              driver.get("http://www.google.com.vn");

              //Create and initialize an instance of Screen object   
              Screen screen = new Screen();

             //Declare image path  
              Pattern image = new Pattern("images\GoogleLogo.png");

            //Wait 10ms for Logo is displayed
            screen.wait(image, 10);

            //Verify the Logo is displayed
            screen.find(image);
  }
}
How the script works. First it will navigate to Google website and recognizes the Logo matching with the image in the image folder as below
The second lesson: Handle some windows popup


Web applications may have features showing popup windows such as file-upload and authentication windows. Unfortunately, Selenium does not have the capability to handle window popup. To overcome this limitation, we can integrate Sikuli or AutoIt tools with Selenium.



Handle some windows popup with Sikuli

  • We can use Sikuli to handle some windows popup like file download popup. Here is an example use Sikuli and Selenium to go to the page http://docs.Seleniumhq.org/download/and download Selenium Server on Firefox. The problem is Selenium cannot handle download popup as below


The script above navigates to http://docs.Seleniumhq.org/download, then browses link “2.44.0”, and finds the area that matches the “Save File” image to click on it.

However, Sikuli script does not work when the computer screen is locked off because Sikuli is fully based on image recognition on the screen, so it cannot recognize any image on the locked screen.



Handling window popup with AutoIt


AutoIt is a freeware with BASIC-like scripting language designed for automating Windows GUI applications. It uses a combination of simulated keystrokes, mouse movements and window/control manipulations in simulating user’s actions.


Below are steps taken to handle the following popup window by using AutoIt and Selenium. 



  • Using “SciTE Script Editor” to write an AutoIt script and save it into AutoIt file “Authentication_example.au3” (. Below is the script in the file:

                WinWaitActive("Authentication Required")
                Send("Username")
                Send("{TAB}")
                Send("Password")
                Send("{ENTER}")

  • Right-click on the file “Authentication_example.au3” and click “Compile Script” to compile this file into an executable file “Authentication_example.exe”.

Lesson 3: Managing test suites, test scripts and test objects

Selenium does not provide detailed results and logs, and it does not offer capabilities to manage test suites, test scripts and test objects either. We can overcome this limitation by integrating Selenium with Robot framework (RIDE). RIDE supports keyword driven testing and it provides Excel sheet table with high level language for scripting. RIDE allows us to write and understand test scripts easier. It also provides capabilities to manage test scripts, analyze test results and logs easily.

To integrate Selenium with RIDE, do the following steps

  • Install JDK
  • Install Python
  • Install robot framework
  • Add selenium2libray to robot framework.
  • Install robot framework IDE
Excel sheet and high level language are shown as follows
Keyword-driven 

Details log:


Details report:
In summary, Selenium is a powerful tool for web automation. And importantly, it is free. As it supports multiple languages, thus, you can use the language that you are most comfortable with. Selenium has an active support community which provides extensions and useful help when needed. However, it has a number of limitations. I hope this post is useful to overcome some of some of the limitations you face with Selenium. 

HoaLe




1 comment: