网页上往往会有些输入域是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);
}