Writing JUnit Tests – Test Automation with Selenium

by seetaram on May 13, 2011

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", "http://127.0.0.1:85/");
		selenium.start();
	}

	public void testSearchGoogle() throws Exception {
		selenium.open("/search?hl=en&client=firefox-a&hs=LkX&rls=org.mozilla%3Aen-US%3Aofficial&q=JUnit%2Bannotations&oq=JUnit%2Bannotations&aq=f&aqi=&aql=&gs_sm=e&gs_upl=134416l136783l0l12l12l0l10l10l0l217l418l2-2");
		selenium.click("btnG");
		selenium.click("//ol[@id='rso']/li[1]/div/span/h3/a/em");
		selenium.waitForPageToLoad("30000");
	}

	public void tearDown(DefaultSelenium Selenium) throws Exception {
		selenium.stop();

	}

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.

{ 8 comments… read them below or add one }

Rahul September 29, 2011 at 9:14 am

Hi Seetaram,

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

–Rahul

Reply

seetaram September 29, 2011 at 10:05 am

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.

Thanks,
Seetaram

Reply

narasimha March 6, 2012 at 1:27 pm

thanks

Reply

sandeep October 24, 2012 at 6:06 am

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.

Reply

akhilesh November 1, 2012 at 7:06 am

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)

Reply

Ashwini April 18, 2013 at 8:58 am

Hi Seetaram,

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

REgards,
Ashwini

Reply

briggs myers personality test online October 8, 2014 at 3:25 am

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.

Reply

Swapnil February 18, 2016 at 10:40 am

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

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