【1、如何处理异步加载】
对于异步加载的捕获,其实就是一个等待的过程,这在之前的例子中早已看过,只不过需要特别说明一下 WebDriverWait,当超时后就会抛出异常,所以如果你的测试中对于这个部分不需要抛出异常的话,那么最好用 try catch 包起来。
这次利用 zTree 异步加载的Demo做测试,同时分别利用 WebElement 的 click 方法 和 zTree 的 expandNode 方法展开节点,进行异步加载。使用起来并不困难,直接看代码即可。
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();
}
}
【2、如何监控 iframe】
能够正常监控 异步加载后,对于 iframe 就只剩下一个问题了,如何用 在父窗口执行iframe 窗口内的 js 了。
对于这个问题其实很简单, iframe 的 name 就是iframe 这个window 的对象,所以,你只需要注意把之前的window 换成 iframe 的name 就可以了;同时调用全局变量时也一定要加上这个iframe 的name
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();
}
}
分享到:
相关推荐
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
selenium RC与selenium webdriver的区别
关于Selenium WebDriver工作原理的介绍,原理相关描述
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
seleniumwebdriver(python) (第三版)
selenium webdriver基于python源码案例,全是案例适合小白入门学习
selenium2.0自动化
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
1.2 selenium webdriver学习(二)————对浏览器的简单操作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 selenium webdriver学习(三)------------执行js脚本 . . . . . . . . . . . . . ...
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
自动化测试:Selenium webdriver学习笔记C#版
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章
selenium webdriver2 环境搭建