Selenium鼠标移动技巧

如何移动鼠标:

行为操作需要用到 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 也没啥问题的。

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

package util;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Common {
 
 public static void waitFor(int second, WebDriver driver) {
  // 等待 5 秒
     try {
      (new WebDriverWait(driver, second, 1000)).until(new ExpectedCondition<Boolean>() {
       public Boolean apply(WebDriver d) {
        return false;
       }
      });
      
     } catch(Exception e) {}
 }
}

以下是测试代码: 
package lesson06;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.HasInputDevices;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Mouse;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.MoveMouseAction;
import org.openqa.selenium.interactions.MoveToOffsetAction;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import util.Common;
public class ExampleForMoveMouse  {
 
    static WebDriver driver;
    
    @BeforeClass
    public static void init() {
     System.out.println("init...");
     //用 Chrome
     System.setProperty(
    "webdriver.chrome.driver",
    "E:\\BaiduWangPan\\百度网盘\\javascript\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe");
     driver = new ChromeDriver();
     
     //用 IE
//     driver = new InternetExplorerDriver();
     
     //用 FireFox
//     System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
//     // 创建一个 FireFox 的浏览器实例
//     driver = new FirefoxDriver();
    }
 
    @Test
    public void test() {
     // 让浏览器访问 zTree Demo
     driver.get("http://www.ztree.me/v3/demo/cn/exedit/edit_super.html");
     
     // 等待 zTree 初始化完毕,Timeout 设置10秒
     try {
      (new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition<Boolean>() {
       public Boolean apply(WebDriver d) {
        WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo li').get(0);");
        return element != null;
       }
      });
      
     } catch(Exception e) {
      e.printStackTrace();
     }
     
     //找到第一个根节点
     ((JavascriptExecutor)driver).executeScript("window.zTreeObj = $.fn.zTree.getZTreeObj('treeDemo');"
       + "window.zTreeNode = window.zTreeObj.getNodes()[0];");
     
     //获取 节点对象 
     WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");
     MoveToOffsetAction action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 10, 5);
     action.perform();
     System.out.println("move to node1: " + 10 + ", " + 5);
     
     // 等待 5 秒
     Common.waitFor(5, driver);
     
     action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, -10, -15);
     action.perform();
     System.out.println("move to node1: " + (-10) + ", " + (-15));
     // 等待 5 秒
     Common.waitFor(5, driver);
     //获取第二个根节点 
     ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeObj.getNodes()[1];");
     element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");
     action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 10, 5);
     action.perform();
     System.out.println("move to node2: " + (10) + ", " + (5));
     
     // 等待 5 秒
     Common.waitFor(5, driver);
     
     //获取zTree Obj 
     element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo').get(0)");
     action = new MoveToOffsetAction( ((HasInputDevices) driver).getMouse(), (Locatable)element, 40, 15);
     action.perform();
     System.out.println("move to treeDom: " + (40) + ", " + (15));
     
     // 等待 5 秒
     Common.waitFor(5, driver);
     
     //测试 MoveMouseAction
     //获取第三个根节点 
     ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeObj.getNodes()[2];");
     element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNode.tId + '_a').get(0)");
     MoveMouseAction action2 = new MoveMouseAction( ((HasInputDevices) driver).getMouse(), (Locatable)element);
     action2.perform();
     System.out.println("move to node3: " + (10) + ", " + (5));
     
     // 等待 5 秒
     Common.waitFor(5, driver);
     
    }
    
    @AfterClass
    public static void destory() {
     System.out.println("destory...");
     //关闭浏览器
     driver.quit();
    }
}

 

转载于:https://my.oschina.net/jimmylee216/blog/827884

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值