pkg/logon-map中使用的另一个类是pkg.MenuHelper,用来选择菜单(就是左边frame中的,超链接组成的菜单树),这个类暴露了parentTestObject 和 parentTestObject,通过spring 配置文件决定传入的菜单名称(必须按照菜单先后顺序),parentTestObject决定了从什么对象开始查找菜单,代码如下:
java 代码
- package pkg;
- import java.util.HashMap;
- import java.util.ArrayList;
- import com.rational.test.ft.ObjectNotFoundException;
- import com.rational.test.ft.object.interfaces.GuiTestObject;
- import com.rational.test.ft.object.interfaces.TestObject;
- import com.rational.test.ft.script.RationalTestScript;
- import com.rational.test.ft.script.SubitemFactory;
- public class MenuHelper
- {
- public MenuHelper()
- {
- }
- /*
- * @parentObject 菜单树的直接父框架(菜单指的是由超连接组成的菜单)
- */
- public MenuHelper(TestObject parentObject, String menus[])
- {
- map = new HashMap();
- parentTestObject = parentObject;
- this.menus = menus;
- }
- /*
- * 点击菜单
- */
- public void performAction()
- {
- for(int i = 0; i < menus.length; i++)
- {
- selectMenu(menus[i]);
- }
- }
- /*
- * @Task: 选择指定的对象并点击
- * @menu: 菜单文本名称
- */
- public void selectMenu(String menu)
- {
- findTestObjectInBrowser(menu);
- }
- /**
- * @return 返回 menus。
- */
- public String[] getMenus()
- {
- return menus;
- }
- /**
- * @param menus 要设置的 menus。
- */
- public void setMenus(String[] menus)
- {
- this.menus = menus;
- }
- /**
- * @return 返回 parentTestObject。
- */
- public TestObject getParentTestObject()
- {
- return parentTestObject;
- }
- /**
- * @param parentTestObject 要设置的 parentTestObject。
- */
- public void setParentTestObject(TestObject parentTestObject)
- {
- this.parentTestObject = parentTestObject;
- }
- /*
- * @Task: 查找Link对象并点击 ,此方法会抛出ObjectNotFoundException
- * @text: Link对象文本名称
- */
- private void findTestObjectInBrowser(String text)
- {
- TestObject[] foundTOs = findObject(text);
- if(foundTOs.length == 0)
- {
- throw new ObjectNotFoundException("Not Found " + text + " Menu");
- }
- else if(foundTOs.length == 1)
- {
- new GuiTestObject((foundTOs[0])).doubleClick();
- }
- else if(foundTOs.length > 1)
- {
- if(map.get(text) == null)
- {
- int index = 0;
- ArrayList array = new ArrayList();
- for(int i = 0; i < foundTOs.length; i++)
- {
- try
- {
- new GuiTestObject((foundTOs[i])).doubleClick();
- index = i + 1;
- break;
- }
- catch(Exception e)
- {
- array.add(foundTOs[i]);
- }
- }
- for(; index < foundTOs.length; index++)
- {
- array.add(foundTOs[index]);
- }
- map.put(text, array);
- }
- else
- {
- ArrayList array = (ArrayList)map.get(text);
- for(int i = 0; i < array.size(); i++)
- {
- try
- {
- new GuiTestObject((TestObject)array.get(i)).doubleClick();
- array.remove(i);
- break;
- }
- catch(Exception e)
- {
- }
- }
- if(array.isEmpty())
- {
- map.remove(text);
- }
- }
- }
- }
- /*
- * @Task: 查找超连接对象
- */
- private TestObject[] findObject(String text)
- {
- String property1=".text";
- String value1=text;
- String property2=".class";
- String value2="Html.A";
- TestObject[] foundTOs = parentTestObject.find(SubitemFactory.atDescendant(property2 , value2 , property1 , value1));
- return foundTOs;
- }
- private HashMap map;
- private TestObject parentTestObject;
- private String[] menus;
- }
下面是如何使用spring配置文件中的对象:
java 代码
java 代码
- package pkg;
- import resources.pkg.LogonHelper;
- import com.rational.test.ft.*;
- import com.rational.test.ft.object.interfaces.*;
- import com.rational.test.ft.object.interfaces.siebel.*;
- import com.rational.test.ft.script.*;
- import com.rational.test.ft.value.*;
- import com.rational.test.ft.vp.*;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- public class Logon extends LogonHelper
- {
- public void testMain(Object[] args)
- {
- String path = "pkg/logon-map.xml";
- BeanFactory context = SpringContextGenerator.getApplicationContext(path);
- //通过RFT中的启动项,启动IE
- startApp("7001");
- BrowserTestObject browser = (BrowserTestObject)context.getBean("browserTestObject");
- browser.maximize();
- TextGuiTestObject text_userName = new TextGuiTestObject((TestObject)context.getBean("userNameText"));
- text_userName.setText("system");
- TextGuiTestObject text_password = new TextGuiTestObject((TestObject)context.getBean("passwordText"));
- text_password.setText("123");
- GuiTestObject submit_button = new GuiTestObject((TestObject)context.getBean("submitButton"));
- submit_button.click();
- sleep(10);
- MenuHelper mh = (MenuHelper)context.getBean("menuHelper");
- mh.performAction();
- }
- }