1

I am using HttpUnit to simulate this web site : http://www.voyages-sncf.com/ This is my code : It don't sent to me the final redirect url just the url for searching not the result

public class TestHttpUnit {

    public static void main(String[] args) throws Exception {

        // Create and initialize WebClient object
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                    System.out.println("handleRefresh");
            }
        });

        // visit Yahoo Mail login page and get the Form object
        HtmlPage page = (HtmlPage) webClient.getPage("http://www.voyages-sncf.com/");
        //Trouver le formulaire par le nom
        HtmlForm form = page.getFormByName("TrainTypeForm");
        //Trouver le formulaire avec l'action 
        //HtmlForm form = page.getFirstByXPath("//form[@action='http://www.voyages-sncf.com/dynamic/expressbooking/_SvExpressBooking']");


        // Enter login and password of 
        form.getInputByName("origin_city").setValueAttribute("paris");
        form.getInputByName("destination_city").setValueAttribute("marseille");
        form.getInputByName("outward_date").setValueAttribute("29/03/2013");


        // Click "Sign In" button/link
        page = (HtmlPage) form.getInputByValue("Rechercher").click();



        System.out.println(page.asText());
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
asma
  • 75
  • 1
  • 10

1 Answers1

0

According to the HTTP Unit cookbook, form submission should be handled like this:

WebForm form = resp.getForms()[0];      // select the first form in the page
// ... fill in form fields, and finally:
form.submit();                          // submit the form

After that, you'll get on the 'waiting' screen. I haven't tried it, but maybe you should do something like this (pseudo code):

do {
    // Wait a while
    wc.waitForBackgroundJavaScript(3000);
    // Somehow refresh the page, since that's what your browser might do, too
} while (someTestToVerifyThatYoureStillOnTheWaitingPage(wc));
mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • My problem isn't in the form submission , is in the redirect url – asma Mar 28 '13 at 11:08
  • After you submit the form, you receive an HTTP request that redirects you? I can't access the site you mentioned in your question, as it redirects me to the [Dutch version](http://www.tgv-europe.nl/nl/). – mthmulders Mar 28 '13 at 11:10
  • After submit the form it redirect me to the page of searching (please wait....) if we submit the form manually it will do the some thing but it redirect us in the final to the result page – asma Mar 28 '13 at 11:14
  • Like the sites of price comparator – asma Mar 28 '13 at 11:15
  • I'm not sure HTTP Unit will be able to handle this, it seems to do some polling to check whether the results are available... Maybe try Selenium (which uses a real browser under the hood)? – mthmulders Mar 28 '13 at 11:15