论坛首页 Java企业应用论坛

HTTPClient模拟登陆开心网001

浏览 19288 次
精华帖 (3) :: 良好帖 (3) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-14  
Foxswily 写道
有更简单的实现
htmlunit  http://htmlunit.sourceforge.net/
可以模拟点击按钮等等,属于在httpclient更高一层的应用,本意用于测试,实际用来模拟登录效果异常的好。

哇~~~
0 请登录后投票
   发表时间:2010-04-14  
Foxswily 写道
有更简单的实现
htmlunit  http://htmlunit.sourceforge.net/
可以模拟点击按钮等等,属于在httpclient更高一层的应用,本意用于测试,实际用来模拟登录效果异常的好。


HTTPUnit这个工具很早就研究过,主要是想做一些系统测试方面的工作,当时觉得并不好用,后来还是没有应用到项目里面。

做开心登陆时,也想到过使用这个工具,实践中发现还是不好用,最新版的下载下来一些简单的例子也运行出错。个人觉得这个工具维护的不好,最近一次更新还是在2008年。

可能还是不太会用这个工具吧,你要有好的经验可以贴出来共享一下。
0 请登录后投票
   发表时间:2010-04-14  
robertliudeqiang 写道
Foxswily 写道
有更简单的实现
htmlunit  http://htmlunit.sourceforge.net/
可以模拟点击按钮等等,属于在httpclient更高一层的应用,本意用于测试,实际用来模拟登录效果异常的好。


HTTPUnit这个工具很早就研究过,主要是想做一些系统测试方面的工作,当时觉得并不好用,后来还是没有应用到项目里面。

做开心登陆时,也想到过使用这个工具,实践中发现还是不好用,最新版的下载下来一些简单的例子也运行出错。个人觉得这个工具维护的不好,最近一次更新还是在2008年。

可能还是不太会用这个工具吧,你要有好的经验可以贴出来共享一下。


Introduction
The dependencies page lists all the jars that you will need to have in your classpath.

The class com.gargoylesoftware.htmlunit.WebClient is the main starting point. This simulates a web browser and will be used to execute all of the tests.

Most unit testing will be done within a framework like JUnit so all the examples here will assume that we are using that.

In the first sample, we create the web client and have it load the homepage from the HtmlUnit website. We then verify that this page has the correct title. Note that getPage() can return different types of pages based on the content type of the returned data. In this case we are expecting a content type of text/html so we cast the result to an com.gargoylesoftware.htmlunit.html.HtmlPage.

@Test
public void homePage() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    final String pageAsXml = page.asXml();
    assertTrue(pageAsXml.contains("<body class=\"composite\">"));

    final String pageAsText = page.asText();
    assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
}Imitating a specific browser
Often you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.

@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.

Finding a specific element
Once you have a reference to an HtmlPage, you can search for a specific HtmlElement by one of 'get' methods, or by using XPath.

Below is an example of finding a 'div' by an ID, and getting an anchor by name:

@Test
public void getElements() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://some_url");
    final HtmlDivision div = page.getHtmlElementById("some_div_id");
    final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
}XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools

@Test
public void xpath() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

    //get list of all divs
    final List<?> divs = page.getByXPath("//div");

    //get div which has a 'name' attribute of 'John'
    final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);
}Using a proxy server
The last WebClient constructor allows you to specify proxy server information in those cases where you need to connect through one.

@Test
public void homePage_proxy() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2, "http://myproxyserver", myProxyPort);

    //set proxy username and password
    final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
    credentialsProvider.addProxyCredentials("username", "password");

    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.

Submitting a form
Frequently we want to change values in a form and submit the form back to the server. The following example shows how you might do this.

@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form,
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();
}
0 请登录后投票
   发表时间:2010-04-15  
robertliudeqiang 写道
fengsky491 写道
问问,有没有工具能够监测c/s客户端的登录过程

ps:我现在玩的是三国策6,一直不知道怎么做个自动登陆器,badboy好像只对网页有效


不好意思,没搞过。 



可以用IBM 的 Rational Performance Tester 自我感觉很有用,可以随意的抓包分析,并可以结合Eclipse定制代码,有兴趣的一起研究下。
0 请登录后投票
   发表时间:2010-04-15  
我去给你问不就知道了。。。哈哈。。。就在我们公司对面。。。前台MM很正哦。。。
0 请登录后投票
   发表时间:2010-04-15  
robertliudeqiang 写道
Foxswily 写道
有更简单的实现
htmlunit  http://htmlunit.sourceforge.net/
可以模拟点击按钮等等,属于在httpclient更高一层的应用,本意用于测试,实际用来模拟登录效果异常的好。


HTTPUnit这个工具很早就研究过,主要是想做一些系统测试方面的工作,当时觉得并不好用,后来还是没有应用到项目里面。

做开心登陆时,也想到过使用这个工具,实践中发现还是不好用,最新版的下载下来一些简单的例子也运行出错。个人觉得这个工具维护的不好,最近一次更新还是在2008年。

可能还是不太会用这个工具吧,你要有好的经验可以贴出来共享一下。



单独开贴写了写,参见 http://www.iteye.com/topic/644353

 

0 请登录后投票
   发表时间:2010-04-15  
http抓取工具:
HttpWatcher:IE插件方式,界面和功能都很好,还能自己编程扩展。抓取特殊格式的二进制流有问题(如amf),它会自作主张的转成字符。
tcpmon:apache开发,用地不多,感觉可以。
grinder 3.3:开源测试工具,用TCPProxy抓取,可以得到二进制流
0 请登录后投票
   发表时间:2010-04-15  
我有一点不明白,我直接调用post请求,就可以成功登陆。有这么复杂吗?

我写了个例子,没有问题啊
0 请登录后投票
   发表时间:2010-04-16  
不用楼主那么麻烦吧,Selenium这个东西还是很好很强大滴~
0 请登录后投票
   发表时间:2010-07-13  
你好,我想問下,如果是Http登陸協議認證方式,該如何測試系統呢?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics