RFT与Spring结合进行自动化脚本开发(2)

pkg/logon-map中使用的另一个类是pkg.MenuHelper,用来选择菜单(就是左边frame中的,超链接组成的菜单树),这个类暴露了parentTestObject 和 parentTestObject,通过spring 配置文件决定传入的菜单名称(必须按照菜单先后顺序),parentTestObject决定了从什么对象开始查找菜单,代码如下:

 

java 代码

 

  1. package pkg;   
  2.   
  3.   
  4. import java.util.HashMap;   
  5. import java.util.ArrayList;   
  6.   
  7. import com.rational.test.ft.ObjectNotFoundException;   
  8. import com.rational.test.ft.object.interfaces.GuiTestObject;   
  9. import com.rational.test.ft.object.interfaces.TestObject;   
  10. import com.rational.test.ft.script.RationalTestScript;   
  11. import com.rational.test.ft.script.SubitemFactory;   
  12.   
  13. public class MenuHelper    
  14. {   
  15.     public MenuHelper()   
  16.     {   
  17.     }   
  18.        
  19.     /*  
  20.      * @parentObject 菜单树的直接父框架(菜单指的是由超连接组成的菜单)  
  21.      */  
  22.     public MenuHelper(TestObject parentObject, String menus[])   
  23.     {   
  24.         map = new HashMap();   
  25.         parentTestObject = parentObject;   
  26.         this.menus = menus;   
  27.     }   
  28.        
  29.     /*  
  30.      * 点击菜单  
  31.      */  
  32.     public void performAction()   
  33.     {   
  34.         for(int i = 0; i < menus.length; i++)   
  35.         {   
  36.             selectMenu(menus[i]);   
  37.         }   
  38.     }   
  39.        
  40.     /*  
  41.      * @Task: 选择指定的对象并点击  
  42.      * @menu: 菜单文本名称  
  43.      */  
  44.     public void selectMenu(String menu)   
  45.     {   
  46.         findTestObjectInBrowser(menu);   
  47.     }   
  48.        
  49.        
  50.     /**  
  51.      * @return 返回 menus。  
  52.      */  
  53.     public String[] getMenus()    
  54.     {   
  55.         return menus;   
  56.     }   
  57.        
  58.     /**  
  59.      * @param menus 要设置的 menus。  
  60.      */  
  61.     public void setMenus(String[] menus)    
  62.     {   
  63.         this.menus = menus;   
  64.     }   
  65.        
  66.     /**  
  67.      * @return 返回 parentTestObject。  
  68.      */  
  69.     public TestObject getParentTestObject()    
  70.     {   
  71.         return parentTestObject;   
  72.     }   
  73.        
  74.     /**  
  75.      * @param parentTestObject 要设置的 parentTestObject。  
  76.      */  
  77.     public void setParentTestObject(TestObject parentTestObject)    
  78.     {   
  79.         this.parentTestObject = parentTestObject;   
  80.     }   
  81.        
  82.     /*  
  83.      * @Task: 查找Link对象并点击 ,此方法会抛出ObjectNotFoundException  
  84.      * @text: Link对象文本名称  
  85.      */  
  86.     private void findTestObjectInBrowser(String text)   
  87.     {              
  88.         TestObject[] foundTOs = findObject(text);   
  89.         if(foundTOs.length == 0)   
  90.         {   
  91.             throw new ObjectNotFoundException("Not Found " + text + " Menu");   
  92.         }   
  93.         else if(foundTOs.length == 1)   
  94.         {   
  95.             new GuiTestObject((foundTOs[0])).doubleClick();   
  96.         }   
  97.         else if(foundTOs.length > 1)    
  98.         {   
  99.             if(map.get(text) == null)   
  100.             {   
  101.                 int index = 0;   
  102.                 ArrayList array = new ArrayList();   
  103.                 for(int i = 0; i < foundTOs.length; i++)   
  104.                 {   
  105.                     try  
  106.                     {   
  107.                         new GuiTestObject((foundTOs[i])).doubleClick();   
  108.                         index = i + 1;   
  109.                         break;   
  110.                     }   
  111.                     catch(Exception e)   
  112.                     {   
  113.                         array.add(foundTOs[i]);   
  114.                     }   
  115.                 }   
  116.                    
  117.                 for(; index < foundTOs.length; index++)   
  118.                 {   
  119.                     array.add(foundTOs[index]);   
  120.                 }   
  121.                    
  122.                 map.put(text, array);   
  123.             }   
  124.             else  
  125.             {   
  126.                 ArrayList array = (ArrayList)map.get(text);   
  127.                 for(int i = 0; i < array.size(); i++)   
  128.                 {   
  129.                     try  
  130.                     {   
  131.                         new GuiTestObject((TestObject)array.get(i)).doubleClick();   
  132.                         array.remove(i);   
  133.                         break;   
  134.                     }   
  135.                     catch(Exception e)   
  136.                     {   
  137.                            
  138.                     }   
  139.                 }   
  140.                    
  141.                 if(array.isEmpty())   
  142.                 {   
  143.                     map.remove(text);   
  144.                 }   
  145.             }   
  146.                
  147.         }      
  148.     }   
  149.        
  150.     /*  
  151.      * @Task: 查找超连接对象  
  152.      */  
  153.     private TestObject[] findObject(String text)   
  154.     {   
  155.         String property1=".text";   
  156.         String value1=text;   
  157.         String property2=".class";   
  158.         String value2="Html.A";   
  159.                    
  160.         TestObject[] foundTOs = parentTestObject.find(SubitemFactory.atDescendant(property2 , value2 , property1 , value1));   
  161.         return foundTOs;   
  162.     }   
  163.        
  164.     private HashMap map;   
  165.     private TestObject parentTestObject;   
  166.     private String[] menus;   
  167. }   

