com.gargoylesoftware.htmlunit.html.HtmlFileInput Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlFileInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HTMLInputElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
}
 
Example #2
Source File: HTMLFormElement2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #3
Source File: HTMLInputElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@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();
}
 
Example #4
Source File: HTMLInputElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: HTMLInputElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}