Selenium webdriver(6) ------基础篇

之前掌握的技术已经可以让我们对 zTree 的很多基本功能进行测试了,但还有个大问题没办法解决就是 编辑状态下 hover 和 拖拽,想搞定这些就要搞定如何移动鼠标。


【1、如何移动鼠标】

行为操作需要用到 org.openqa.selenium.interactions.Action ;移动鼠标这里面提供了2个实现类:MoveMouseAction 和 MoveToOffsetAction;后者比前者多了2个参数(x,y)。

做这个测试时发现了一个很严重的问题:只有在 Chrome 上是正常的,对于 IE8 和 FireFox 都不能很正常的进行此功能测试的。

(补充:目前是XP 的机器,所以使用的是 IE8 )


  • 在 FireFox 上 MoveToOffsetAction 让浏览器模拟出鼠标移动到指定元素上的事件,这样导致的结果是只有 mouseover 会被触发;而 mouseout 就找不到喽,即使你把 x、y 设置的很大 或者 设置为负值,你捕获到的事件都是这个元素有鼠标移入,但绝对不会移出。
  • 在 IE 上 执行 action 后事件成功触发,但可能由于真实鼠标并不在指定位置,从而导致立刻又触发了 mouseout 事件,会发现按钮一闪而过


用 zTree 高级增删改查的 Demo 来做测试:

1、让鼠标移动到第一个根节点

   Chrome、FireFox:你会看到 编辑、删除按钮出现了。

   IE8:按钮一闪即逝。


2、然后把 x、y 设置为负值,继续移动

   Chrome:按钮消失

   IE8:从上一步消失后,就再没有出现过,也没有出现一闪而过的现象。

   FireFox:你会发现 按钮还在。


3、让鼠标移动到第二个根节点

   Chrome:第二个节点的按钮显示、第一个节点的按钮消失

   IE8:按钮一闪即逝。

   FireFox:你会看到第一个节点的 按钮消失了,这不是 mouseout 的作用,是 zTree 内部的功能,当有新的 hover 事件后,会让之前添加的 hover 对象删除


4、让鼠标移动到 树 ul 对象,把 x、y 设置为第一个根节点的位置

   Chrome:第一个根节点的按钮显示,第二个根节点的按钮消失

   IE8:按钮一闪即逝。

   FireFox:你会发现界面上没有任何变化,依然显示这第二个根节点的按钮


5、利用 MoveMouseAction 让鼠标移动到 第三个根节点

   Chrome、FireFox:会看到第三个根节点的 编辑、删除按钮出现

   IE8:会看到第三个根节点前面的所有节点依次出现编辑、删除按钮,最后到了第三个根节点停止,他的编辑、删除按钮依旧是一闪即逝


看来用这个工具还是多用 chrome 来测试吧,反正实际工作中基本上 Chrome 没有问题的话,FireFox 也没啥问题的。


因为是为了测试功能,专门把设置等待的代码提取出来做成一个工具:

 

Java代码   收藏代码
  1. package util;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.support.ui.ExpectedCondition;  
  5. import org.openqa.selenium.support.ui.WebDriverWait;  
  6.   
  7. public class Common {  
  8.       
  9.     public static void waitFor(int second, WebDriver driver) {  
  10.         // 等待 5 秒  
  11.         try {  
  12.             (new WebDriverWait(driver, second, 1000)).until(new ExpectedCondition<Boolean>() {  
  13.                 public Boolean apply(WebDriver d) {  
  14.                     return false;  
  15.                 }  
  16.             });  
  17.               
  18.         } catch(Exception e) {}  
  19.     }  
  20.   
  21. }  
 

以下是测试代码:


