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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 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(); } } |
联系客服