昨天咱們已經能夠輕鬆移動鼠標了,距離拖拽只有一步之遙。 其實這就是一層窗戶紙,捅破它就搞定了,以前作的操做能夠說都是單步操做:移動鼠標、點擊頁面元素、彈出窗口等等;而拖拽操做就不行了,他須要一連串連貫的動做配合起來:mousedown、mousemove、mouseup,缺了哪一個都不行,順序不對也不行。
【一、如何進行拖拽】
這時候咱們就須要用到 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();
}
}