---
title: "和我一起学 Selenium WebDriver（6）——基础篇"
author: "zTreeAPI"
date: "2012-12-19T17:47:55Z"
tags: []
categories: ["Web前端"]
slug: "selenium-webdriver-6"
draft: false
lastmod: "2012-12-19T17:47:55Z"
id: 1750554
url: "https://ztreeapi.iteye.com/blog/1750554"
---

之前掌握的技术已经可以让我们对 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 也没啥问题的。

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

 

```
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();
    }
}
```

 

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