昨天我们已经可以轻松移动鼠标了,距离拖拽只有一步之遥。 其实这就是一层窗户纸,捅破它就搞定了,之前做的操作可以说都是单步操作:移动鼠标、点击页面元素、弹出窗口等等;而拖拽操作就不行了,他需要一连串连贯的动作配合起来:mousedown、mousemove、mouseup,缺了哪个都不行,顺序不对也不行。
【1、如何进行拖拽】
这时候我们就需要用到 org.openqa.selenium.interactions.Actions 这个类了,它专门用来做动作组合的。 Actions 中有若干方法,可以让你很容易的生成 鼠标、按键的操作集合。
例如: clickAndHold + moveToElement + release 就可以组合成一套拖拽的操作;
详细内容还请查看 Selenium 的 javadoc:http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html
生成操作组合后,利用 build 方法可以得到一个有效的 Action 对象;最后使用 perform 方法执行就可以了。
和昨天测试鼠标移动的情况类似,还是 FireFox 问题最大, IE8有小问题, Chrome 测试最正常。
FireFox:使用 moveToElement 方法时,效果同昨天使用 MoveToOffsetAction 情况类似,xOffset、yOffset值无论如何设置,在页面上得到的都是 指定的 页面元素;
另外,如果在不使用 moveToElement的时候就使用moveByOffset 很容易报错:org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view: (WARNING: The server did not provide any stacktrace information)
IE8: 使用 moveToElement 方法时,如果用到了 xOffset、yOffset 参数,你会发现在 IE8中 计算的情况 和 Chrome 上不太一样,貌似范围会更大一些,因此导致如果设置为0, 0 时,就不是你预期的结果了
测试代码我分成了3个部分:
可以专门用来观察 moveToElement 在不同浏览器下的情况
可以专门用来观察 moveByOffset 在不同浏览器下的情况,FireFox 会报错
可以专门用来观察 多种组合操作 在 不同浏览器下的情况
总之,对于鼠标移动和拖拽的测试还是直接在 Chrome 下进行就可以了吧;ie的只能略微参考;剩下的还是自己手动来吧。。。。
如果想在 IE 上正常测试,建议采用moveToElement(WebElement)+ moveByOffset(xOffset, yOffset); 避免直接使用 moveToElement(WebElement, xOffset, yOffset),同时还是要严格注意 xOffset 和 yOffset 的设置;这个需要根据自己的实际情况来调试了。
学习了这些内容以后,对于 测试 zTree 这类前端 js 插件来说就足够了,剩下的就努力干活儿吧。 貌似我是真用不上 Selenium 的 webdriver 了。。。
以下是测试代码:
package lesson07;
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.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.Action;
import org.openqa.selenium.interactions.Actions;
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 ExampleForDrag {
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/drag.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.zTreeNodeSrc = window.zTreeObj.getNodes()[0].children[0];");
//获取 需要拖拽的节点对象
WebElement elementSrc = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#' + window.zTreeNodeSrc.tId + '_a').get(0)");
//获取 目标节点对象
WebElement elementTarget = (WebElement) ((JavascriptExecutor)driver).executeScript("window.zTreeNodeTarget = window.zTreeNodeSrc.getNextNode().children[0]; return $('#' + window.zTreeNodeTarget.tId + '_a').get(0)");
Actions actions = new Actions(driver);
Action action;
//观察反复拖拽测试 1
// actions.clickAndHold(elementSrc);
// for (int i=0; i<500; i++) {
// actions.moveToElement(elementTarget, i%100-50, i%50-20);
// }
// actions.release();
// action = actions.build();
// action.perform();
//
// Common.waitFor(10, driver);
//观察反复拖拽测试 2
// actions.clickAndHold(elementSrc).moveToElement(elementTarget);
// int x = 0, y = 0, dx=2, dy=2;
// for (int i=0; i<500; i++) {
// x+=2; y+=2;
// if (x > 50) {
// dx = -x;
// x = 0;
// } else {
// dx = 2;
// }
// if (y > 150) {
// dy = -y;
// y = 0;
// } else {
// dy = 2;
// }
// actions.moveByOffset(dx, dy);
// }
// actions.release();
// action = actions.build();
// action.perform();
// Common.waitFor(10, driver);
//观察系列操作测试
System.out.println("移动成为目标节点的 前一个节点");
actions.clickAndHold(elementSrc).moveToElement(elementTarget, 60, 1).release();
action = actions.build();
action.perform();
// 等待 10 秒
Common.waitFor(10, driver);
System.out.println("移动成为目标节点的后一个节点");
actions.clickAndHold(elementSrc).moveToElement(elementTarget, 60, 38).release();
action = actions.build();
action.perform();
// 等待 10秒
Common.waitFor(10, driver);
System.out.println("移动成为目标节点的子节点");
actions.clickAndHold(elementSrc).moveToElement(elementTarget).release();
action = actions.build();
action.perform();
// 等待 10秒
Common.waitFor(10, driver);
System.out.println("移动成为目标节点下一个节点的子节点");
actions.clickAndHold(elementSrc).moveToElement(elementTarget).moveByOffset(0, 20).release();
action = actions.build();
action.perform();
// 等待 10秒
Common.waitFor(10, driver);
}
@AfterClass
public static void destory() {
System.out.println("destory...");
//关闭浏览器
driver.quit();
}
}
分享到:
相关推荐
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
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源码案例,全是案例适合小白入门学习
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 webdriver学习笔记C#版
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章
selenium webdriver2 环境搭建