Friday, January 9, 2009

JWebUnit and JUnit Test Examples Step 55

Now that we have jWebUnit and JUnit installed in our project with the eclipse build path pointing to it, it is time to create some test cases.

You should have a package called com.testing with a test class in the package that I am calling Webtest.java.

Paste this code into that class:

package com.testing;

import junit.framework.TestCase;
import net.sourceforge.jwebunit.junit.WebTester;


public class Webtest extends TestCase {
private WebTester tester;

public void setUp() {
try
{
super.setUp();

}
catch(Exception e)
{

}
tester = new WebTester();
tester.setBaseUrl("http://localhost:8080/UserBook/");

}

public void test1() {

tester.beginAt("/");
tester.assertTextPresent("This is my first tag!");

}


}


This code sets the base url to the location of your project. Each method must start with test so our first test case is called test1. The first command I do is called an assert. The assertTextPresent will check if the text is present on the page. If it isn't, then it will fail, and if it is, it will pass the test. To run the test, right click your test class in your project->run as->JUnit Test. If you do this in Eclipse, your test will run.

Go To Step 56

1 comments:

Azhaguvel.A October 18, 2011 5:51 AM  

Is it possible to open a browser through JWebUnit.