Link to home
Start Free TrialLog in
Avatar of ana-redondo-lopez
ana-redondo-lopez

asked on

Clicking on a button with HttpUnit

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

perhaps also the search failed and it returned you to the default page
Avatar of a_b
a_b

When you go and click the button manually; does the URL change?
Avatar of ana-redondo-lopez

ASKER

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.



if its a form I think you need to submit() it, not click() it
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?
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?
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.
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.
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());
but its a 'submit' button. If you don't want it to submit a form then use a plain button
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.
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>
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 .
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());
ASKER CERTIFIED SOLUTION
Avatar of sailingbye
sailingbye
Flag of United Kingdom of Great Britain and Northern Ireland 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