RFT Best Practices - 3. 對象查找

          如何靈活查找窗體內的對象呢?和查找窗體類似,可以使用這些對象特有的屬性,依照一定的查找邏輯進行處理。下面是一個典型的查找方法,以此為例進行說明:

              public TestObject getObject(ArrayList<Property> v) {

                  rootTO.waitForExistence(waitMaxTime, waitCheckInterval);
                  TestObject returnObject 
          = null;
                  TestObject to[] 
          = null;
                  
          double timeNow = System.currentTimeMillis() / 1000;
                  
          double endTime = timeNow + waitMaxTime;
                  v.add(
          new Property("showing""true"));
                  
          while (returnObject == null && timeNow < endTime) {
                      to 
          = rootTO.find(atDescendant((Property[]) v.toArray(new Property[0])));
                      
          if (to.length > 1{
                          
           throw new AmbiguousRecognitionException("Find more than one object.");
                      }

                      
          if (to.length == 1{
                          returnObject 
          = to[0];
                      }
           else
                          sleep(waitCheckInterval);
                      timeNow 
          = System.currentTimeMillis() / 1000;
                  }

                  
          return returnObject;
              }

          上面的方法根據傳入的參數集合對當前窗口中的所有對象進行查找。和之前的窗體查找一樣,最好顯示的添加showing=true參數,因為在Swing程序的運行過程中,內存中會對GUI元素進行緩存,可能一個界面消失了,但它還在內存中,等待著隨后被顯示。這樣一來,就需要這個參數過濾到所有未被顯示的GUI元素。在實際使用過程中,可以使用如下的方法進行調用: (調用前使用RFT的對象查看器確定待查找對象的唯一屬性)

          protected WButton getButton(String name) {
                  ArrayList
          <Property> v = new ArrayList<Property>();
                  v.add(
          new Property(".class""javax.swing.JButton"));
                  v.add(
          new Property("accessibleContext.accessibleName", name));
                  TestObject to 
          = og.getObject(v);
                  
          if (!Utility.exists(to))
                      
          throw new ObjectNotFoundException();
                  
          else
                      
          return new WButton(to);
              }
           

          與窗口處理一樣,如果某些參數需要使用正則表達式處理,可以使用下面的方法:

              protected WListBox getList(String label) {
                  RegularExpression exp 
          = new RegularExpression(".*JComboBox$|.*JList$"false);
                  ArrayList
          <Property> v = new ArrayList<Property>();
                  v.add(
          new Property(".class", exp));
                  v.add(
          new Property(".priorLabel", label));
                  TestObject to 
          = og.getObject(v);
                  
          if (!Utility.exists(to))
                      
          throw new ObjectNotFoundException();
                  
          else
                      
          return new WListBox(to);
              }
           

          在對象查找過程中,可能需要各種不同的查找邏輯。例如,如果對象可能存在也可能不存在,在查找的時候就不需要等待并反復查找,這時候,可以使用如下的方法:

              public TestObject getObjectWithoutWait(ArrayList<Property> v) {
                  rootTO.waitForExistence();
                  TestObject returnObject 
          = null;
                  v.add(
          new Property("showing""true"));
                  TestObject to[] 
          = rootTO.find(atDescendant((Property[]) v.toArray(new Property[0])));
                  
          if (to.length > 1{
                      
          throw new AmbiguousRecognitionException(
                              Find more than one object.);
                  }

                  
          if (to.length == 1{
                      returnObject 
          = to[0];
                  }

                  
          return returnObject;
              }

           

          有時候,界面上有多個具有相同屬性的對象,只能通過他們的編號來區分他們;有時候需要以某個確定對象為根來進行查找;有時候需要查找直接子對象而不是所有子對象,等等。并且,這些邏輯之間也存在排列組合的情況,實際使用中可以根據自身需要靈活處理。這些方法都是對上面基本方法的擴展,大家可以嘗試自己來實現。

          posted on 2009-03-31 09:31 terrypang 閱讀(1191) 評論(0)  編輯  收藏 所屬分類: RFT Best Practices

          導航

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蒙城县| 葫芦岛市| 祁连县| 壤塘县| 潜江市| 阳高县| 宁乡县| 华宁县| 镶黄旗| 巫溪县| 桃园县| 凤庆县| 新河县| 开封县| 泾川县| 洪洞县| 莫力| 库尔勒市| 吐鲁番市| 巩留县| 黎平县| 梧州市| 荣成市| 双鸭山市| 阿勒泰市| 阿合奇县| 商丘市| 金华市| 贺兰县| 南川市| 柳林县| 宣恩县| 桓仁| 正蓝旗| 咸宁市| 晴隆县| 精河县| 乌拉特中旗| 垦利县| 沈丘县| 自治县|