Using Selenium for testing portlets deployed in WebSphere Portal Server

I am big fan of unit testing, There are lots of advantages of good unit testing suite, one is its a very good way of documentation, it makes it easier to maintain the code and also it give you confidence even during development if you can run a big test suite on the code and everything works.

One problem with portlet development is its hard to develop unit test cases, because of lot of things, lets say if your using Portal API such as Controller SPI, or portlet model then how do you mock test that code or what if your code makes use of Ajax. Only option in that case would be to deploy your code in WPS and then test it through browser manually.

Recently i learned a tool called Selenium, which makes it easy to test the code running inside browser. The way it works is first you install a Firefox plug-in called Celenium IDE and use it to record user interaction inside the browser. Once the recording is done you can either play it back inside the browser or Selenium can create a JUnit test for you. The Junit test will actually lanuch the browser of your choice and play back the browser interaction you recorded. One good thing with Selenium is that it remembers the link name or form name that clicked on instead of the URLs so it works very well in the portlet world where urls are not constant or get generated on the fly.

I just figured out how to use Selenium to test the portlets deployed in WebSphere portal server and i thought i will share it with everyone. So first i created a simple SeleniumTestPortlet that shows a form to user in the view mode, when you enter user name and submit, the control will go to processAction(), inside the processAction i am reading the userName and setting it as render parameter and the user name is displayed in the generated HTMl after that. You can download the SeleniumTestPortlet from here. This is how the form looks like



Now i wanted to automate this part, so what i did is i used the Selenium IDE to test this interaction and verify that the userName is getting displayed in the output of doView() after i submit the form. After recording i converted the test in the Java- Junit Format which looks like this




package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class Untitled 2 extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");
}
public void testUntitled 2() throws Exception {
selenium.open("/wpcert/demo/!ut/p/c4/04_SB8K8xLLM9MSSzPy8xBz9CP0os3hnd0cPE3MfAwMLT2cTA08TJ_8gI1d_AwNfQ_2CbEdFAGNKK9I!/");
selenium.type("userID", "wasadmin");
selenium.type("password", "wasadmin");
selenium.click("ns_7_CGAH47L008IC40I4BOR2EO00E4__login");
selenium.waitForPageToLoad("30000");
selenium.click("link=fun");
selenium.waitForPageToLoad("30000");
selenium.click("link=Selenium Test Page");
selenium.waitForPageToLoad("30000");
selenium.type("userName", "Sunil Patil");
selenium.click("submit");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("User Name Sunil Patil"));
}
}


After that i created a SeleniumTestCase as simple java project and added JUnit, Selenium-server.java and selenium-java-driver.jar to the build path. And i created a JUnit test case class like this


package com.webspherenotes.test;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

import org.openqa.selenium.server.SeleniumServer;

public class SecondTestCase extends SeleneseTestCase {

private SeleniumServer seleniumServer;
public void setUp() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();

setUp("http://localhost:10040/firefox", "*chrome");
}
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

public void testCase() throws Exception {
selenium.open("/wpcert/demo/!ut/p/c4/04_SB8K8xLLM9MSSzPy8xBz9CP0os3hnd0cPE3MfAwMLT2cTA08TJ_8gI1d_AwNfQ_2CbEdFAGNKK9I!/");
selenium.type("userID", "wasadmin");
selenium.type("password", "wasadmin");
selenium.click("ns_7_CGAH47L008IC40I4BOR2EO00E4__login");
selenium.waitForPageToLoad("30000");
selenium.click("link=fun");
selenium.waitForPageToLoad("30000");
selenium.click("link=Selenium Test Page");
selenium.waitForPageToLoad("30000");
selenium.type("userName", "Sunil Patil");
selenium.click("submit");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("User Name Sunil Patil"));
}
}


All that i had to do was add the lines shown in the bold to the code generated by Selenium IDE. Now i can run this code from my Eclipse IDE as unit test and i can see that a browser instance is opened and the same test is played back.

1 comment:

Software Development Company said...

Hello Sunil,
The Article on Using Selenium for testing portlets deployed in WebSphere Portal Server . It give detail information about it .Thanks for Sharing the information about Using Selenium for testing portlets deployed . Software Testing Services