Java代码   收藏代码
  1. package lesson06;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.junit.AfterClass;  
  8. import org.junit.BeforeClass;  
  9. import org.junit.Test;  
  10. import org.openqa.selenium.HasInputDevices;  
  11. import org.openqa.selenium.JavascriptExecutor;  
  12. import org.openqa.selenium.Mouse;  
  13. import org.openqa.selenium.WebDriver;  
  14. import org.openqa.selenium.WebElement;  
  15. import org.openqa.selenium.chrome.ChromeDriver;  
  16. import org.openqa.selenium.firefox.FirefoxDriver;  
  17. import org.openqa.selenium.ie.InternetExplorerDriver;  
  18. import org.openqa.selenium.interactions.MoveMouseAction;  
  19. import org.openqa.selenium.interactions.MoveToOffsetAction;  
  20. import org.openqa.selenium.internal.Locatable;  
  21. import org.openqa.selenium.support.ui.ExpectedCondition;  
  22. import org.openqa.selenium.support.ui.WebDriverWait;  
  23.   
  24. import util.Common;  
  25.   
  26. public class ExampleForMoveMouse  {  
  27.       
  28.     static WebDriver driver;  
  29.       
  30.     @BeforeClass  
  31.     public static void init() {  
  32.         System.out.println("init...");  
  33.         //用 Chrome  
  34.         System.setProperty(  
  35.                 "webdriver.chrome.driver",  
  36.                 "E:\\BaiduWangPan\\百度网盘\\javascript\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe");  
  37.         driver = new ChromeDriver();  
  38.           
  39.         //用 IE  
  40. //      driver = new InternetExplorerDriver();  
  41.           
  42.         //用 FireFox  
  43. //      System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  44. //      // 创建一个 FireFox 的浏览器实例  
  45. //      driver = new FirefoxDriver();  
  46.     }  
  47.       
  48.     @Test  
  49.     public void test() {  
  50.         // 让浏览器访问 zTree Demo  
  51.         driver.get("http://www.ztree.me/v3/demo/cn/exedit/edit_super.html");  
  52.           
  53.         // 等待 zTree 初始化完毕,Timeout 设置10秒  
  54.         try {  
  55.             (new WebDriverWait(driver, 10500)).until(new ExpectedCondition<Boolean>() {  
  56.                 public Boolean apply(WebDriver d) {  
  57.                     WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo li').get(0);");  
  58.                     return element != null;  
  59.                 }  
  60.             });  
  61.               
  62.         } catch(Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.           
  66.         //找到第一个根节点  
  67.         ((JavascriptExecutor)driver).executeScript("window.zTreeObj = $.fn.zTree.getZTreeObj('treeDemo');"  
  68.                 + "window.zTreeNode = window.zTreeObj.getNodes()[0];");  
  69.           
  70.         //获取 节点对象   
  71.         WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");  
  72.         MoveToOffsetAction action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 105);  
  73.         action.perform();  
  74.         System.out.println("move to node1: " + 10 + ", " + 5);  
  75.           
  76.         // 等待 5 秒  
  77.         Common.waitFor(5, driver);  
  78.           
  79.         action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, -10, -15);  
  80.         action.perform();  
  81.         System.out.println("move to node1: " + (-10) + ", " + (-15));  
  82.         // 等待 5 秒  
  83.         Common.waitFor(5, driver);  
  84.   
  85.         //获取第二个根节点   
  86.         ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeObj.getNodes()[1];");  
  87.         element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");  
  88.         action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 105);  
  89.         action.perform();  
  90.         System.out.println("move to node2: " + (10) + ", " + (5));  
  91.           
  92.         // 等待 5 秒  
  93.         Common.waitFor(5, driver);  
  94.           
  95.         //获取zTree Obj   
  96.         element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo').get(0)");  
  97.         action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 4015);  
  98.         action.perform();  
  99.         System.out.println("move to treeDom: " + (40) + ", " + (15));  
  100.           
  101.         // 等待 5 秒  
  102.         Common.waitFor(5, driver);  
  103.           
  104.         //测试 MoveMouseAction  
  105.         //获取第三个根节点   
  106.         ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeObj.getNodes()[2];");  
  107.         element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");  
  108.         MoveMouseAction action2 = new MoveMouseAction( ((HasInputDevices) driver).getMouse(), (Locatable)element);  
  109.         action2.perform();  
  110.         System.out.println("move to node3: " + (10) + ", " + (5));  
  111.           
  112.         // 等待 5 秒  
  113.         Common.waitFor(5, driver);  
  114.           
  115.     }  
  116.       
  117.     @AfterClass  
  118.     public static void destory() {  
  119.         System.out.println("destory...");  
  120.         //关闭浏览器  
  121.         driver.quit();  
  122.     }  
  123. }  
 


今天时间不够了,明天再研究拖拽吧....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值