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.

16 comments:

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


Anonymous said...

It is very good blog for Selenium testing tool. The blog will useful to improve my knowledge about testing. Thanks for sharing this valuable blog.
Selenium Training in Chennai | Selenium Training

Mirnalini Sathya said...

Thanks for posting useful information.
Selenium Training in Chennai
Selenium Training
iOS Training in Chennai
iOS Training Institutes in Chennai
Android Training in Chennai
Android Training

mercyroy said...

Thanks first of all for the useful info.
the idea in this article is quite different and innovative please update more.
German Training in Nungambakkam
German Training in Mogappair
german learning classes in bangalore
german coaching classes in bangalore

Sadhana Rathore said...

Learned a lot from your blog. Good creation and hats off to the creativity of your mind. Share more like this.
AWS course in Chennai
AWS Certification in Chennai
AWS Course
AWS Training institute in Chennai
RPA Training in Chennai
Angularjs Training in Chennai

Anbarasan14 said...

Excellent blog!!! I got to know the more useful information by reading your blog. Thanks for posting this blog.

Spoken English in Bangalore
Spoken English Institutes in Bangalore
Spoken English Coaching Classes near me
English Speaking Classes in Bangalore
Spoken English Coaching in Bangalore
Spoken English Training Institute in Bangalore
Best Spoken English Coaching in Bangalore

sudhagar said...

This is really too useful and have more ideas and keep sharing many techniques. Eagerly waiting for your new blog keep doing more.
Aws Training Institutes in Bangalore
Aws Certification Training in Bangalore
Aws Training Center in Bangalore
Best Institute For Java Training In Bangalore
Java J2ee Courses In Bangalore

VenuBharath2010@gmail.com said...

Awesome Writing. Extra-Ordinary piece of work. Waiting for your future updates.
Data Analytics Courses in Chennai
Big Data Analytics Courses in Chennai
Data Analytics Courses
Big Data Analytics Training
Data Analytics Courses in Adyar
Data Analytics Courses in Porur
Data Analytics Courses in Anna Nagar

Veta said...

Vetadelhincr is among the best English speaking institute in DELHI/NCR offers English spoken classes by experts that helps you to sharpen your communication skills, Interview training and personality development to be proficient in the language.

English Speaking Institutes in Pitampura
IELTS in Laxmi Nagar
Spoken English Classes in Pitampura
Personality Development in Pitampura
Interview Training in Pitampura

Abhi said...

Thanks for info
Web Design Company in Bangalore
Website development in Bangalore

Abhi said...


Thanks for info
software application development services

Shiva said...

This is really too useful and have more ideas and keep sharing many techniques..thank you
Data Science Training In Hyderabad
Python Training In Hyderabad
Tableau Training In Hyderabad

Unknown said...

Appreciating the persistence you put into your blog and detailed information you provide.

Oracle dba training chennai | oracle dba training course chennai

varshaDeepak said...

Best Oracle Training in Chennai

SAP Academy said...

Great Stuff
SAP Training in Chennai
Java Training in Chennai
CCNA Training in Chennai
Pearson Vue Exam Center in Chennai
QTP Training in Chennai
Selenium Training in Chennai
Hardware and Networking Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
AWS Training in Chennai

Sowmiya R said...

Learning technological skills seems to be the important thing in this modern era. I thank you for being instructive and kind in this write-up.

Oracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore