51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3022|回复: 0
打印 上一主题 下一主题

selenium RC选择自动EXT的 Combox 控件

[复制链接]
  • TA的每日心情

    2015-4-9 17:10
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2012-2-16 13:00:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 jia8162 于 2012-2-16 13:39 编辑

    由于选项框是自动生成的
    如果选项一样还用Xpath text去定位的话会出现定位多个的情况发生(在运行的时候就只定位第一个实行)
    我的项目中等到的就碰到以下情况
    <div id="ext-gen259" class="x-layer x-combo-list" style="position: absolute; z-index: 11000; visibility: hidden; left: -10000px; top: -10000px; width: 148px; height: 42px;">
           <div id="ext-gen261" class="x-combo-list-inner" style="width: 148px; overflow: auto; height: 42px;">
                  <div class="x-combo-list-item x-combo-selected">CCCCC</div>
                  <div class="x-combo-list-item">AAAAA</div>
           </div>
    </div>
    <div id="ext-gen314" class="x-layer x-combo-list" style="position: absolute; z-index: 11000; visibility: visible; left: 76px; top: 249px; width: 148px; height: 84px;">
           <div id="ext-gen316" class="x-combo-list-inner" style="width: 148px; overflow: auto; height: 84px;">
                  <div class="x-combo-list-item x-combo-selected">CCCCC</div>
                  <div class="x-combo-list-item">BBBBB</div>
                  <div class="x-combo-list-item">DDDDD</div>
                  <div class="x-combo-list-item">AAAAA</div>
           </div>
    </div>
    从上面就可以看出 id、class、都不能用 唯一能用的就是 style
    但是 如果在IE中运行的话 style 是不能定位的
    那怎么办呢 还好我找到了
    getAttribute()的方法
    这个方法可以返回Xpath 属性的内容
    就可以判断是那个个下拉选项框显示就能定位到唯一的选项了
    一下是我的代码
    1.         //下拉框选择
    2.         private void objectSelectText(String objectXpath,String SelectText)
    3.         {
    4.                 String htmlLocator = "";
    5.                 try{
    6.                         ifElementTrue(objectXpath.split(";")[0]);
    7.                         selenium.click(objectXpath.split(";")[0]);
    8.                         //火狐
    9.                         if(this.getBrowserType().toLowerCase().equals("firefox3"))
    10.                         {
    11.                                 htmlLocator = "//div[contains(@style,'visibility: visible')]/" + objectXpath.split(";")[1] +"/div[text()='" + SelectText + "']";
    12.                         }
    13.                         else
    14.                         {
    15.                                 htmlLocator = ConXpach("//div" + "[text()='" + SelectText + "']");
    16.                         }
    17.                         //
    18.                         Thread.sleep(300);
    19.                         selenium.mouseOver(htmlLocator);
    20.                         Thread.sleep(300);
    21.                         selenium.mouseDown(htmlLocator);
    22.                         Thread.sleep(300);
    23.                         selenium.click(htmlLocator);
    24.                         this.logger.info(Handle + "---->" + SelectText + ":PASS");
    25.                         FailOrPass = "PASS";
    26.                         //ScreenShots();
    27.                 }catch(Exception e)
    28.                 {
    29.                 StackTrace = e.toString();
    30.                 FailOrPass = "FAIL";
    31.                 ScreenShots("err");
    32.                 this.logger.error("Exception",e);
    33.                 }
    34.         }
    35.         //IE下拉框判断
    36.         public String ConXpach(String Xpath)
    37.         {
    38.                 int sumConList = 0;
    39.                 String isXpath = "";
    40.                 String runhtmlLocator = "";
    41.                 String tmpStyle = "";
    42.                 do
    43.                 {
    44.                         sumConList++;
    45.                         isXpath = "//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(sumConList)+"]";
    46.                        
    47.                 }while(selenium.isElementPresent(isXpath));
    48.                 if(sumConList !=1 && sumConList > 0)
    49.                 {
    50.                         sumConList=sumConList-1;
    51.                 }
    52.                
    53.                 for(int index = 1 ; index<=sumConList;index++)
    54.                 {
    55.                         tmpStyle = selenium.getAttribute("//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(index)+"]/@style");
    56.                         if(tmpStyle.indexOf("visible")>=0)
    57.                         {
    58.                                 runhtmlLocator = "//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(index)+"]"+Xpath;
    59.                                 break;
    60.                         }
    61.                 }
    62.                
    63.                 return  runhtmlLocator;
    64.         }
    复制代码
    大家看多以后有什么更好的方法可以回复哦
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-21 17:01 , Processed in 0.066049 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表