Tips on HttpUnit

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



    • 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 );
      }
      • Httpunit can't parse iframe's id attribute but name attribute is accepted.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值