之前掌握的技术已经可以让我们对 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();
}
}
今天时间不够了,明天再研究拖拽吧....
分享到:
相关推荐
selenium RC与selenium webdriver的区别
关于Selenium WebDriver工作原理的介绍,原理相关描述
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
seleniumwebdriver(python) (第三版)
selenium webdriver基于python源码案例,全是案例适合小白入门学习
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
selenium2.0自动化
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
1.2 selenium webdriver学习(二)————对浏览器的简单操作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 selenium webdriver学习(三)------------执行js脚本 . . . . . . . . . . . . . ...
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
自动化测试:Selenium webdriver学习笔记C#版
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章
selenium webdriver2 环境搭建