Solved

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

Posted on 2006-06-22
15
1,120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better way to make a string with template variables in java 3 53
Java: anonymous class 4 55
restrict decimal places for double datatype 10 50
Session in java desktop 5 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 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