WebDriver处理extjs树形菜单的例子

上一篇 / 下一篇  2012-12-28 23:20:08 / 个人分类:WebDriver

网页中经常会碰到 extjs的树形菜单,对于这种菜单,基本的处理思路就按照菜单树一层一层的定位元素,判断元素的class,如果存在 x-tree-node-leaf,说明是叶节点,直接点击,否则,如果存在 x-tree-node-collapsed ,说明已经展开,无需点击,否则需要做双击操作。下面是例子代码:

  1. import java.util.ArrayList;
  2. import java.util.concurrent.TimeUnit;
  3.  
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.ie.InternetExplorerDriver;
  8. import org.openqa.selenium.interactions.Actions;
  9. import org.testng.Reporter;
  10. import org.testng.annotations.Test;
  11.  
  12. public class TempTest {
  13.  
  14.         @Test()
  15.         public void test1() {
  16.                 WebDriver dr = new InternetExplorerDriver();
  17.                 dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  18.                 dr.get("http://extjs.org.cn/extjs/examples/tree/reorder.html");
  19.  
  20.                 for (int i = 0; i < 2; i++) {
  21.                         ArrayList<String> menus = new ArrayList<String>();
  22.                         menus.add("Ext JS");
  23.                         menus.add("locale");
  24.                         menus.add("ext-lang-zh_CN.js");
  25.                         ClickExtjsTree(dr, menus);
  26.                         try {
  27.                                 Thread.sleep(5000);
  28.                         } catch (InterruptedException e) {
  29.                                 e.printStackTrace();
  30.                         }
  31.                 }
  32.                 dr.quit();
  33.         }
  34.  
  35.         public void ClickExtjsTree(WebDriver dr, ArrayList<String> menus) {
  36.                 // 确定顶层元素
  37.                 WebElement we_treeview = dr.findElement(By
  38.                                 .xpath("//div[@id='tree-div']/div/div"));
  39.  
  40.                 // 遍历菜单
  41.                 for (String menu : menus) {
  42.                         Reporter.log("查找:'" + menu + "'", true);
  43.                         WebElement we_div = we_treeview.findElement(By
  44.                                         .xpath(".//span[text()='" + menu + "']/../.."));
  45.                         // 判断是不是叶节点
  46.                         if (we_div.getAttribute("class").contains("x-tree-node-leaf")) {
  47.                                 Reporter.log("'" + menu + "'是叶节点,单击", true);
  48.                                 we_treeview.findElement(
  49.                                                 By.xpath(".//span[text()='" + menu + "']")).click();
  50.                                 return;
  51.                         } else {
  52.                                 if (we_div
  53.                                                 .getAttribute("class")
  54.                                                 .contains(
  55.                                                                 "x-tree-node-collapsed")) {
  56.                                         Reporter.log("'" + menu + "'未展开,双击", true);
  57.                                         Actions action = new Actions(dr);
  58.                                         action.doubleClick(we_treeview.findElement(By
  59.                                                         .xpath(".//span[text()='" + menu + "']")));
  60.                                         action.perform();
  61.                                 }
  62.                                 else{
  63.                                         Reporter.log("'" + menu + "'已展开,不处理", true);
  64.                                 }
  65.                         }
  66.                 }
  67.         }
  68.  
  69. }

TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 17265
  • 日志数: 9
  • 建立时间: 2012-12-28
  • 更新时间: 2012-12-29

RSS订阅

Open Toolbar