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(); } } |