Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlFileInput

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlFileInput. These examples are extracted from open source projects. 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 Project: htmlunit   Source File: HTMLInputElement.java    License: 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 Project: htmlunit   Source File: HTMLFormElement2Test.java    License: 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 Project: HtmlUnit-Android   Source File: HTMLInputElement.java    License: 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 Project: htmlunit   Source File: HTMLInputElement.java    License: 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 Project: HtmlUnit-Android   Source File: HTMLInputElement.java    License: 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;
}