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

昨天我們已經可以輕鬆移動鼠標了,距離拖拽只有一步之遙。 其實這就是一層窗戶紙,捅破它就搞定了,之前做的操作可以說都是單步操作:移動鼠標、點擊頁面元素、彈出窗口等等;而拖拽操作就不行了,他需要一連串連貫的動作配合起來: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個部分:
  • 觀察反覆拖拽測試 1
   可以專門用來觀察 moveToElement 在不同瀏覽器下的情況

  • 觀察反覆拖拽測試 2
   可以專門用來觀察 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();
    }
}
發佈了21 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章