Writing JUnit Tests – Test Automation with Selenium

For Test Automation with Selenium, we can either utilize JUnit or TestNG framework.  Both the frameworks are pretty simple and easy to use.

In JUnit 3, a test which needs to be executed before all the other test methods is named as “SetUp” and the other method which will be executed just before stopping the test execution is termed as “TearDown”.

Name of every test in JUnit needs to be prefixed as “test”.  Let us have a look at the below code snippet to understand this better.


	public void setUp() throws Exception {
		DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "");

	public void testSearchGoogle() throws Exception {

	public void tearDown(DefaultSelenium Selenium) throws Exception {


If we want to execute a particular test in Eclipse, then just right click on the test, then click Run As and click JUnit Test.

In the above example setUp method contains Selenium initialization and tearDown method contains the code to stop Selenium.

Selenium Test method is prefixed with test (testSearchGoogle).  This is a simple flow of JUnit 3 test.

Comments 8

  • Hi Seetaram,

    Can you please dedicate a post to differentiate TESTNG with Junit.. That will be really helpful


    • Hi Rahul,

      Sure.. I will definitely write a post on the TESTNG and also the difference between TESTNG & JUNIT.. Kindly, subscribe to the email and you will be updated whenever I write a new topic.


  • Hi
    For me browser is always getting closed after the script is executed, though i have not included any tear down method in my script, also i am not using any selenium.stop() command.

    Can you please help with that, i want the browser to be remained open after the script execution.

  • plz help to short out this problem.I fed up 3 days to recover from It…

    com.thoughtworks.selenium.SeleniumException: Timed out after 30000ms
    at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
    at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
    at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:369)
    at google.testCasee(google.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    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)

  • Hi Seetaram,

    I like your site very much. I would like to receive updates.


  • Hey there, You’ve done an incredible job. I will definitely digg it and personally recommend to my friends.
    I’m sure they’ll be benefited from this website.

  • That’s great! Thanks for bringing it to us.However, one quieston: are there any ways to show all ignored test cases? The reason is that sometime we want to take a look at all ignored test cases and to see why they are ignored and try to fix them. But it seems that it was very hard.Frankie

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.