下面是如何使用spring配置文件中的对象:

java 代码
java 代码
  1. package pkg;   
  2.   
  3. import resources.pkg.LogonHelper;   
  4.   
  5. import com.rational.test.ft.*;   
  6. import com.rational.test.ft.object.interfaces.*;   
  7. import com.rational.test.ft.object.interfaces.siebel.*;   
  8. import com.rational.test.ft.script.*;   
  9. import com.rational.test.ft.value.*;   
  10. import com.rational.test.ft.vp.*;   
  11.   
  12.   
  13. import org.springframework.beans.factory.BeanFactory;   
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  15. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  16.   
  17.   
  18.   
  19. public class Logon extends LogonHelper   
  20. {   
  21.     public void testMain(Object[] args)    
  22.     {   
  23.            
  24.         String path = "pkg/logon-map.xml";   
  25.         BeanFactory context = SpringContextGenerator.getApplicationContext(path);   
  26.            
  27.         //通过RFT中的启动项,启动IE   
  28.         startApp("7001");   
  29.            
  30.         BrowserTestObject browser = (BrowserTestObject)context.getBean("browserTestObject");    
  31.         browser.maximize();   
  32.            
  33.         TextGuiTestObject text_userName = new TextGuiTestObject((TestObject)context.getBean("userNameText"));   
  34.         text_userName.setText("system");   
  35.            
  36.         TextGuiTestObject text_password = new TextGuiTestObject((TestObject)context.getBean("passwordText"));   
  37.         text_password.setText("123");   
  38.            
  39.         GuiTestObject submit_button = new GuiTestObject((TestObject)context.getBean("submitButton"));   
  40.         submit_button.click();   
  41.            
  42.         sleep(10);   
  43.            
  44.         MenuHelper mh = (MenuHelper)context.getBean("menuHelper");   
  45.         mh.performAction();   
  46.     }   
  47. }   
  48.   

你可能感兴趣的:(spring,xml,框架,脚本,IE)