Search code examples
jsfrichfaceshtmlunit

JSF Richfaces fileUpload testing by HtmlUnit


My question is how can i test Richfaces FileUpload by HtmlUnit.

My html page code is like:

<div class="uploadFile">
    Upload File:
    <div id="allForm:upload" class="rf-fu ">
        <div class="rf-fu-hdr">
            <span class="rf-fu-btns-lft">
            <span class="rf-fu-btn-add">
            <span class="rf-fu-btn-cnt-add" style="background-position: 2px 2px; padding: 3px 5px 3px 21px;">
            <span class="rf-fu-inp-cntr">
            <input class="rf-fu-inp" type="file">
            </span> Add...
            </span>
            </span>
            <span class="rf-fu-btn-upl">
            </span>
            <span class="rf-fu-btns-rgh">
            </div>
            <div class="rf-fu-lst" style="height: 0px"></div>
            <div class="rf-fu-cntr-hdn">
            </div>
            </div>
    </div>

My java htmlunit test code is like:

@Test(dataProvider = "browsers")
public void testFileUpload(BrowserVersion browserVersion) throws IOException {
    HtmlPage page = (HtmlPage) doLogin(browserVersion);
    HtmlFileInput fileUpload = page.getElementByName("rf-fu-inp");
    assertNotNull(fileUpload);
    fileUpload.setData(fileForUpload(FILEPATH).toByteArray());
}

In this case, I get the following exception:

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[rf-fu-inp]

My main problem is that I have not a id on the input element else I have the chance to make something like this.

 HtmlFileInput fileUpload = (HtmlFileInput) page.getElementById("rf-fu-inp");

Solution

  • I found a solution:

     //UplaodFile
            HtmlFileInput htmlFileInput = null;
            DomNodeList<DomElement> inputs = page.getElementsByTagName("input");
            for (DomElement domElement : inputs) {
                if (domElement.getAttribute("class").equals("rf-fu-inp") &&
                        domElement.getAttribute("type").equals("file")) {
                    htmlFileInput = (HtmlFileInput) domElement;
                    htmlFileInput.setValueAttribute(System.getProperty("basedir") + FILEPATHCURRENTXML);
                    break;
                }
            }