以前掌握的技術已經可讓咱們對 zTree 的不少基本功能進行測試了,但還有個大問題沒辦法解決就是 編輯狀態下 hover 和 拖拽,想搞定這些就要搞定如何移動鼠標。
【一、如何移動鼠標】
行爲操做須要用到 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 來作測試:
一、讓鼠標移動到第一個根節點
Chrome、FireFox:你會看到 編輯、刪除按鈕出現了。
IE8:按鈕一閃即逝。
二、而後把 x、y 設置爲負值,繼續移動
Chrome:按鈕消失
IE8:從上一步消失後,就再沒有出現過,也沒有出現一閃而過的現象。
FireFox:你會發現 按鈕還在。
三、讓鼠標移動到第二個根節點
Chrome:第二個節點的按鈕顯示、第一個節點的按鈕消失
IE8:按鈕一閃即逝。
FireFox:你會看到第一個節點的 按鈕消失了,這不是 mouseout 的做用,是 zTree 內部的功能,當有新的 hover 事件後,會讓以前添加的 hover 對象刪除
四、讓鼠標移動到 樹 ul 對象,把 x、y 設置爲第一個根節點的位置
Chrome:第一個根節點的按鈕顯示,第二個根節點的按鈕消失
IE8:按鈕一閃即逝。
FireFox:你會發現界面上沒有任何變化,依然顯示這第二個根節點的按鈕
五、利用 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();
}
}
今天時間不夠了,明天再研究拖拽吧....