Solved

Testing a link using HttpUnit...........

Posted on 2006-06-22
15
1,108 Views
Last Modified: 2008-02-26
Hi All:
I have a small testing project. I have to test the web-site link and forms using HttpUnit. I am trying to create a WebLink class without any success. Please show me a way how to test form and links. My code fragment is as follows:

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebLink;


public class MyDemo {
            public void testHomePage() throws Exception
          {
              // Initiate WebConversation
              WebConversation wc   =  new WebConversation();
              WebRequest request   =  new GetMethodWebRequest ("http://www.test.net");
              
              WebResponse response =  wc.getResponse(request);
              WebForm myact           =  response.getForms()[0];
              
              WebLink myvoice        =  new WebLink();
          //  I am not able to create above constructor, show me a way how to test links and forms.

                                                                                                                              

              
          }
}

A code fragment will help greatly. I just need to navigate through links and test the web form and links.  Look forward to quick response.

Regards

-Gyan
0
Comment
Question by:sunnybrad
  • 7
  • 6
  • 2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
public class MyDemo {
          public void testHomePage() throws Exception
         {
             // Initiate WebConversation
             WebConversation wc   =  new WebConversation();
             WebRequest request   =  new GetMethodWebRequest ("http://www.test.net");
             
             WebResponse response =  wc.getResponse(request);
             WebForm myact           =  response.getForms()[0];
 
             // to get link with text "myvoice"
             WebLink myvoice       =  response.getLinkWith("myvoice");
                                                                                                                                         
         }
}
0
 

Author Comment

by:sunnybrad
Comment Utility
Hi All:

I have a few additional question, since I am new to HttpUnit:

I have to test a few webpages.
This mean I have to confirm the presence of few links and form elements.
I know how to navigate through these pages. How can I do that.
Is there a method to do all this.

AssertLink something of that nature.

Can someone give me readable and nice way to report failures and success.

Link found then it is a success.
Link not found then it is a failure. Code fragment would be greatly appreciated.

Regards

-Gyan
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
            WebLink myvoice       =  response.getLinkWith("myvoice");
             assertNotNull("myvoice link not found", myvoice);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
public static Map testLinks(WebResponse response, String[] links) {
      Map results = new HashMap();
      final String FAIL = "FAIL";
      final String SUCCESS = "SUCCESS";      
      for(int i = 0;i < links.length;i++) {
            WebLink wl = response.getLinkWith(links[i]);
            results.put(links[i], wl != null? SUCCESS : FAIL);
      }
      return results;
}
0
 

Author Comment

by:sunnybrad
Comment Utility
Hi:

Thanks for the response. How do you make sure  certain form elements are present.
Is there a quick and easy way like testing the forms.

Regards

-sunnybrad
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
same way, look for them in the response and add an assert that they are present.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sunnybrad
Comment Utility
Hi All:

I am not able to get the webpage because of  some javascript in the page. The code that causes error is below:

            WebLink myvoice = response.getLinkWith("myVoice");            
            WebResponse myVoicepage = myvoice.click();

The error message is below. It is not full but partial error message from console. The javascript sets the focus in the form and that causes error. Please let me know how to get round it. This is all that remains from the successful execution of my demo. Thanks in advance. Look forward to your response.

Javascript that causes error:

Error: Script 'if (document.forms['login_form'][0].value != '') {
        document.forms['login_form'][1].focus();
    } else {
        document.forms['login_form'][0].focus();
    }
  // ' failed: ConversionError: The undefined value has no properties. (httpunit; line 676)

Console error message:

