package lesson4; import static org.junit.Assert.*; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForAjax { static WebDriver driver; @BeforeClass public static void init() { System.out.println("init..."); // 若是你的 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/core/async.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');"); //判斷節點總數 Long count =(Long) ((JavascriptExecutor)driver).executeScript("return window.zTreeObj.transformToArray(window.zTreeObj.getNodes()).length;"); assertTrue(count == 4); //利用 expandNode 方法展開第一個父節點 ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeObj.getNodeByParam('isParent', true); window.zTreeObj.expandNode(window.zTreeNode, true);"); try { (new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { Boolean isAjaxing = (Boolean) ((JavascriptExecutor)driver).executeScript("return !!window.zTreeNode.isAjaxing;"); return !isAjaxing; } }); } catch(Exception e) { e.printStackTrace(); } //判斷節點總數 count =(Long) ((JavascriptExecutor)driver).executeScript("return window.zTreeObj.transformToArray(window.zTreeObj.getNodes()).length;"); assertTrue(count == 8); //模擬 click 事件 單擊節點 +/- 號展開 WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("window.zTreeNode = window.zTreeNode.children[0]; return $('#' + window.zTreeNode.tId + '_switch').get(0);"); element.click(); // 展開第一個子節點 ((JavascriptExecutor)driver).executeScript("window.zTreeObj.expandNode(window.zTreeNode, true);"); try { (new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { Boolean isAjaxing = (Boolean) ((JavascriptExecutor)driver).executeScript("return !!window.zTreeNode.isAjaxing;"); return !isAjaxing; } }); } catch(Exception e) { e.printStackTrace(); } //判斷節點總數 count =(Long) ((JavascriptExecutor)driver).executeScript("return window.zTreeObj.transformToArray(window.zTreeObj.getNodes()).length;"); assertTrue(count == 12); } @AfterClass public static void destory() { System.out.println("destory..."); //關閉瀏覽器 driver.quit(); } }
package lesson4; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForIframe { static WebDriver driver; @BeforeClass public static void init() { System.out.println("init..."); // 若是你的 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.php#_102"); // 等待 iframe 加載完畢,Timeout 設置10秒 try { (new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { Boolean loaded = (Boolean) ((JavascriptExecutor)driver).executeScript("return !!demoIframe.$.fn.zTree.getZTreeObj('treeDemo');"); return loaded; } }); } catch(Exception e) { e.printStackTrace(); } ((JavascriptExecutor)driver).executeScript("demoIframe.zTreeObj = demoIframe.$.fn.zTree.getZTreeObj('treeDemo');"); //利用 expandNode 方法展開第2個根節點 ((JavascriptExecutor)driver).executeScript("demoIframe.zTreeNode = demoIframe.zTreeObj.getNodes()[1]; demoIframe.zTreeObj.expandNode(demoIframe.zTreeNode, true);"); // 等待 5 秒 try { (new WebDriverWait(driver, 5, 1000)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return false; } }); } catch(Exception e) {} } @AfterClass public static void destory() { System.out.println("destory..."); //關閉瀏覽器 driver.quit(); } }