Search in sources :

Example 11 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project struts by apache.

the class FileUploadTest method testEmptyFile.

@Test
public void testEmptyFile() throws Exception {
    try (final WebClient webClient = new WebClient()) {
        final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/fileupload/doUpload.action");
        final HtmlForm form = page.getFormByName("doUpload");
        HtmlInput captionInput = form.getInputByName("caption");
        HtmlFileInput uploadInput = form.getInputByName("upload");
        captionInput.type("some caption");
        File tempFile = File.createTempFile("testEmptyFile", ".tmp");
        tempFile.deleteOnExit();
        uploadInput.setValueAttribute(tempFile.getAbsolutePath());
        final HtmlSubmitInput button = form.getInputByValue("Submit");
        final HtmlPage resultPage = button.click();
        DomElement errorMessage = resultPage.getFirstByXPath("//span[@class='errorMessage']");
        Assert.assertNotNull(errorMessage);
        Assert.assertEquals("File cannot be empty", errorMessage.getVisibleText());
    }
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) File(java.io.File) Test(org.junit.Test)

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