不可编辑域和日历控件域的输入
2013-05-07 13:46
323 查看
网页上往往会有些输入域是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也是可以的。具体方法如下:
[java]
/**
* 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个参数,使用起来不是很方便,我们可以继续重载出更简单实用的方法:
[java]
/**
* 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);
}
这里先讲一下document的几个getElement的方法:
1、 document.getElementById,由于ID是页面元素的唯一性标示属性,所以这种方法返回的元素是单个的。
2、 document.getElementsByName,与ID不同,Name不是唯一标示属性,所以返回的是一个对象数组,可以从字面上看出是getElements而不是getElement;
3、 document.getElementsByTagName,与getElementsByName原理相同。
了解了这三种方法,我们就可以通过执行JS去修改控件属性值了,可编辑域要输入的内容一般都是value属性,当然innertext也是可以的。具体方法如下:
[java]
/**
* 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);
}
/** * 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个参数,使用起来不是很方便,我们可以继续重载出更简单实用的方法:
[java]
/**
* 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);
}
/** * 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】不可编辑域和日历控件域的输入
- Qt设置QLineEdit控件不可编辑、密文输入、输入格式等小技巧
- VC6下使编辑控件只能输入数字的简单实现
- --=== 利用微软网格控件进行编辑输入==--
- bootstrap selectpicker控件select下拉框动态数据无法回显的问题 && 不可编辑的下拉框数据提交至后台
- 第二章 弹出日历选择输入的Calendar控件
- 验证GridView控件在编辑状态下输入的数据是否合法(不合法,不允许更新,同时弹出提示框)
- jsp 文本框不可编辑,禁止文本框的输入,JSP页面中的文本框只显示内容,而用户无法修改
- 不可编辑控件的变相解决方式
- 让文本框内容不可编辑,如果type="file",那么只能通过点击浏览来改变,不能通过输入文字改变
- 文本输入框不可编辑或下划线效果
- [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
- 几种设置表单元素中文本输入框不可编辑的方法
- ASP.NET 控件不可编辑
- --=== 利用微软网格控件进行编辑输入==--
- 如何实现JSP页面控件的不可编辑——disabled readonly
- 结合日历控件使用的强制输入时分秒的js
- --=== 利用微软网格控件进行编辑输入==--
- winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
- 几种设置表单元素中文本输入框不可编辑的方法