下面列出了怎么用com.gargoylesoftware.htmlunit.html.HtmlFileInput的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the value of the JavaScript attribute {@code value}.
*
* @return the value of this attribute
*/
@JsxGetter
@Override
public String getValue() {
final HtmlInput htmlInput = getDomNodeOrDie();
if (htmlInput instanceof HtmlFileInput) {
final File[] files = ((HtmlFileInput) htmlInput).getFiles();
if (files == null || files.length == 0) {
return ATTRIBUTE_NOT_DEFINED;
}
final File first = files[0];
final String name = first.getName();
if (name.isEmpty()) {
return name;
}
return "C:\\fakepath\\" + name;
}
return htmlInput.getAttributeDirect("value");
}
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("onchange")
public void fileInput_fireOnChange() throws Exception {
final String html = "<html><body>\n"
+ "<form>\n"
+ " <input type='file' name='myFile' id='myFile' onchange='alert(\"onchange\")'/>\n"
+ "</form>\n"
+ "</body></html>";
final List<String> collectedAlerts = new ArrayList<>();
final HtmlPage page = loadPage(html, collectedAlerts);
final HtmlFileInput fileInput = page.getHtmlElementById("myFile");
fileInput.focus();
fileInput.setAttribute("value", "dummy.txt");
assertEquals(getExpectedAlerts(), collectedAlerts);
}
/**
* {@inheritDoc}
*/
@Override
public String getValue() {
final HtmlInput htmlInput = getDomNodeOrDie();
if (htmlInput instanceof HtmlFileInput) {
final File[] files = ((HtmlFileInput) getDomNodeOrDie()).getFiles();
if (files == null || files.length == 0) {
return ATTRIBUTE_NOT_DEFINED;
}
final File first = files[0];
final String name = first.getName();
if (name.isEmpty()) {
return name;
}
if (getBrowserVersion().hasFeature(HTMLINPUT_FILE_VALUE_FAKEPATH)) {
return "C:\\fakepath\\" + name;
}
return name;
}
return super.getValue();
}
/**
* Returns the {@code files} property.
* @return the {@code files} property
*/
@JsxGetter
public Object getFiles() {
final HtmlInput htmlInput = getDomNodeOrDie();
if (htmlInput instanceof HtmlFileInput) {
final FileList list = new FileList(((HtmlFileInput) htmlInput).getFiles());
list.setParentScope(getParentScope());
list.setPrototype(getPrototype(list.getClass()));
return list;
}
if (getBrowserVersion().hasFeature(HTMLINPUT_FILES_UNDEFINED)) {
return Undefined.instance;
}
return null;
}
/**
* Returns the {@code files} property.
* @return the {@code files} property
*/
@JsxGetter
public Object getFiles() {
final HtmlInput htmlInput = getDomNodeOrDie();
if (htmlInput instanceof HtmlFileInput) {
final FileList list = new FileList(((HtmlFileInput) htmlInput).getFiles());
list.setParentScope(getParentScope());
list.setPrototype(getPrototype(list.getClass()));
return list;
}
if (getBrowserVersion().hasFeature(HTMLINPUT_FILES_UNDEFINED)) {
return Undefined.instance;
}
return null;
}