Link to home
Start Free TrialLog in
Avatar of sunnybrad
sunnybrad

asked on

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

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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");
                                                                                                                                         
         }
}
Avatar of sunnybrad
sunnybrad

ASKER

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
            WebLink myvoice       =  response.getLinkWith("myvoice");
             assertNotNull("myvoice link not found", myvoice);
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;
}
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
same way, look for them in the response and add an assert that they are present.
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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thats what DialogAdapter does by default, ie. confirms all dialogs
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