RFT scripting
RFT scripting

RFT scripting

Timed Search of Elements on a Web Page

One of the most common issues when searching for elements on web pages is finding those elements who get loaded at different moments in time. It would be a simple but inefficient solution to introduce a delay big enough, and then just search for the element.

How about doing it smarter?

Now, in order to simply find an element on a web page using IBM Rational Functional Tester scripting (equivalent to HP / Mercury QuickTestPro descriptive programming), you can use a method which already exists in RFT (API reference link here):

public TestObject[] find(Subitem properties, boolean mappableOnly)

Using the above method as such can be an issue if the browser does not load all the content/elements at the same time; in this case your script must wait until the searched element is loaded. One way to get past this inconvenient is creating a method as below:

public static TestObject[] find_html_element(TestObject html_object, String elem_attribute_name, String elem_attribute_value) {
int timeout = 0;
double sleep_time = 0.1;
double timeout_wait = 4000;
TestObject[] html_element = html_object.find(atDescendant(elem_attr_name, elem_attr_value), false);
while (html_element.length == 0) {
    sleep(sleep_time);
    timeout++;
    html_element = html_object.find(atDescendant(elem_attr_name, elem_attr_value), false);
    if (timeout <= timeout_wait) {
        timeout = 0;
        break;
    }
}
if (html_element.length == 0) {
    System.out.println("In browser the element with name " + elem_attr_name + " and value: " + elem_attr_value + " was not found");
    return null;
    } else return html_element;
}

Example of using find_html_element method:

RootTestObject root = RootTestObject.getRootTestObject();
TestObject[] LoginButton = find_html_element(root, ".name", "login")

The advantages of this method

A total wait time can be configured to wait the element to be loaded  (in above code the total wait time is configured to 400 (seconds = sleep_time * time_wait); if the element is not found in that total wait time, the method will return null and a message – element was not found.
during that total wait time, the element is searched every 0.1 seconds (sleep_time); if the element is found, then the method will break and will return that element.