Monday, May 4, 2009

JUnit Parameterization

Now say you need to run the same test case for the multiple values. you can do this with the help of the parametrization.

JUnit 4 has changed the whole way you do the parametrization. it has introduced a special runner called Parameterized. It allows you to run the same test(s) with different datasets.

Now let's go step by step to make the test case parametrized ( data driven )
Use Parameterized Runner.
If you wanted to make any test case parameterized ( Data Driven ) you need to make it's whole class to run using Parameterized Runner.
This will be done with the help of the @RunWith annotation. Just add the following line before the class.
@RunWith(value=Parameterized.class)

Make Dataset
Now you need to have dataset with all input values and expected output values. Here I have used two dimension array with one column as (input) value and other column as expected (output).
Now every testcase ( method with annotation @Test) will run as many times as the number of rows in the dataset (array).

Now let's move to the example
Example
The example below will do the following task
1. Navigate to the Google.co.in Site.
2. For all the available dataset it will verify the factorial using google calculator.

import com.thoughtworks.selenium.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.*;

import java.util.regex.Pattern;
//require for the collection class
import java.util.*;

@RunWith(value=Parameterized.class)
public class parameterization {

private static DefaultSelenium selenium;
private long expected;
private int value;

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

@Parameters
public static Collection data() {
return Arrays.asList( new Object[][] {
{ 1, 0 }, // expected, value
{ 1, 1 },
{ 2, 2 },
{ 24, 4 },
{ 5041, 7 },//wrong expected
});
}

public parameterization(long expected, int value) {
this.expected = expected;
this.value = value;
}
@Test
public void firstTest() throws Exception {

selenium.open("/");
selenium.type("q", value+"!=");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");

String text = selenium.getText("//div[@id='res']/table[1]/tbody/tr[1]/td[3]/h2/b");
//to remove whitespaces
text= text.replace(" ","");
// will verify the text
assertEquals(value+"!="+expected,text);

}
}
If you will look at the above example you will find out that the init() method which is used to open the firefox browser and to navigate to google.co.in site has marked annotation @BeforeClass rather then @Before.
This is because we need site to be opened for all the testcases.

The array which we have used is static. This is because we want it to be same for all the testcase. If it will not be static a new array will be created for the number of nows in the column. and so every test will get the same value, first row.

The another things is when you will run the last test case will show as fail. This is because we have put its expected value in the dataset as 5041 which is wrong. This is just to verify that the assert statement is working correctly.

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