Re: Execute Javascript of span-element

"Wolfgang Fahl" <[email protected]>
Newsgroups gmane.comp.web.httpunit.devel
Organization BITPlan GmbH
Message-ID <[email protected]>
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
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.