Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a working example of HTMLUnit log in and few clicks

Tags:

htmlunit

Possibly showing Javascript Test Support

 
package htmlunitpoc;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

/**
 *
 * @author 
 */
public class HtmlPoc {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) throws Exception {

        WebClient wc = new WebClient();
                HtmlPage page = (HtmlPage) wc.getPage("http://www.google.com");
                HtmlForm form = page.getFormByName("f");
                HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("btnG");
                HtmlPage page2 = (HtmlPage) button.click();

    }


}

but i get:

Nov 17, 2010 3:41:14 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. BUILD SUCCESSFUL (total time: 4 seconds)

Which does not help as it does not run as a Unit test, and shows Pass/Fail etc.

I am using netbeans 6.9.1

like image 524
kamal Avatar asked Dec 10 '25 08:12

kamal


1 Answers

That's because you haven't written it as a unit test. HtmlUnit is somewhat mis-named, as it's not a test runner itself, instead it's a "headless browser" which allows you to interact with a website from Java as if you were a browser.

Try this instead:

import junit.framework.TestCase;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class HtmlPoc
    extends TestCase
{
   public void test()
      throws Exception
    {
        WebClient wc = new WebClient();
        HtmlPage page = (HtmlPage) wc.getPage("http://www.google.com");
        HtmlForm form = page.getFormByName("f");
        HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("btnG");
        HtmlPage page2 = (HtmlPage) button.click();
        assertNotNull( page2 ) ;
    }
}
like image 83
Rodney Gitzel Avatar answered Dec 13 '25 17:12

Rodney Gitzel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!