国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

和我一起學 Selenium WebDriver(6)——基礎篇

2019-11-06 06:40:21
字體:
來源:轉載
供稿:網友
之前掌握的技術已經可以讓我們對 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(); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 古丈县| 连江县| 武隆县| 铁力市| 邢台县| 朝阳区| 张家口市| 万年县| 邯郸县| 新密市| 临江市| 高雄市| 吉安县| 娱乐| 安岳县| 石屏县| 凤冈县| 金沙县| 张家港市| 正蓝旗| 新龙县| 乌拉特后旗| 祁门县| 海丰县| 潜江市| 富蕴县| 大连市| 抚松县| 富平县| 夹江县| 望谟县| 高青县| 钟祥市| 鸡泽县| 恩施市| 平南县| 大足县| 万盛区| 弥勒县| 介休市| 六盘水市|