Clicking on a button with HttpUnit

ana-redondo-lopez
ana-redondo-lopez used Ask the Experts™
on
Now I'm trying to click on the button and check the retrieved page is the right one, but I get this message:

Exception in thread "main" junit.framework.AssertionFailedError: expected:<http://cbrint172/DMS_test_area/search.aspx>
but was:<http://cbrint172/DMS_test_area/default.aspx>

It looks like the button hasn't been clicked because the page being retrieved is the page I'm at at the moment.
Any ideas? (java code is attached)

Ana
       package-secondTrial.doc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
perhaps also the search failed and it returned you to the default page
a_b
Top Expert 2009

Commented:
When you go and click the button manually; does the URL change?

Author

Commented:
Yes, when I click the button manually the URL changes from
http://cbrint172/DMS_test_area/default.aspx
to
http://cbrint172/DMS_test_area/search.aspx

This is why I don't understand why jdoc is returning the first web page, not the second one.



11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Mick BarryJava Developer
Top Expert 2010

Commented:
if its a form I think you need to submit() it, not click() it

Author

Commented:
I'm not sure, it's a submit button inside the 5th row of a table (this table is inside a form):
<TR>
      <TD>
                 <input type="submit" name="cmdSearch" value="Search for Projects &amp; Documents" onclick="window.open('search.aspx');" id="cmdSearch" title="Click on the search link to see the search engine." style="width:288px;" />

        </TD>
</TR>
I thought I wouldn't treat it as a form. What do you think?

Author

Commented:
Given treating the button as a button doesn't seem to work, I'm treating it as a form, so I can submit ()instead of click()

The new code compiles but when executing  something goes wrong at the following line:
assertEquals("cmdSearch", Form2.getName());
because the system.out.println that comes after it doesn't execute.

I get no error messages, all I get is:
Exception: java.lang.NullPointerException
Found the Siemens Projects page
Found table where 'Search for Proj & Docs button' is.
Found form where 'Search for Proj & Docs button' is.

Any ideas?
Thank you

Could it be that  window.open('search.aspx')  is opening a new (virtual) window but that your WebCoversation is still focussed on the old (virtual) window?  

Have you tried  window.location('search.aspx')  or is this not the desired web behaviour?

Author

Commented:
I haven't tried window.location('search.aspx') , don't know what it is.
Sorry I forgot to attach the java code in the last posting.
It's attached now.
package-secondTrial.doc
In response to #33115325 I'm guessing that either WebForm.getName() or WebResponse.getFormWithId(String) are returning null.

Have you tried to assert that neither are null?
Sorry, I meant window.location = 'search.aspx'.  It changes the url of the current browser window as opposed to opening a new one.

Author

Commented:
Just checked, did
        assertNotNull("Form2 returns null", Form2);
and got
        Exception in thread "main" junit.framework.AssertionFailedError: Form2 returns null

Does it mean this method doesn't return anything because button is not a form?
It means that this method is returning null:

resp.getFormWithID("cmdSearch")

Presumably because no form with an _ID_ of "cmdSearch" was found.

Try <form id="cmdSearch" ...

I understand that 'id' is the standard way of identifying html elements and that 'name' was a proprietary extension that stuck.

Author

Commented:
Ok, I think it's simply button is not a form. In the HTML code it's a submit button, it doesn't have the tag <form       ...........             form/>

I think I'll go back to what I was doing before.  Can you please have a look at 26299989 at the top of the page?
I suggest that instead of window.open you use

onclick="window.location='search.aspx';"

I think this will satisfy your assertion:

assertEquals("http://cbrint172/DMS_test_area/search.aspx", jdoc2.getURL());
Mick BarryJava Developer
Top Expert 2010

Commented:
but its a 'submit' button. If you don't want it to submit a form then use a plain button

Author

Commented:
Hi sailingbye,

I've just realized what the problem is, although I don't know how to solve it.
I can find the button on the form and I can click it indeed. BUT after clicking it, the current page is not replaced, a new window is opened instead. That's why after  clicking the button I get the current page's URL, not the new opened page URL.
Now I don't know how to retrieve this new window/page.
Any ideas?

Thank you
I can see two options:

1) If you want (or cannot change) the search page to open in a new window, then you must use theWebConversation object to get hold of the new window.  To do this there appears to be two methods available, getCurrentPage() or getOpenWindows()
 
http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/WebClient.html#getCurrentPage%28%29
http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/WebClient.html#getOpenWindows%28%29

2) If would prefer the search page to replace the default page, then change the html such that window.open is replaced with window.location. I.e.

onclick="window.location='search.aspx';"


If you need any coding help with option 1) do let us know.

Hope this helps.

Author

Commented:
About point 1, I've tried both methods. With getCurrentPage() , after clicking the button I try to retrieve the referenced page but I only get the current page.
With getOpenWindows(), when I print out the name of the retrieved page I get
$$HttpUnit_Window$$_0
which doesn't make any sense to me. I've google searched it, hasn't been of much use.
So I've got to the conclusion the clicking is working somehow but is not retrieving what it should..
You can  have a look at my code if you need to.

So I'll try point 2. But I don't know how to fit in your line of code into mine, or find similar methods within the classes I'm using.

Thanks a lot,

Ana
package-secondTrial.doc
Hi

1)  getOpenWindows() returns an array of WebWindow objects, presumably one for each window that is open. The curious name that you printed was the name of the array.  To print the name of each window in the array, iterate over the array like this:

WebWindow[] ww = wc.getOpenWindows();
for(i=0; i<ww.length; i++) {
  System.out.println(ww[i].getName();
}

2) The code from comment #33113470 above becomes

<TR>
      <TD>
                 <input type="submit" name="cmdSearch" value="Search for Projects &amp; Documents" onclick="window.location='search.aspx';" id="cmdSearch" title="Click on the search link to see the search engine." style="width:288px;" />

        </TD>
</TR>

Author

Commented:
The funny name I got printed was the name of the first element in the array, that should have been the opened window. The array name is jdoc.

I need to talk to a colleage about changing the HTML code, I didn't write  it.
Hope it works.

Thank you .

Author

Commented:
I finally found out.
The solution is being aware there are two windows opened (the first window that initiated the conversation and the second window opened when clicking on the button).
So I  need to declare an array that contains these two windows. Then i can retrieve the any of them, I want the second window in this case.

WebWindow windows[]= wc.getOpenWindows();
          assertEquals(2, windows.length);
          System.out.println("Clicked it");
      **  resp = windows[1].getCurrentPage();   **
          assertNotNull("Page hasn't been found.", resp);
          assertEquals("http://cbrint172/DMS_test_area/search.aspx", resp.getURL().toString());
Forgive me but this is precisely one of the two solutions I was guiding you to with my comments #33115501, #33115633, #33136571 and #33136743.  In those comments I explicitly recommend the getOpenWindows() method and advise you that it returns an array of WebWindows.

With respect to your solution, I suggest that this is not good testing practise for the following reason; you are testing for the situation when, initially, your 'default' page is the only window open and the 'search' page becomes the second window (i.e. element 1 in the array). Any situation other than this (e.g. if the user has another, unrelated, window open) will fail your test, even though the 'search' page might well have opened correctly. Iterating over the array, as I suggested, is a more robust test.

Apologies if this seems pedantic but I like to do things correctly and advise likewise :-).

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial