如何通过脚本获取已添加到某个脚本中的测试对象?(已添加到脚本中的测试对象在脚本资源管理器中的测试对象节点下会列出来。)
下面的脚本将打印Login脚本中的测试对象的相关信息:
Vector vector = new Vector();
vector.addElement (new Login());
Vector testScript = vector;
for(int i = 0; i < testScript.size(); i++)
{
if(testScript.elementAt(i) instanceof RationalTestScript)
{
RationalTestScript ts = (RationalTestScript)testScript.elementAt(i);
IScriptDefinition sd = ts.getScriptDefinition();
Enumeration e = sd.getTestObjectNames();
while(e.hasMoreElements())
{
String currentObjName = (String)e.nextElement();
String role = sd.getRole(currentObjName);
String mapID = sd.getMapId(currentObjName);
String curClassName = (String)ts.getMap().find(mapID).getClassName();
String objClassName = (String)ts.getMap().find(mapID).getTestObjectClassName();
System.out.println(role);
System.out.println(mapID);
System.out.println(curClassName);
System.out.println(objClassName);
}
}
}
输出:
Button
B.JGKG3Ig8tvE:kFGtR:MFnGLmf:8WT
Html.INPUT.submit
GuiTestObject
Text
9.JGKG3Ig8tvE:kFGtR:MFnGLmf:8WU
Html.INPUT.text
TextGuiTestObject
Text
A.JGKG3Ig8tvE:kFGtR:MFnGLmf:8WU
Html.INPUT.password
TextGuiTestObject
其中使用getScriptDefinition 方法来获取IscriptDefinition类型的对象,也就是脚本相关的定义对象,其中就包含测试对象。
Persists the definition about the artifacts associated with the script as an object that implements this interface when the script is created or updated. Object-map information is maintained to improve the resilience of scripts relative to changes in shared object maps. Also, verification points and other assocatied artifacts are managed by the script definition interface.
再通过getTestObjectNames方法来获取测试对象的集合。
Returns an enumerator for the TestObject names associated with a script. Each object returned by the enumerator is a java.lang.String value known to be unique relative to a script.
对于测试对象集合中的每一个元素,通过getRole方法来获取测试对象的角色,通过getMapId来获取测试对象的映射ID。
通过getMap可返回与测试脚本关联的对象映射(object map),再根据MapID,通过find方法查找返回映射的测试对象,然后通过getClassName和getTestObjectClassName返回对象的类型以及测试对象类名。