Building an Automated Test Script with RFT without Recording

Some times ago, I wrote an entry about Automated Test. Until now, I still agree that a good automated test project, should be written without recording anything at all. I don't know why those automated test tools provide "recording" function. I don't think recorded automated scripts will be useful enough to support software development, since UI will always change, and some day you'll always get to migrate "things" around.

By using recording, clicking a button will become "pressing mouse at the screen coordinate x and y". Some day when you run this script again, any of the condition below will fail the script:

  • the application windows are maximized (it wasn't maximized when you recorded it)
  • the UI was updated, now there are some texts above the OK button
  • the OS was on resolution 1024 x 800 when you recorded the script (your boss gave you a new high resolution monitor)
  • some other reasons that will always be there
So I have been doing some research and testing with Rational Functional Test version 7.0 and web browser Internet Explorer 6.

There are some tools you should be familiar with when using RFT. For starter, here are the 2 most important tools you have to know:
The last icon on the image below, is for opening the "Test Object Inspector".
The second icon, is for showing the "Enable Environments" preference.



The Test Object Inspector, is something you can use to figure out the properties' keys and values of you application's component. Once you hover it, the window will show all properties related to it.



"Enable Environments", is the preference for setting the application you want to test. In this entry, I will be talking only about web application. With this preference, you can add the browser that you want to use for testing.

There is one thing you should know, that RFT 7.0 can only work with IE6. If you want to perform testing in IE7 or IE8, then you will have to use RFT 8.0.



Below are some codes that you can use as a base to build an integrated automated test script. It has been already tested and running well, as long as you are using the correct version of RFT and web browser.

To start running a browser with a particular web address:
ProcessTestObject proc = startBrowser("http://www.google.com/");

To kill the browser:
proc.kill();

To wait for a specific time (allowing the page to load):
sleep(10); //meaning wait for 10 seconds.. 

And the most important thing, to get the DomainTestObject for the loaded HTML domain. With this DomainTestObject you can later perform a "find" to look for the objects you wish to test:

private void clickLink(String title) {
TestObject[] links = dto.find(atDescendant(".class", "Html.A", ".text", title));
while (links != null && links.length <= 0) {
links = dto.find(atDescendant(".class", "Html.A", ".text", title));
}
clickSomething((GuiTestObject) links[0]);
}

To find a link, and to click on it:

private void clickLink(String title) {
TestObject[] links = dto.find(atDescendant(".class", "Html.A", ".text", title));
while (links != null && links.length <= 0) {
links = dto.find(atDescendant(".class", "Html.A", ".text", title));
}
clickSomething((GuiTestObject) links[0]);
}

To click on the object found:

private void clickSomething(GuiTestObject object) {
boolean tryAgain = true;
while (tryAgain) {
try {
object.click();
tryAgain = false;
} catch (UnsupportedActionException e) {
sleep(1);
}
}
}

The reason behind try and catching the UnsupportedActionException is that because sometimes the page hasn't been finished loaded, and then clicking the object will throw an exception and it will stop the entire script, which we don't want to happen.

To input some text into an input box:

private void typeInput(String name, String inputText, boolean hitEnter) {
TestObject[] links = dto.find(atDescendant(".name", name));
while (links != null && links.length <= 0) {
links = dto.find(atDescendant(".name", name));
}
while (!((GuiTestObject) links[0]).exists()) {
sleep(1);
}
clickSomething((GuiTestObject) links[0]);
TopLevelTestObject textBox = new TopLevelTestObject(links[0].getTopParent());
textBox.inputChars(inputText);
if (hitEnter) {
textBox.inputKeys("~");
}
}

To press Yes/No at browser's confirmation dialog:

private void clickConfirmationButton(String button) {
TestObject[] links = null;
do {
sleep(1);
links = dto.find(atDescendant(".class", "Html.DialogButton", ".text", button));
} while (links != null && links.length == 0);
clickSomething((GuiTestObject) links[0]);
}


Those are just examples, you are always free to modify and improve it with your own creativity.

4 comments: (+add yours?)

Anonymous said...
This comment has been removed by a blog administrator.
Apply Visa Online said...

Really nice article :)
Very informative.

You may also check Website Builder Script

Devang Patel said...

can you give some link which explains all these classes in more details other classes which i can use to write java code like yours i want to learn more

Anonymous said...

PLease explain step by step how to find the object without using functional script recorder