Reputation: 83
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
I would like to know which component has the Focus
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
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
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