Selenium与JavaScript--Selenium深入浅出之三
来源:互联网 发布:哪里有淘宝号买 编辑:程序博客网 时间:2024/05/17 06:48
Selenium与JavaScript
Selenium深入浅出之三
http://blog.csdn.net/shendl/
Selenium全方位解读--Selenium深入浅出之一
Selenium中文参考手册--Selenium深入浅出之二
《Selenium深入浅出》一书的PDF格式的全文见 http://code.google.com/p/shendl/downloads/list
Selenium与EXT
n EXT使用JS动态生成Html和CSS代码。
n 如果未给EXT元素指定id,会使用自增id。无法确定id的值。
n 如果EXT的界面元素发生变化,id也会发生变化。
n Selenium录制工具因此无法定位EXT生成的Web界面
根本原因就是Selenium需要定位html元素,而Ext的很多html元素都是Ext生成的。而且生成的元素的Id都是Ext自动创建的。
解决方法:
n Ext为每一个组件分配ID
n 但是有一些Html元素无法指定ID
n Selenium录制工具辅助手工编写测试代码
n Selenium可以使用JS代码,操作用户界面的EXT对象。
可以使用Selenium接口的getEval方法在浏览器中执行JS代码。
Selenium接口
java.lang.String getEval(java.lang.String script)
方法,动态执行JavaScript代码。
storeEval (script, variableName )
Arguments:
· script - the JavaScript snippetto run
· variableName - the name of avariablein which the result is to be stored.
Returns:
the results ofevaluating the snippet
结果存储在JS的变量名中。
动态执行JavaScript代码的注意点
通过调用Selenium接口的getEval()等方法可以直接把Java中的JavaScrip代码发送到浏览器中执行。
但是,这里实际上执行的并不是标准的JavaScript的eval()方法。getEval()方法中的JavasScript代码的this是JavaScript中的Selenium对象,而不是window对象。
如果你需要调用当前页面的window对象的方法,你需要直接指出window对象,如:“window.document.getElementById('foo');”。
getEval()方法中的JavasScript代码的this是JavaScript中的Selenium对象。请注意这不是Java中的Selenium接口,而是JavaScript中的Selenium对象。JavaScript中的Selenium对象的作用是代表Java中的Selenium接口执行一系列的命令,让浏览器执行。
另外,Selenium接口的selectFrame和selectWindow方法可以修改当前Selenium命令的目标窗口。也就是说,当前的window对象会改变。
Selenium执行Ext的例子:
protected Selenium browser;
……
this.browser.getEval("window.frames['1000'].window.Ext.getCmp('testId-09').setValue('应严办!');");
说明:
Window是当前Selenium命令的目标窗口,这个当前窗口可以通过Selenium接口的selectFrame和selectWindow方法修改!
Selenium动态执行JavaScript的例子:
也可以使用这种方法,在Java端注入javascript代码到页面中动态执行,如:
rowNumStr = this.browser
.getEval("(function(){"
+ "vartableLength=0;"
+ " vartables=window.frames['iframe_00300000000000000003'].window.document.getElementsByTagName('table');"
+ "for(varj=0;j<tables.length;j++){"
+ " if(tables[j].className=='x-grid3-row-table'){"
+ " tableLength++;"
+ " if(tables[j].childNodes[0].childNodes[0].childNodes[1].childNodes[0].innerText=='"
+complainIdValue + "' ) {"
+ " return tableLength;" +" }" +" }"
+ "}" + "return -1;" +"})();");
《Selenium深入浅出》一文到这里就结束了。读者通过本文应该已经能够完全驾驭Selenium这个独特的Web界面测试工具一展身手了。
- Selenium与JavaScript--Selenium深入浅出之三
- Selenium中文参考手册--Selenium深入浅出之二
- Selenium WebDriver之JavaScript
- Selenium WebDriver之JavaScript
- 封装selenium之三
- selenium之执行JavaScript脚本
- Selenium学习笔记之三
- selenium之三,各种driver
- selenium Javascript
- selenium(三)之webDriver与浏览器版本问题
- Selenium全方位解读--Selenium深入浅出之一
- Selenium全方位解读--Selenium深入浅出之一
- selenium + python之元素定位(三)
- selenium-Python之unittest(三)
- Selenium
- Selenium
- Selenium
- selenium
- 颠覆C#王权的“魔比斯环” — 实现AOP框架的终极利器
- 中国经济要掌握好分寸啊
- c#全角半角转换
- javascript访问frame,iframe框架和href的定向
- Business Intelligence: Using SQL 2008 Reporting Services and SQL Report Builder 2.0 with Log Parser
- Selenium与JavaScript--Selenium深入浅出之三
- 解析SQL Server中如何得到自动编号字段
- 认识ASP.NET配置文件Web.config
- 大家好!欢迎加入QQ技术交流群。
- C/C++编程新手错误语录(续二)
- Flex笔试 (面试题)
- 什么是Linux系统,Linux与UNIX系统区别和Linux的创始人简介
- 应用程序域
- 如何理解JAVA 参数传递总是值传递(pass-by-value )