Thursday, February 25, 2010

Watij - Web App Testing In Java

Watij: is a pure Java api created to allow for the automation of web applications through a real browser. It performs the same actions you would in a browser: filling out forms, navigating, clicking random stuff, and verifying results.

Did I mentioned, it was inspired by Watir - Web App testing in Ruby

Basic terminology that Watij follows is:
Container: Browser is a container.
Document: The webpage you view

To open new instance of IE, just see this www.watij.com or
IE ie = new IE();
ie.start();

To open new url:

Define URL = www.google.com

IE ie = new IE();
ie.start(URL);

To close instance:
ie.close();

You can find API: http://watij.sourceforge.net/docs/api/index.html?watij/IE.html

How to find elements using watij ?

Elements can be found in watij using many approaches:
1. Symbols: name, url, id, href, value, caption, title, alt, src, action, method, text, xpath, tag like:

ie.textField(id,“SOME_ID”);

2. Finders: exist for each symbol and also allow for more flexibility in defining ways to find elements like:

ie.htmlElement(attribute(“myattr1”,”attribute1”));

3. Xpath: Watij provides support for using XPath as a Symbol or a Finder. Watij uses the local copy of the document for faster parsing like:

ie.button(xpath, "//INPUT[@value='Hate Me']”);


4. Reg Ex: Watij also provides support for finding elements using regular expressions like:

ie.button(value, “/Hate Me/”);


Watij provides direct support for Javascript alerts, confirm pop ups, file download dialogs and prompt dialogs.

Here is an example of JS alert:
AlertDialog alertDialog = ie.alertDialog();
alertDialog.ok();

While automation there are a few things that everyone should keep in mind:

1.Ensure that you are following good design principles.
2.Try to model the application under test using a layer of abstraction.
3. Your tests should be readable by your customer.

The drawback is it only supports Windows and IE..hmmm