0

I have some issue with parsing HTML page using htmlUnit WebDriver. I have not any exception. My code looks like:

   public static void main(String[] args)
        throws FailingHttpStatusCodeException, MalformedURLException,
        IOException {

       WebClient wc = initWebClient();
       HtmlPage page = wc.getPage(Constants.START_PAGE);

       HtmlTextInput userInput = (HtmlTextInput) page
            .getElementById(Constants.INPUT_USERNAME_ID);
       userInput.setText(Constants.USERNAME_VALUE);

       HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
            .getElementById(Constants.INPUT_PASSWORD_ID);
       passwordInput.setText(Constants.PASSWORD_VALUE);

       // get submit button
       HtmlSubmitInput submitButton = (HtmlSubmitInput) page
            .getElementById(Constants.SUBMIT_BUTTON_ID);

       HtmlPage afterLoginPage = submitButton.click();
       System.out.println(afterLoginPage.asXml());

       // some further processing
       ....
    }

    private static WebClient initWebClient(){
       WebClient wc = new WebClient(BrowserVersion.CHROME);
       wc.getCookieManager().setCookiesEnabled(true);
       wc.getOptions().setJavaScriptEnabled(false);
       wc.getOptions().setThrowExceptionOnScriptError(true);

       System.out.println("USE SSL");
       wc.getOptions().setUseInsecureSSL(true);

       return wc;
   }

In above page source as XML I am able to find my name. So, It seems I am logged it correctly.

If I am logged it then I'd like to go to page by set its url in WebClient.

    for(some loop){
            // create new WebClient, because it is MULTITHREADING processing. WebDriver is not thread safe so I need to create new WebClient for every thread
            WebClient wc = initWebClient();
            String url = "https://website/details/31944";
            HtmlPage detailsPage = wc.getPage(url);

            System.out.println(url);
            System.out.println(detailsPage.getUrl());
     }

Above sysout will return:

      https://website/details/31944
      http://website/details/31944

It means that when I go to https://website/details/31944, I get http://website/details/31944, so I AM NOT logged anymore.

Is it any way to pass SSL session when I create new WebClient? Or maybe any other approach for multithreading with WebClient?

Best regards, DS

1 Answers1

1

I have resolved the problem using cookies.

    public static void main(String[] args)
        throws FailingHttpStatusCodeException, MalformedURLException,
        IOException {

        WebClient wc = initWebClient(null);

        HtmlPage page = wc.getPage(Constants.START_PAGE);

        HtmlTextInput userInput = (HtmlTextInput) page
            .getElementById(Constants.INPUT_USERNAME_ID);
        userInput.setText(Constants.USERNAME_VALUE);

        HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
            .getElementById(Constants.INPUT_PASSWORD_ID);
        passwordInput.setText(Constants.PASSWORD_VALUE);

        // get submit button
        HtmlSubmitInput submitButton = (HtmlSubmitInput) page
            .getElementById(Constants.SUBMIT_BUTTON_ID);

        HtmlPage afterLoginPage = submitButton.click();

       Set<Cookie> cookies = wc.getCookieManager().getCookies();
       ....
       ....
       // for every thread I create new WebClient
       for(threads loop){
           WebClient wc2 = initWebClient(cookies);
       }
    }

    private static WebClient initWebClient(Set<Cookie> cookies) {
       WebClient wc = new WebClient(BrowserVersion.CHROME);
       wc.getCookieManager().setCookiesEnabled(true);
       wc.getOptions().setJavaScriptEnabled(false);
       wc.getOptions().setThrowExceptionOnScriptError(false);

       if (cookies != null) {
          Iterator<Cookie> i = cookies.iterator();
          while (i.hasNext()) {
              wc.getCookieManager().addCookie(i.next());
          }
        }

        return wc;
   }