Selenium 2 with JUnit4 – Create Tests & Generate Reports

by seetaram on June 7, 2011

JUnit is a unit testing framework which mostly used for unit testing of Java projects.  JUnit4 is the enhanced version of JUnit 3 and comes with annotations such as @Before, @After, @Test, @AfterClass, @BeforeClass, etc.  (Please refer – http://tinyurl.com/6al62p8 for detailed discussion on these annotations).   JUnit4 can be utilized with Selenium 2.0 WebDriver or Selenium 1.x for Test Automation of web applications.

Now, the question is what if we create our own framework instead of using JUnit Testing Framework for test automation.  There are some pros and cons with this approach.

Advantages:

We can define our own,

  • Report format
  • Logging
  • Exception handling

Disadvantages:

  • Java (Any coding language) coding effort
  • Maintenance is difficult as getting testing resources with Java knowledge will be difficult (Resources with Java experience may not be interested in Automation Testing)

Using JUnit testing framework with Selenium 1.x / Selenium 2.0 WebDriver

Advantages:

  • Very good reporting structure is available
  • Can generate XML, HTML reports
  • There are options available to create test methods, test suites, etc
  • Utilize Selenium IDE or Firebug / Firepath to record test scripts

Disadvantages:

  • We will not be able to define our own reporting format

Let us have a look at the below code.  First let us try to use JUnit 4 with Selenium 1.x (Selenium RC, Selenium Java Client Driver, and Eclipse)

package com.selftechy.parameterization;

/*
*Author - Seetaram Hegde
*Selenium 1.x with JUnit 4
*/
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SeleniumOne extends SeleneseTestCase {

	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
		selenium.start();
	}

	@Test
	public void testSearchGoogle() throws Exception {
		selenium.open("http://www.google.co.in/");
		selenium.type("q", "Selenium with JUnit 4");
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}

Below code is the example for using JUnit 4 with Selenium 2.0 WebDriver

package com.selftechy.selenium2;

/*
*Author - Seetaram Hegde
*Selenium 2 WebDriver with JUnit 4
*/
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.support.pagefactory.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.server.SeleniumServer;

public class testCode {

	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.get("http://www.google.com");
	}

	@Test
	public void testAdvancedSearch() throws Exception {
		driver.findElement(By.name("q")).sendKeys("Selenium 2.0 WebDriver");
		driver.findElement(By.name("q")).submit();
		System.out.println("Page title is: " + driver.getTitle());
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}

I think the above code snippets will definitely help to differentiate between using Selenium 2 with JUnit 4 and Selenium 1.x with JUnit 4

Refer previous post – http://tinyurl.com/6al62p8 for more information on annotations.

Reports can be generated using either Ant or using TestNG framework.

Visit: Generate HTML Reports with JUnit & Ant

{ 13 comments… read them below or add one }

shashi June 16, 2011 at 7:05 pm

i wanted to know how to generate reports in HTML format using junit and htmlunitdriver when we are using seleniumrc.

Reply

seetaram June 28, 2011 at 8:28 am

Hi,

This we can accomplish with Ant. Use ANT tasks junit, test, and junitreport to execute tests and generate HTML reports.

Reply

yehan August 29, 2011 at 2:58 am

hey,seetaram,Can I use maven realize this function?

Reply

seetaram August 29, 2011 at 8:05 am

Yes.. Definitely you can use. Instead of build.xml you need to write pom.xml

Reply

seetaram July 1, 2011 at 6:45 am

Hi, I have written a post on using JUnit to generate HTML report…visit: http://tiny.cc/c2w62

Reply

Irfan September 18, 2011 at 3:11 pm

Hi everyone,

Would you please tell me how to create our own framework
??

Thanks in advance….

Reply

seetaram September 21, 2011 at 6:36 am

I have explained this question in detail in the blog. Please refer the post – “http://selftechy.com/2011/06/19/designing-test-automation-framework-with-selenium” for more details on framework

Reply

Ankur June 27, 2012 at 6:13 pm

I am trying to run the example in Selenium webdriver 2 and get the following stack trace-

org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
Build info: version: ’2.21.0′, revision: ’16552′, time: ’2012-04-11 19:09:00′
System info: os.name: ‘Windows 7′, os.arch: ‘amd64′, os.version: ’6.1′, java.version: ’1.7.0′
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:95)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:69)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:200)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:94)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:147)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:85)
at TestYellow.setUp(TestYellow.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

java.lang.NullPointerException
at TestYellow.tearDown(TestYellow.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Can you help me spot the problem??

Reply

Ankur June 30, 2012 at 7:15 pm

This problem is addressed in the discussion here-
https://groups.google.com/forum/#!topic/webdriver/600j__wM0qY

Reply

Maitreya July 6, 2012 at 7:01 am

Since unable to edit my comment – link for adding screenshot in report:
http://stackoverflow.com/questions/1727616/custom-junit-report

Reply

Prashanth May 3, 2013 at 11:31 am

Hi ,
Nice writeup.
I have question when I run ANT and generate Junit reports
I see that the package name is shown as “None” and the class name is shown as Blank. Can you let me know as to what the issue is.

Regards
Prashanth

Reply

Debdeep June 13, 2013 at 5:20 pm

“Exception from ImportData Function: Unable to bind to locking port 7054 within 45000 ms”

Getting this error while trying to launch firefox from Selenium.
Below code is used
FirefoxProfile profile = new FirefoxProfile();
driver = new FirefoxDriver(profile); //Assigns the driver new capibilities
driver_bkd=new WebDriverBackedSelenium(driver, “http://systest1.myuhc.com”);
BrowserName=”Firefox”;

Any suggestions, how should I proceed?

Reply

seetaram August 21, 2013 at 8:57 am

Shutdown and restart the machine

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email