Krisztián
Krisztián

Reputation: 83

How can I know after TAB where the focus is and how to work with that component in RFT?

I work with RFT and would like to know how to get the object where the Focus is and be able to work with the object after. For example my script starts than I write getScreen().inputKeys("{TAB}") and

  1. I would like to know which component has the Focus

  2. and after this I would like to know how can I get the properties of this focused object like

.getProperty(".text"); or .getProperty(".name");

The reason I Need this is because I would like to write a testscript to test the Focus order in our Website.

Thank you in advance,

Krisz

Upvotes: 1

Views: 760

Answers (2)

Jonathan Thompson
Jonathan Thompson

Reputation: 11

I'd do a search on objects whose ".hasFocus" property is set to "true". From there, you could just run the method in a loop to check that the currently focused element is the one you want. I'd also personally recommend (if possible) to check against the ".id" property, since for a given (web)page, that's guaranteed to be a unique identifier... whereas I'm not entirely sure the ".name" property is.

public void testMain(Object[] args) {
    ArrayList<String> focusOrder = new ArrayList<String>();
    String currentlyFocusedObjectName = "";

    // Add element names to the list
    focusOrder.add("Object1");
    focusOrder.add("Object2");
    // ...
    focusOrder.add("Objectn");

    // Iterate through the list, tabbing and checking ".name" property each time
    for (String s: focusOrder) {
        TestObject currentObject = getCurrentlyFocusedElement();

        // Tab
        getScreen().inputKeys("{TAB}");

        if (currentObject != null) {
            currentlyFocusedObjectName = getCurrentlyFocusedElement().getProperty(".name")
                .toString();

            // Do other stuff with the object
        }
        else {
            currentlyFocusedObjectName = "";
        }

        // Verify that the currently focused object matches the current iteration in the list.
        vpManual(s + "HasFocus", currentlyFocusedObjectName, s).performTest();
    }
}

private TestObject getCurrentlyFocusedElement() {

    RootTestObject root = RootTestObject.getRootTestObject();
    TestObject[] focusedObjects = root.find(atProperty(".hasFocus", "true");
    TestObject currentlyFocusedObject = null;

    // Check to ensure that an object was found
    if (focusedObjects.length > 0) {
        currentlyFocusedObject = focusedObjects[0];
    }
    else {
        unregister(focusedObjects);
        return null;
    }

    // Clean up the object
    unregister(focusedObjects);

    return currentlyFocusedObject;
}

Upvotes: 0

Cagin Uludamar
Cagin Uludamar

Reputation: 476

You can do this using a simple method like

private void hasFocus(TestObject to) {
    boolean hasFocus = ((Boolean)to.getProperty(".hasFocus")).booleanValue();
    if (!hasFocus)
        throw new RuntimeException(to.getNameInScript()+" has an invalid focus order!");
}

and calling this method after each TAB press; giving the test object expected to gain focus as the parameter. Sample script code:

    browser_htmlBrowser().inputKeys("{TAB}");
    hasFocus(firstObj());

    browser_htmlBrowser().inputKeys("{TAB}");
    hasFocus(secondObj());

Upvotes: 0

Related Questions