Hi,
If I read this correctly, you have multiple tests that are dependend on one another. I.E. testFoo() can only be run if testBar() has been done. Otherwise you wouldn't need the conversation state to be passed each time.
Normally this is frowned upon. You would need to try to keep the tests more localized and independend of one another (they are webunit tests afterall ;-) ).
Also make sure if you navigate through your application, you only need to test whether you are still where you expect you are. It is not necessary to assert everything each time you visit a page, just the stuff which will get you where you want to be. Create a seperate test for the everything test which is done only once.
If you need some navigation through the application, which is repeated, extract that code to a method and call that in the two tests. See the example below.
public class TestPersonCRUD extends WebTestCase
void testSearchFoo() {
beginAt("/newPerson");
setFormElement("name", "Foo");
submit();
assertTextPresent("New person Foo added");
clickLinkWithText("Search");
setFormElement("personName", "F%");
submit();
assertLinkWithTextPresent("Foo");
}void testDeleteFoo() {
beginAt("/newPerson");
setFormElement("name", "Foo");
submit();
assertTextPresent("New person Foo added");
clickLinkWithText("Search");
setFormElement("personName", "F%");
submit();
assertLinkWithTextPresent("Foo");
clickButton("deleteFoo"); assertTextPresent("Person Foo deleted");
}
}
this could become:
public class TestPersonCRUD extends WebTestCase
public void createNewPerson(String name) {
beginAt("/newPerson");
setFormElement("name", "Foo");
submit();
assertTextPresent("New person " + name + " added");
}
void searchByName(String personName) {
clickLinkWithText("Search");
setFormElement("personName", personName);
submit();
}
void testSearchFoo() {
createNewPerson("Foo");
assertLinkWithTextPresent("Foo");
searchByName("F%");
assertLinkWithTextPresent("Foo");
}
void testDeleteFoo() {
createNewPerson("Foo");
searchByName("F%");
assertLinkWithTextPresent("Foo");clickButton("deleteFoo"); assertTextPresent("Person Foo deleted");
}
}
I've used this technique with great success.
Hope to help,
Martijn Dashorst.
Srinivasan Ranganathan wrote:
Hi
I have a web app that I'd like to test. I find my code becoming too hard to maintain, each test case gets too long with lots of code repeated across them. In an effort to refactor the tests, I'm thinking of this approach. Each "page" is a step required to complete a "task". There's a tester for each page that knows how to fill in its values and navigate back, forward, cancel or complete the task. So a test case simply creates a tester for the page, sets values, runs assertions. Advancing to the next/previous page returns the page tester object for that page.
What's the best way to do this?
Here is what I came up with so far. My PageTester extends WebTestCase. The constructor of the Tester inits the TestContext. The client of the PageTester, say HomePageTest extends TestCase. In HomePageTest, I might do
public void testSomething() { HomePageTester tester = new HomePageTester(); tester.setValue(); SomeOtherPageTester otherPageTester = tester.next(); }
My problem is that the test can't make use of the easier to use methods (setFormElementWithLabel...etc). To use them, the tests need to extend WebTestCase or use WebTester. In either case, i'll be faced with the problem of keeping the Tester's web conversation and the Test's web converstaion in sync. I'm just thinking out loud and looking for feedback.
One other way I thought of is to add a WebTester constructor that takes a HTTPUnigDialog that store the WebTester in the Tester class to hold the conversation state. But JWebUnit 1.1.1 (which Im using or 1.2) don't have such a constructor.
------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click _______________________________________________ Jwebunit-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jwebunit-development