ConversionError: The undefined value has no properties. (httpunit; line 676)
      at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:597)
      at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:557)
      at org.mozilla.javascript.ScriptRuntime.getProp(ScriptRuntime.java:712)
      at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2035)
      at org.mozilla.javascript.InterpretedScript.call(InterpretedScript.java:62)
      at org.mozilla.javascript.InterpretedScript.exec(InterpretedScript.java:55)
      at org.mozilla.javascript.Context.evaluateReader(Context.java:820)
      at org.mozilla.javascript.Context.evaluateString(Context.java:784)
      at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.executeScript(JavaScript.java:132)
      at com.meterware.httpunit.scripting.ScriptableDelegate.runScript(ScriptableDelegate.java:65)
      at com.meterware.httpunit.parsing.ScriptFilter.getTranslatedScript(ScriptFilter.java:151)
      at com.meterware.httpunit.parsing.ScriptFilter.endElement(ScriptFilter.java:131)
      at org.cyberneko.html.filters.DefaultFilter.endElement(Unknown Source)
      at org.cyberneko.html.filters.NamespaceBinder.endElement(Unknown Source)
      at org.cyberneko.html.HTMLTagBalancer.callEndElement(Unknown Source)
      at org.cyberneko.html.HTMLTagBalancer.endElement(Unknown Source)
      at org.cyberneko.html.HTMLScanner$SpecialScanner.scan(Unknown Source)
      at org.cyberneko.html.HTMLScanner.scanDocument(Unknown Source)
      at org.cyberneko.html.HTMLConfiguration.parse(Unknown Source)
      at org.cyberneko.html.HTMLConfiguration.parse(Unknown Source)
      at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
      at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
      at com.meterware.httpunit.parsing.NekoHTMLParser.parse(NekoHTMLParser.java:41)
      at com.meterware.httpunit.HTMLPage.parse(HTMLPage.java:255)
      at com.meterware.httpunit.WebResponse.getReceivedPage(WebResponse.java:1109)
      at com.meterware.httpunit.WebResponse.getFrames(WebResponse.java:1098)
      at com.meterware.httpunit.WebResponse.getFrameRequests(WebResponse.java:875)
      at com.meterware.httpunit.FrameHolder.updateFrames(FrameHolder.java:179)
      at com.meterware.httpunit.WebWindow.updateFrameContents(WebWindow.java:252)
      at com.meterware.httpunit.WebClient.updateFrameContents(WebClient.java:485)
      at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:146)
      at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:130)
      at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:121)
      at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:144)
      at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:130)
      at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:121)
      at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:144)
      at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:130)
      at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:121)
      at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:144)
      at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:130)
      at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:121)
      at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:144)Error: Script 'if (document.forms['login_form'][0].value != '') {
        document.forms['login_form'][1].focus();
    } else {
        document.forms['login_form'][0].focus();
    }
  // ' failed: ConversionError: The undefined value has no properties. (httpunit; line 676)

      at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:130)
      at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:121)
      at com.meterware.httpunit.WebWindow.sendRequest(WebWindow.java:110)
      at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:253)
      at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:232)
      at com.meterware.httpunit.WebLink.click(WebLink.java:98)

Regards

sunnybrad
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
0
 

Author Comment

by:sunnybrad
Comment Utility
Hi:

One additional question. I get a security alert...because of a bad certificate (through HTTPS).
Is there a way to go around it (e.g. by pressing yes button.)

Let me know if there is any way to get rid of it.

Thanks for all the responses so far, I really appreciate it.

Regards

-sunnybrad

0
 

Author Comment

by:sunnybrad
Comment Utility
Hi:

Just some clarification:

I want to keep interacting with the server after ignoring the security alert.
Please let me know of a way todo that.

Regards,

-sunnybrad
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
Comment Utility
if its a javascript alert then httpunit should click it for you.

or register a DialogResponder

http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/DialogAdapter.html
0
 

Author Comment

by:sunnybrad
Comment Utility
Hi objects:

I might have misrepresented myself, the security alert comes from the server.
DialogResponder seems to get an user input from the program user.
Whereas I need to respond.....to server presenting me a cert and asking me if I want to continue
I want to say yes. and continue working with the form. This we can do WebConversation etc....normal
interaction with form.

I just want to say yes on cert window that https pops up.

Regards

-sunnybrad
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Thats what DialogAdapter does by default, ie. confirms all dialogs
0
 

Author Comment

by:sunnybrad
Comment Utility
Hi:

One more question connecting to https server compared to http server

WebRequest request   =  new GetMethodWebRequest ("http://www.test.net");
This works

WebRequest request   =  new GetMethodWebRequest ("https://208.70.234.12");

Does not work, gives the following error:

Error: HTTPS hostname wrong:  should be <208.70.234.12>

Should I be doing something different.

Look forward to your respond.

Regards

-sunnybrad
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now