Solved

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

Posted on 2006-06-22
15
1,110 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
ID: 16962604
0
 
LVL 92

Expert Comment

by:objects
ID: 16964546
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
ID: 16964580
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
ID: 16964621
            WebLink myvoice       =  response.getLinkWith("myvoice");
             assertNotNull("myvoice link not found", myvoice);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16964695
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
ID: 16964697
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
ID: 16964702
same way, look for them in the response and add an assert that they are present.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sunnybrad
ID: 16972739
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
ID: 16973548
0
 

Author Comment

by:sunnybrad
ID: 16988577
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
ID: 16988696
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
ID: 16988718
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
ID: 16989075
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
ID: 16989087
Thats what DialogAdapter does by default, ie. confirms all dialogs
0
 

Author Comment

by:sunnybrad
ID: 17005463
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
JAVA part two 5 57
advertisement module in core php 4 145
Spring Framework HTTPSession management 1 23
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

12 Experts available now in Live!

Get 1:1 Help Now