清单 2. 将父对象解析为应用程序域
/* Parse the Top Level Test Object to the corresponding test object type based * on the domain name that was set */
TestObject myDomain;
if (myDomainName.equalsIgnoreCase("Html.HtmlBrowser")) { myDomain = (BrowserTestObject) returnTO(".class", "Html.HtmlBrowser"); } else if (myDomainName.equalsIgnoreCase("Html.Dialog")) { myDomain = (TopLevelTestObject) returnTO(".class", "Html.Dialog"); } else if (myDomainName.equalsIgnoreCase("javax.swing.JDialog")) { myDomain = (TopLevelTestObject) returnTO(".class","javax.swing.JDialog"); } else if (myDomainName.equalsIgnoreCase("javax.swing.JFrame")) { myDomain = (TopLevelTestObject) returnTO(".class", "javax.swing.JFrame"); } else if (myDomainName.equalsIgnoreCase ("com.ibm.retail.rma.res.ui.config.MasterAgentInfoDlg")) { myDomain = (TopLevelTestObject) returnTO(".class", "com.ibm.retail.rma.res.ui.config.MasterAgentInfoDlg"); }
/* This function returns the 1st instance of a test object based on the * recognition property and the property value provided. If the test object * is not found null will be returned. */ public TestObject returnTO(String recognitionProperty, String recognitionPropertyValue,) { RootTestObject rTO = getRootTestObject(); TestObject[] TOs = rTO.find(atDescendant(recognitionProperty, propValue)); TestObject returnTO = null; int numOfObjects = TOs.length; if (numOfObjects >= 1) returnTO = TOs[0]; return returnTO; } |
在将顶层测试对象解析为正确的父测试对象类型后,使用 find() API 将有助于获得该对象下面的子对象的引用。然后,您可以编写一个通用的可重用函数,该函数将以跨域的方式工作。清单 3 中的代码显示一个示例,其中定义了通用 setText 函数,无论应用程序域是 HTML、Java、.NET,还是基于当前图形化用户界面技术的任何其他域,该函数均有效。
清单 3. 通用的跨域 setText() 函数
public boolean setTextGuiTestObject(TestObject myDomain, String recognitionProperty, String recognitionPropertyValue, String valueToSet) { boolean status = true; try { TestObject[] TOs = null; TOs = myDomain.find(atDescendant (recognitionProperty, recognitionPropertyValue)); int numOfObjects = TOs.length;
if (numOfObjects >= 1) { TextGuiTestObject myGTO1 = null; myGTO1 = (TextGuiTestObject) TOs[0]; myGTO1.waitForExistence(2500, 0.10); if (myGTO1.exists()) { System.out.println("Setting Text: " + valueToSet); myGTO1.click(); myGTO1.setText(text); } } myGTO1.unregister(); } catch (Exception e) { System.out.println("Unable to set Text :: " + e.toString()); status = false; } return status; } |
在本例中,该函数没有绑定到任何应用程序域。相反,在将值设置到 Text Box 之前,应用程序域作为一个输入参数被传递给该函数。因此,只要提供了正确的域、标识属性以及要设置的值,自动化脚本就能够在任何域的编辑框中设置文本。