Tuesday, January 12, 2010

Java Web UI Functional Testing with Selenium

All of us required to write Functional test cases for the Web UI. As Java developers, we are not used to write Web UI automated test cases and it is really a cumbersome job to write those test cases. It requires a lot of learning curve and lot of experience to write really good UI test cases, that are automated.
But wait, here comes the simple and free to use rescue. The Selenium. It comes with an easy to use and interactive IDE, that we can leverage to record our Web UI workflow and it will generate Java Test Cases for us. Moreover, it has support for not only Java, but different technologies like C#, Perl, PHP, Ruby, Groovy etc.

Selenium IDE is a great tool to quickly write the automated test cases for web UI. for Selenium tests.It can be downloaded and installed as a Firefox extension, and it allows you to record, edit, and debug tests. More information is at Selenium Site

Simply download the Selenium IDE to your firefox. Then start the application server, where you are running your web app and open web UI in firefox.
  1. Go to Tools -> Selenium IDE and it will open up interactive IDE.
  2. You can click on record icon and start recording your web actions.
  3. Once it is over, go to IDE and stop recording and see your actions got recorded.
  4. You can edit the code using wonderful facilities of Selenium IDE using Xpath and other simple syntax.
  5. Run the test through Selenium and see the test results.

You can also get the Java code of the Selenium recording.
In Selenium IDE, go to Options -> Format -> Java. And you will get the Java Junit / TestNG
code for your WebUI functional testing.

Ajax and Selenium Issues:
Now, if you're using Ajax based Web UI, it may happen that Selenium tries to test the component before Ajax request actually getting processed.
  • So, if you are running from Selenium IDE, slow down the speed of test.
And in Java code, just add one line as per the requirement.


//This is important step to set the speed as per test case
//It requires the Selenium to wait for Ajax components to load and available
//If you're setting low speed, you may encounter element not found, because
//Selenium will try to access components, before they are loaded.
selenium.setSpeed("1000");


Windows Vista and Selenium Issues:
In Windows Vista, due to User Account Control, when you run from normal command prompt, you may encounter Firefox crash or firefox chrome registration issues. In order to solve these issues, just run command prompt as an administrator and run your Selenium test cases through it.

Selenium and Maven Integration:
Selenium has very good support with Maven Integration. Just do some google search and you'd get lots of references about how to integrate and run through.
A simple and good example is present at: Selenium-Maven Integration

No comments:

Post a Comment