国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
和我一起學 Selenium WebDriver(6)
之前掌握的技術(shù)已經(jīng)可以讓我們對 zTree 的很多基本功能進行測試了,但還有個大問題沒辦法解決就是 編輯狀態(tài)下 hover 和 拖拽,想搞定這些就要搞定如何移動鼠標。

【1、如何移動鼠標】
行為操作需要用到 org.openqa.selenium.interactions.Action ;移動鼠標這里面提供了2個實現(xiàn)類:MoveMouseAction 和 MoveToOffsetAction;后者比前者多了2個參數(shù)(x,y)。
做這個測試時發(fā)現(xiàn)了一個很嚴重的問題:只有在 Chrome 上是正常的,對于 IE8 和 FireFox 都不能很正常的進行此功能測試的。
(補充:目前是XP 的機器,所以使用的是 IE8 )

在 FireFox 上 MoveToOffsetAction 讓瀏覽器模擬出鼠標移動到指定元素上的事件,這樣導(dǎo)致的結(jié)果是只有 mouseover 會被觸發(fā);而 mouseout 就找不到嘍,即使你把 x、y 設(shè)置的很大 或者 設(shè)置為負值,你捕獲到的事件都是這個元素有鼠標移入,但絕對不會移出。
在 IE 上 執(zhí)行 action 后事件成功觸發(fā),但可能由于真實鼠標并不在指定位置,從而導(dǎo)致立刻又觸發(fā)了 mouseout 事件,會發(fā)現(xiàn)按鈕一閃而過

用 zTree 高級增刪改查的 Demo 來做測試:
1、讓鼠標移動到第一個根節(jié)點
   Chrome、FireFox:你會看到 編輯、刪除按鈕出現(xiàn)了。
   IE8:按鈕一閃即逝。

2、然后把 x、y 設(shè)置為負值,繼續(xù)移動
   Chrome:按鈕消失
   IE8:從上一步消失后,就再沒有出現(xiàn)過,也沒有出現(xiàn)一閃而過的現(xiàn)象。
   FireFox:你會發(fā)現(xiàn) 按鈕還在。

3、讓鼠標移動到第二個根節(jié)點
   Chrome:第二個節(jié)點的按鈕顯示、第一個節(jié)點的按鈕消失
   IE8:按鈕一閃即逝。
   FireFox:你會看到第一個節(jié)點的 按鈕消失了,這不是 mouseout 的作用,是 zTree 內(nèi)部的功能,當有新的 hover 事件后,會讓之前添加的 hover 對象刪除

4、讓鼠標移動到 樹 ul 對象,把 x、y 設(shè)置為第一個根節(jié)點的位置
   Chrome:第一個根節(jié)點的按鈕顯示,第二個根節(jié)點的按鈕消失
   IE8:按鈕一閃即逝。
   FireFox:你會發(fā)現(xiàn)界面上沒有任何變化,依然顯示這第二個根節(jié)點的按鈕

5、利用 MoveMouseAction 讓鼠標移動到 第三個根節(jié)點
   Chrome、FireFox:會看到第三個根節(jié)點的 編輯、刪除按鈕出現(xiàn)
   IE8:會看到第三個根節(jié)點前面的所有節(jié)點依次出現(xiàn)編輯、刪除按鈕,最后到了第三個根節(jié)點停止,他的編輯、刪除按鈕依舊是一閃即逝

看來用這個工具還是多用 Chrome 來測試吧,反正實際工作中基本上 Chrome 沒有問題的話,F(xiàn)ireFox 也沒啥問題的。

因為是為了測試功能,專門把設(shè)置等待的代碼提取出來做成一個工具:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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) {}
    }
}

以下是測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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\\百度網(wǎng)盤\\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");
//      // 創(chuàng)建一個 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 設(shè)置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();
        }
         
        //找到第一個根節(jié)點
        ((JavascriptExecutor)driver).executeScript("window.zTreeObj = $.fn.zTree.getZTreeObj('treeDemo');"
                + "window.zTreeNode = window.zTreeObj.getNodes()[0];");
         
        //獲取 節(jié)點對象
        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);
        //獲取第二個根節(jié)點
        ((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
        //獲取第三個根節(jié)點
        ((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...");
        //關(guān)閉瀏覽器
        driver.quit();
    }
}

今天時間不夠了,明天再研究拖拽吧....
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Selenium2(webdirver)入門之環(huán)境搭建(Java版)
selenium WebDriver之Testng學習一 Testng環(huán)境搭建
Selenium WebDriver 之 PageObjects 模式 by Example
selenium 學習
Selenium Grid深入學習
java selenium webdriver處理JS操作窗口滾動條
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服