First of all thank you for your reply. It seems that the next version (1.7) will contain really helpful functions :)
I've checked out the current version from your repository and tried to adapt your example. Here is a short test version:
HTMLElement elements[]=response.getElementsByTagName("span");
HTMLElement rightSpan = null;
for (int i=0; i<elements.length; i++)
{
HTMLElement span = elements[i];
if (span.getText().equals("489"))
{
// the first element with this text is always the right one
if (rightSpan == null)
{
rightSpan = span;
}
}
}
System.out.println(rightSpan.getAttribute("onclick")); //result = "CCUtility.crtCtrla(this, "rim_ModuleSearchResult=Drilldown=key_0", null, null);"
rightSpan.handleEvent("onclick");
response = conversation.getCurrentPage();
System.out.println(response.getText());
In my application the CCUtitly-objects sends a request to the server and a new page will be shown in my browser. So after calling the handleEvent-function, conversation.getCurrentPage() should return this new page, right? Unfortunately the page doesn't change so that the handleEvent doesn't call the CCUtility-object in a right way. That might be hard to figure out because this Javascript object is part of the CommonControls framework. But perhaps you have any advise for me.
Regards,
Christoph
> -----Ursprüngliche Nachricht-----
> Von: [email protected], Discussion of use and development of HttpUnit <[email protected]>
> Gesendet: 01.04.08 15:05:34
> An: Discussion of use and development of HttpUnit <[email protected]>
> Betreff: Re: [Httpunit-develop] Execute Javascript of span-element
> Christoph,
>
>
>
> you wrote:
>
> > I've got a table column that looks like this:
>
> >
>
> > <td class='cl'><span onClick='CCUtility.crtCtrla(this, "rim_ModuleSearchResult=Drilldown=key_0", null, null);' class='feact'><span>489</span></span></td>
>
> >
>
> > Now I want to call this onClick-Method but there is no possibility to get this part of the span object. The source code cannot be modified because it is generated by the CommonControls-Framework.
>
> >
>
> > So any hints or comments are welcome.
>
> The not yet released version 1.7 starts being fun with questions like this one.
>
>
>
> Please find below a testcase that does what you intend to do. In 1.7 (if that's going to be the name) there are two convenience functions for you getElementsByTagName will work on a response and use the entire dom to start with. In fact it's just a shortcut for:
>
>
>
>
>
>
>
>
>
> return
>
>
> getReceivedPage().getElementsByTagName(getDOM(),tagName);
>
>
>
>
>
>
> getElementsWithAttribute will also work to use the onClick you are expecting - except that at this time it seems to want to return the span element twice.
>
>
>
> Could anybody tell me why the function would return the span element twice?
>
>
>
> For clicking there is now the "handleEvent" shortcut function (since 1.7). That is a result of refactoring that should be motivation for you to get the 1.7 from the subversion trunk as long as there is no release available. Version 870 has the test below in the class FormScriptingTest.
>
>
>
>
>
>
>
> Yours
>
> Wolfgang
>
>
>
>
>
>
>
>
>
>
>
> /**
>
>
>
>
>
>
> *
>
>
>
>
>
> test
>
>
>
>
>
> clicking
>
>
>
>
>
> on
>
>
>
>
>
> a
>
>
>
>
>
> span
>
>
>
>
>
>
>
> *
>
>
>
>
>
> inspired
>
>
>
>
>
> by
>
>
>
>
>
> Mail
>
>
>
>
>
> from
>
>
>
>
>
> Christoph
>
>
>
>
>
> to
>
>
>
>
>
> developer
>
>
>
>
>
> mailinglist
>
>
>
>
>
> of
>
>
>
>
>
> 2008
>
>
> -
>
>
> 04
>
>
> -
>
>
> 01
>
>
>
>
>
>
>
> */
>
>
>
>
>
>
>
> public
>
>
>
>
>
> void
>
>
> testClickSpan()
>
>
> throws
>
>
> Exception {
>
>
>
>
> defineResource(
>
>
> "OnCommand.html"
>
>
> ,
>
>
> "<html><head><script language='JavaScript'>"
>
>
> +
>
>
>
>
>
>
>
> "function crtCtrla(obj,otherArg) {"
>
>
> +
>
>
>
>
>
>
>
> " alert(otherArg);"
>
>
> +
>
>
>
>
>
>
>
> "}"
>
>
> +
>
>
>
>
>
>
>
> "</script></head>"
>
>
> +
>
>
>
>
>
>
>
> "<body>"
>
>
> +
>
>
>
>
>
>
>
> "<table><tr><td class='cl'><span onClick='crtCtrla(this, \"rim_ModuleSearchResult=Drilldown=key_\", null, null);' class='feact'><span>489</span></span></td></tr></table>"
>
>
> +
>
>
>
>
>
>
>
> "</body></html>"
>
>
> );
>
>
>
>
> WebConversation wc =
>
>
> new
>
>
> WebConversation();
>
>
>
>
> WebResponse response = wc.getResponse( getHostPath() +
>
>
> "/OnCommand.html"
>
>
> );
>
>
>
>
> String elementNames[]=response.getElementNames();
>
>
>
>
> HTMLElement elements[]=response.getElementsByTagName(
>
>
> "span"
>
>
> );
>
>
>
>
> assertTrue(
>
>
> "Two span elements should be found "
>
>
> ,elements.
>
>
> length
>
>
> ==2);
>
>
>
>
> HTMLElement span1=elements[0];
>
>
>
>
> String onclick=
>
>
> "crtCtrla(this, \"rim_ModuleSearchResult=Drilldown=key_\", null, null);"
>
>
> ;
>
>
>
>
> assertEquals(span1.getAttribute(
>
>
> "onclick"
>
>
> ),onclick);
>
>
>
>
> span1.handleEvent(
>
>
> "onclick"
>
>
> );
>
>
>
>
> String alert=wc.popNextAlert();
>
>
>
>
> assertEquals(
>
>
> "function should have been triggered to alert"
>
>
> ,alert,
>
>
> "rim_ModuleSearchResult=Drilldown=key_"
>
>
> );
>
>
>
>
> elements=response.
>
> getElementsWithAttribute
>
> (
>
>
> "onclick"
>
>
> , onclick);
>
>
>
>
>
>
>
> int
>
>
> expected=1;
>
>
>
>
>
>
>
> //
>
>
> TODO
>
>
> check how 2 could be correct ...
>
>
>
>
> expected=2;
>
>
>
>
> assertTrue(expected+
>
>
> "elements should be found "
>
>
> ,elements.
>
>
> length
>
>
> ==expected);
>
>
>
>
> span1=elements[0];
>
>
>
>
> span1.handleEvent(
>
>
> "onclick"
>
>
> );
>
>
>
>
> alert=wc.popNextAlert();
>
>
>
>
> assertEquals(
>
>
> "function should have been triggered to alert"
>
>
> ,alert,
>
>
> "rim_ModuleSearchResult=Drilldown=key_"
>
>
> );
>
>
>
>
>
>
>
> //
>
>
> TODO
>
>
> remove this part
>
>
>
>
> span1=elements[1];
>
>
>
>
> span1.handleEvent(
>
>
> "onclick"
>
>
> );
>
>
>
>
> alert=wc.popNextAlert();
>
>
>
>
> assertEquals(
>
>
> "function should have been triggered to alert"
>
>
> ,alert,
>
>
> "rim_ModuleSearchResult=Drilldown=key_"
>
>
> );
>
>
>
>
> }
>
>
>
>
>
>
>
>
>
>
>
> BITPlan - smart solutions
>
> Pater-Delp-Str. 1, D-47877 Willich Schiefbahn
>
> Tel. +49 1805 - BITPLAN / +49 1805 248 752, Fax +49 2154 811-481
>
> Web: http://www.bitplan.de
>
> bitplan GmbH, Willich - HRB 6820 Krefeld, VAT-ID: 10258040548, Geschäftsführer: Wolfgang Fahl
>
>
>
> -----------------------------------------------------------------
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
>
> -----------------------------------------------------------------
> _______________________________________________
> Httpunit-develop mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/httpunit-develop
>
_______________________________________________________________
Schon gehört? Der neue WEB.DE MultiMessenger kann`s mit allen:
http://www.produkte.web.de/messenger/?did=3016
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.