Search code examples
formsbuttonyuijenkinshtmlunit

problem writing HTMLUnit script for YUI form submit


I want to write a simple HTMLUnit test script for Jenkins (former Hudson). INFO: Jenkins uses the YUI Javascript library. The YUI library replaces the form submit with a custom button. The script just creates a new job in Jenkins.

start Jenkins: java -jar jenkins.war

Current versions of HTMLUnit do not support form.submit any more and require you to use button.click() for form submitting. Unfortunately this does not work for Jenkins (the sample below does not advance the page and create the job but stays on the new job page)

I tried for some hours now to find a solution or workaround but so far I could not get the form submitted. Hopefully somebody has found a solution and let me know.

Here is my sample code:

package example;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class jenkins3 {
     public static void main(String args[]) {
        // create a new job in jenkins
        // home
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        try {
            final HtmlPage page1 = webClient.getPage("http://localhost:8080");
            //assertEquals("Dashboard [Jenkins]", page1.getTitleText());

            // new job
            final HtmlAnchor new_job = page1.getAnchorByText("New Job");
            final HtmlPage page2 = new_job.click();

            // job name
            HtmlTextInput name_field = (HtmlTextInput) page2.getElementById("name");
            name_field.type("new job by htmlunit");

            // radio button
            final HtmlInput radio_freestyle = (HtmlInput) page2.getByXPath("//input[@value='hudson.model.FreeStyleProject']").get(0);
            radio_freestyle.click();
            Thread.sleep(10000);

            // OK button (submit form)
            final HtmlForm form = page2.getFormByName("createItem");
            //final HtmlSubmitInput button = (HtmlSubmitInput) form.getByXPath("//button").get(0);
            final HtmlButton button = (HtmlButton) form.getByXPath("//button").get(0);

            final HtmlPage page3 = button.click(); // !!!!! Form submit does not workstacko
            //assertEquals("Dashboard [Jenkins]", page3.getTitleText());
        }

        catch( Exception e ) {
            System.out.println( "General exception thrown:" + e.getMessage() );
            e.printStackTrace();
        }

        webClient.closeAllWindows();
    }
}

Solution

  • Although this may be completely out of the question in your senerio: i would suggest giving up on HTMLUnit. I've had bug after bug, mainly because of the pages I've been automating tests for aren't always 100% valid html (3rd party piggybacked requests). I finally went looking elsewhere and found PhantomJS (js bindings to the WebKit engine) much better suited. For me the advantages were evident: - no need for another app server - much simpler and faster to script JS than Java - a "real"-world engine is more realistic than a buggy emulator one Thats my advice, Cheers