Search in sources :

Example 6 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project faces by jakartaee.

the class Spec1555IT method testSingleSelection.

private void testSingleSelection(String form) throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "spec1555IT.xhtml");
    HtmlFileInput input = page.getHtmlElementById(form + ":input");
    assertEquals("Multiple attribute is NOT set", "", input.getAttribute("multiple"));
    File file = generateTempFile("file", "bin", 123);
    input.setValueAttribute(file.getAbsolutePath());
    page = page.getHtmlElementById(form + ":submit").click();
    assertEquals("Value attribute is NOT set", "", page.getHtmlElementById(form + ":input").getAttribute("value"));
    HtmlElement messages = page.getHtmlElementById("messages");
    assertEquals("There is 1 message", 1, messages.getChildElementCount());
    DomElement message = messages.getChildElements().iterator().next();
    assertEquals("Uploaded file has been received", "field: singleSelection, name: " + file.getName() + ", size: " + file.length(), message.asNormalizedText());
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HtmlInputFile(jakarta.faces.component.html.HtmlInputFile) File(java.io.File)

Example 7 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project faces by jakartaee.

the class Spec1555IT method addValueAttribute.

/**
 * HtmlUnit's HtmlFileInput doesn't support submitting multiple values.
 * The below is a work-around, found on https://stackoverflow.com/a/19654060
 */
private static void addValueAttribute(HtmlFileInput input, String valueAttribute) {
    AttributesImpl attributes = new AttributesImpl();
    attributes.addAttribute(null, null, "type", null, "file");
    attributes.addAttribute(null, null, "name", null, input.getNameAttribute());
    HtmlFileInput cloned = (HtmlFileInput) new HtmlUnitNekoHtmlParser().getFactory("input").createElementNS(input.getPage(), null, "input", attributes);
    input.getParentNode().appendChild(cloned);
    cloned.setValueAttribute(valueAttribute);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) HtmlUnitNekoHtmlParser(com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput)

Example 8 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project faces by jakartaee.

the class Spec1556IT method testRenderingOfAcceptAttribute.

/**
 * @see HtmlInputFile#getAccept()
 * @see https://github.com/jakartaee/faces/issues/1556
 */
@Test
public void testRenderingOfAcceptAttribute(String form) throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "spec1556IT.xhtml");
    HtmlFileInput inputFileWithoutAccept = page.getHtmlElementById("form:inputFileWithoutAccept");
    assertEquals("Unspecified 'accept' attribute on h:inputFile is NOT rendered", "", inputFileWithoutAccept.getAttribute("accept"));
    HtmlFileInput inputFileWithAccept = page.getHtmlElementById("form:inputFileWithAccept");
    assertEquals("Specified 'accept' attribute on h:inputFile is rendered", "image/*", inputFileWithAccept.getAttribute("accept"));
    // It's for Mojarra also explicitly tested on h:inputText because they share the same renderer.
    HtmlTextInput inputTextWithoutAccept = page.getHtmlElementById("form:inputTextWithoutAccept");
    assertEquals("Unspecified 'accept' attribute on h:inputText is NOT rendered", "", inputTextWithoutAccept.getAttribute("accept"));
    HtmlTextInput inputTextWithAccept = page.getHtmlElementById("form:inputTextWithAccept");
    assertEquals("Specified 'accept' attribute on h:inputText is NOT rendered", "", inputTextWithAccept.getAttribute("accept"));
// NOTE: HtmlUnit doesn't support filtering files by accept attribute. So the upload part is not tested to keep it simple (it's nonetheless already tested in Spec1555IT).
}
Also used : HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) Test(org.junit.Test)

Example 9 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project htmlunit by HtmlUnit.

the class HTMLInputElement method getFiles.

/**
 * 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;
}
Also used : FileList(com.gargoylesoftware.htmlunit.javascript.host.file.FileList) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 10 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project htmlunit by HtmlUnit.

the class HTMLFormElement2Test method fileInput_fireOnChange.

/**
 * @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);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Aggregations

HtmlFileInput (com.gargoylesoftware.htmlunit.html.HtmlFileInput)11 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)7 Test (org.junit.Test)5 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)4 File (java.io.File)4 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)3 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)2 HtmlTextInput (com.gargoylesoftware.htmlunit.html.HtmlTextInput)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 HtmlInputFile (jakarta.faces.component.html.HtmlInputFile)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)1 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlButtonInput (com.gargoylesoftware.htmlunit.html.HtmlButtonInput)1