1

我使用 RFT 并想知道如何获取焦点所在的对象并能够在之后使用该对象。例如,我的脚本比我写的开始 getScreen().inputKeys("{TAB}"),并且

  1. 我想知道哪个组件有焦点

  2. 在此之后,我想知道如何获得这个重点对象的属性,例如

.getProperty(".text"); 或者.getProperty(".name");

我需要这个的原因是因为我想编写一个测试脚本来测试我们网站中的焦点顺序。

先感谢您,

克里斯

4

2 回答 2

0

我会搜索“.hasFocus”属性设置为“true”的对象。从那里,您可以在循环中运行该方法以检查当前聚焦的元素是否是您想要的元素。我个人还建议(如果可能)检查“.id”属性,因为对于给定的(网页)页面,这保证是唯一标识符......而我不完全确定“.name” “财产是。

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;
}
于 2013-12-31T01:38:20.593 回答
0

您可以使用简单的方法来做到这一点,例如

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

并在每次按 TAB 后调用此方法;将预期获得焦点的测试对象作为参数。示例脚本代码:

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

    browser_htmlBrowser().inputKeys("{TAB}");
    hasFocus(secondObj());
于 2013-12-11T12:18:23.100 回答