Saturday, May 2, 2009

Junit 4

Recently I was learning selenium with JUnit. I had bit prior experience with the JUnit 3 so it didn't take much time to setup and run the test case. But then After I decided to take a look at the JUnit 4. And believe me guys it is far better then it's ancestors.So let's go step by step with Junit4.

What's new in JUnitt4 ?

JUnit Annotations

Editing test case

Running test case


So what's new in the JUnit 4 ??
JUnit 4 has introduced the annotation which is supported by JAVA 5, same as TestNG. And that has added much of the flexibility in writing the test cases.

Now you do not need to:
  • Write down the test cases name as starting with test.
  • Extend you test case
  • Have setUp() or tearDown() methods
  • add any main or suit() methods in the test case.

JUnit Annotations:
take a look at some of the frequently used JUnit annotations while writing test cases.

  • @Test: Marks the method as the test case.
  • @Before: Marks the method to execute before each and every test case. Same as setUP()
  • @After : Marks the method to execute after each and every test case. Same as tearDown()
  • @BeforeCalass: Marks the method to execute before all the test cases. Used to do the initilization which is command for all the test cases.
  • @AfterClass: Marks the method to execute after all the test cases executed. Used to do the system wide cleanup.


The thing to take care here is, If a method has annotation @BeforeClass or @AfterClass it has to be static. And that means all the variable inside that also has to be static.

Converting Selenium test case to JUnit 4.

Take a look at the following code, which has been recorded through selenium and exported in java.
package com.example.tests;

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

public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com/", "*chrome");
}
public void testNew() throws Exception {
selenium.open("/");
selenium.type("q", "selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
selenium.click("link=Selenium IDE");
selenium.waitForPageToLoad("30000");
}
}

Now to convert this into the JUnit 4 testcase you need to perform the following things
  • Remove first line starting with Package or comment it.
  • add the following import statement
    import org.junit.*;
  • Remove the setUp() method.
  • Do no extend the class, Remove the "extends SeleneseTestCase" string
  • Now you need to create the object of the DefaultSelenium class as we have removed the extend statement and setUp() method. Add the following code in you class for that.

    private DefaultSelenium selenium;
    @Before
    Public void init() throws Exception {
    selenium = new DefaultSelenium("localhost", 4444, "*chrome","http://google.com");
    selenium.start();
    }

The Exported code will look like as below after editing it.
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import org.junit.*;

public class GoogleSearch {

private DefaultSelenium selenium;

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

@Test
public void firstTest() throws Exception {

selenium.open("/");
selenium.type("q", "selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
selenium.click("link=Selenium IDE");
selenium.waitForPageToLoad("30000");
}
}
  • Make sure the file name and the class name is same.
  • Save the file and compile it with following code.

    javac path/filename.java

    If everything goes fine up to the point code will compile without any error.


Run the testcase
Now before you run the test case make sure that the Selenium server has started if not please start it.

Now run your test case by following command on the shell prompt.

java java org.junit.runner.JUnitCore classname

4 comments:

Vojtech said...

Hi,

I wrote a library which integrates Selenium into JUnit 4 infrastructure.

http://code.google.com/p/selenium4junit/

selenium4junit provides two JUnit runners (one for individual tests and one for test suites), both of which offering following features:
- configurable Selenium sessions
- test suites containing tests depending on each other
- embedded Selenium server startup
- advanced error reporting
- ability to plug in your custom error reporting / Session execution listener

selenium4junit integrates with Maven as well.

Vojtech

bavads said...

I also used a SeleneseTestCase object (instantiated it in the init()) and used the methods exposed by the same. Thought I would post this in case someone else came in here looking for why the assertTrue doesnt work... :-)

Milan said...

Hi All,
I was trying to click on a particular link inside a open source tool after login with the with the help of junit code, but its throwing error that unable to find particular link.
so please suggest me the proper way for this

Unknown said...

Hi Milan,
There may be two things, first your xpath is wrong or it's dynamic that changes. You can check out the xpath with firebug or xpather. If xpath is dynamic you can use regular expression to solve it.
The second thing is that xpath is right but junit is not able to identify link because execution speed it to high. put waitforpageload after you login and set the execution speed with setSpeed. And problem will be solved probably.

Post a Comment