是否有HTMLUnit登录的工作示例,只需点击几下

时间:2010-11-17 21:41:18

标签: htmlunit

可能显示Javascript测试支持

 
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();

    }


}

但我明白了:

2010年11月17日下午3:41:14 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告:遇到过时的内容类型:'text / javascript'。 建立成功(总时间:4秒)

这没有帮助,因为它不作为单元测试运行,并显示通过/失败等。

我正在使用netbeans 6.9.1

1 个答案:

答案 0 :(得分:1)

那是因为你没有把它写成单元测试。 HtmlUnit有点错误,因为它本身不是测试运行器,而是一个“无头浏览器”,它允许您与Java中的网站进行交互,就像您是浏览器一样。

请改为尝试:

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 ) ;
    }
}