【开源自动化测试疑难FAQ】【WebDriver】不可编辑域和日历控件域的输入
来源:互联网 发布:数据库预留字段命名 编辑:程序博客网 时间:2024/06/05 01:11
网页上往往会有些输入域是readonly的,但是它的值又可以通过其他控件进行赋值,比如日历控件。这种可编辑域的输入通过selenium.type或者WebDriver.sendKeys都无法做到,但是我们可以考虑通过DOM赋值,下面仅以WebDriver为例,简单讲解一下如何做到。请注意,相关的被引用的对象和方法的声明请参见http://blog.csdn.net/fudax/article/details/7879910和http://blog.csdn.net/fudax/article/details/7879915这两个完整的工具类,整个工程的代码参见https://github.com/fudax/star-framework。
这里先讲一下document的几个getElement的方法:
1、 document.getElementById,由于ID是页面元素的唯一性标示属性,所以这种方法返回的元素是单个的。
2、 document.getElementsByName,与ID不同,Name不是唯一标示属性,所以返回的是一个对象数组,可以从字面上看出是getElements而不是getElement;
3、 document.getElementsByTagName,与getElementsByName原理相同。
了解了这三种方法,我们就可以通过执行JS去修改控件属性值了,可编辑域要输入的内容一般都是value属性,当然innertext也是可以的。具体方法如下:
/** * readonly text box or richtext box input. * * @param by the attribute of the element, default support is TagName/Name/Id * @param byValue the attribute value of the element * @param text the text you want to input to element * @param index the index of the elements shared the same attribute value * @throws RuntimeException * @throws IllegalArgumentException */protected void sendKeysByDOM(String by, String byValue, String text, int index) {String js = null;boolean isSucceed = false;if (by.equalsIgnoreCase("tagname")) {js = "document.getElementsByTagName('" + byValue + "')[" + index + "].value='" + text + "'";} else if (by.equalsIgnoreCase("name")) {js = "document.getElementsByName('" + byValue + "')[" + index + "].value='" + text + "'";} else if (by.equalsIgnoreCase("id")) {js = "document.getElementById('" + byValue + "').value='" + text + "'";} else {throw new IllegalArgumentException("only can find element by TagName/Name/Id");}try {driver.executeScript(js);isSucceed = true;pass("input text [ " + text + " ] to element [ " + by + " ]...");} catch (WebDriverException e) {LOG.error(e);} catch (Exception e) {throw new RuntimeException(e.getMessage());}operationCheck(isSucceed);}
可以看得出这个测试方法有4个参数,使用起来不是很方便,我们可以继续重载出更简单实用的方法:
/** * readonly text box or richtext box input, finding elements by element id. * * @param elementId the id of the element * @param text the text you want to input to element * @throws RuntimeException * @throws IllegalArgumentException */protected void sendKeysById(String elementId, String text) {sendKeysByDOM("Id", elementId, text, 0);}/** * readonly text box or richtext box input, finding elements by element name. * * @param elementName the name of the element * @param text the text you want to input to element * @param elementIndex the index of the elements shared the same name, begins with 0 * @throws RuntimeException * @throws IllegalArgumentException */protected void sendKeysByName(String elementName, String text, int elementIndex) {sendKeysByDOM("Name", elementName, text, elementIndex);}/** * readonly text box or richtext box input, finding elements by element tag name. * * @param elementTagName the tag name of the element * @param text the text you want to input to element * @param elementIndex the index of the elements shared the same tag name, begins with 0 * @throws RuntimeException * @throws IllegalArgumentException */protected void sendKeysByTagName(String elementTagName, String text, int elementIndex) {sendKeysByDOM("TagName", elementTagName, text, elementIndex);}
- 【开源自动化测试疑难FAQ】【WebDriver】不可编辑域和日历控件域的输入
- 【开源自动化测试疑难FAQ】【WebDriver】WebDriver对SWFUpload的无奈之举
- 【开源自动化测试疑难FAQ】【WebDriver】几种类型的组件的判断
- 【开源自动化测试疑难FAQ】【WebDriver】WebDriver启动时白屏挂起问题解决方法(一)
- 【开源自动化测试疑难FAQ】【WebDriver】WebDriver启动时白屏挂起问题解决方法(二)
- 【开源自动化测试疑难FAQ】【WebDriver】RemoteWebDriver日志问题处理过程
- 【开源自动化测试疑难FAQ】【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- 【开源自动化测试疑难FAQ】【框架】使用批处理进行过期的测试结果和空目录清理
- 不可编辑域和日历控件域的输入
- 【开源自动化测试疑难FAQ】【框架】测试数据之CSV文件的读写(带编码格式)
- 【开源自动化测试疑难FAQ】【框架】EXE/VBS/BAT/SHELL等文件的调用安全守护
- 【开源自动化测试疑难FAQ】【框架】TestNG运行失败汇总XML文件格式重塑
- Google的开源自动化测试框架
- 开源自动化测试工具selenium的使用
- Twitter的开源自动化测试工具diff
- KDATFFS 开源自动化测试框架
- 开源自动化测试框架Tellurium
- TestNG 开源自动化测试框架
- “盲人”马云:骑着盲虎贩卖梦想
- 虚拟文件系统系列(一)--概述
- C#字符串的比较
- JAVA————框架布局的实例练习
- Matlab函数参考
- 【开源自动化测试疑难FAQ】【WebDriver】不可编辑域和日历控件域的输入
- mysql不能使用innodb存储引擎
- 页面设计 中英文输出 自动换行
- Delphi 教程
- ref和out的区别
- HDU 4276 The Ghost Blows Light
- OAException: Application: ICX, Message Name: ICX_SESSION_FAILED.
- 说一下mysql,oracle等常见数据库的分页实现方案?
- Linux 连续执行多条命令的方法 .