关闭

Tips on HttpUnit

1119人阅读 评论(0) 收藏 举报

Tips on HttpUnit

  1. import package.
    import com.meterware.httpunit.*;
    import junit.framework.*;
  2. Unit test source code of httpunit is primary resource to study how to use it.
    httpunit-1.6.2/test/com/meterware/httpunit
  3. Code fragment which shows how to log in.
    WebConversation     conversation = new WebConversation();
    WebRequest request = new GetMethodWebRequest( "http://localhost/portal" );

    WebResponse response = conversation.getResponse( request );
    WebForm loginForm = response.getForms()[0];
    request = loginForm.getRequest();
    request.setParameter( "user", "user" );
    request.setParameter( "pwd", "password123" );
    response = conversation.getResponse( request );

    assertEquals( "login url", "http://localhost/portal/servlet/login", response.getURL().toString() );
  4. Code fragment which shows how to disable javascript. Some web application does do some bad html which httpunit can't parse correctly.
    WebConversation conversation = new WebConversation();

    //
    // Disabling JavaScript validation
    //
    HttpUnitOptions.setScriptingEnabled(false);

    WebRequest request = new GetMethodWebRequest( "http://localhost" );

    WebResponse response = conversation.getResponse( request );



  5. Code fragment which shows how to get frame.

    String[] frames = response.getFrameNames();
    for ( int i = 0; i < frames.length; i++ )
    {
    System.out.println( "frame[" + i + "]:" + frames[i] );
    }

    WebResponse frameResponse = response.getSubframeContents( "topFrame" );
    // WebResponse frameResponse = response.getSubframeContents( "leftFrame" );
    System.out.println( "mainFrame:" + frameResponse.getURL().toString() );

    WebLink[] links = frameResponse.getLinks();
    for ( int i = 0; i < links.length; i++ )
    {
    System.out.println( "link:" + links[i].getText() + ", url:" +links[i].getURLString() );
    }

    WebResponse menuResponse = getFrameWithURL( conversation, "topmenu" );
    assertNotNull( "topMenu is null", menuResponse );

    links = frameResponse.getLinks();
    for ( int i = 0; i < links.length; i++ )
    {
    System.out.println( "topmenu link:" + links[i].getText() + ", url:" +links[i].getURLString() );
    }

    NOTE: the following methods are from httpunit's own unit test.
    private String getNameOfFrameWithURL( WebConversation wc, String urlString ) {
    String[] names = wc.getFrameNames();
    for (int i = 0; i < names.length; i++) {
    WebResponse candidate = wc.getFrameContents( names[i] );
    System.out.println( "getNameOfFrameWithURL() frame url:" + candidate.getURL().toExternalForm() );
    if (candidate.getURL().toExternalForm().indexOf( urlString ) >= 0) {
    return names[i];
    }
    }
    return null;
    }


    private WebResponse getFrameWithURL( WebConversation wc, String urlString ) {
    String name = getNameOfFrameWithURL( wc, urlString );
    System.out.println( "getFrameWithURL:" + name );
    if (name == null) return null;
    return wc.getFrameContents( name );
    }
  6. Httpunit can't parse iframe's id attribute but name attribute is accepted.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:130145次
    • 积分:1804
    • 等级:
    • 排名:千里之外
    • 原创:48篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    文章分类
    最新评论