McMahon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks...

There was an interesting discussion about automating tests by driving the browser recently here: Using Perl to Test a Web App that uses Javascript

diotalevi and PodMaster imply that it is possible to actually execute the Javascript from a program, but after reading the documentation, I fail to understand how it's done.

Being a total Javascript lizard, I did a Super Search and then stole some excellent test code from vagnerr's scratchpad.

So my question is: is it possible to write a script in Perl that will automatically hit the "test1", "test2", etc. buttons by exercising the "onclick="javascript:addItem..." thingies in the code below?

<html> <head> <title>Test form</title> <script type="text/JavaScript"> <!-- function storeCaret (textEl){ if (textEl.createTextRange){ textEl.caretPos = document.selection.createRange().duplicate +(); } } function insertAtCaret (textEl, text){ if (textEl.createTextRange && textEl.caretPos) { var caretPos = textEl.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - +1) == ' ' ? text + ' ' : text; }else{ textEl.value += text; } } function addItem(label){ //document.display.showStuff.value += "%"+label+"%"; insertAtCaret(document.display.showStuff,label); } --> </script> </head> <body> <h1>Test Form</h1> <form name="display" method="post" action=""> <input type="button" value="test1" onclick="javascript:addItem(' +TEST')"> <input type="button" value="test2" onclick="javascript:addItem(' +ABCD')"> <input type="button" value="test3" onclick="javascript:addItem(' +RGFD')"> <input type="button" value="test4" onclick="javascript:addItem(' +HGFD')"> <br> <textarea name="showStuff" rows="5" cols="80" wrap="off" onselec +t="storeCaret(this);" ond blclick="storeCaret(this);" onclick="storeCaret(this);" onkeyup="store +Caret(this)"></textarea> </form> </body> </html>

Replies are listed 'Best First'.
Re: Further question: automating Javascript with Perl in browser?
by diotalevi (Canon) on Jun 11, 2004 at 16:05 UTC
    I said that it would be desirable for some JavaScript to execute from the context of something WWW::Mechanize-like. For thorough testing I eventually came around and thought that the browser itself has to be scripted since it is an integral part of the application being tested. If you are running this on a Win32 machine then you would use whatever COM hooks are required to drive Internet Explorer and then whatever else is appropriate for the other browsers.
Re: Further question: automating Javascript with Perl in browser?
by Fletch (Bishop) on Jun 11, 2004 at 16:02 UTC

    Use your favorite HTML parser to extract the onclick attributes, and then run the code through something like JavaScript::SpiderMonkey.

Re: Further question: automating Javascript with Perl in browser?
by PodMaster (Abbot) on Jun 11, 2004 at 18:32 UTC
      I think I understand the example-- it does "eval" on some Javascript and passes the results back to the script that contains the eval statement, yes?

      But for my original example, I don't see what code I would have to eval, such that I actually cause the Javascript to do what it would do if I were to click on it with a mouse. The ultimate goal is to have a Perl script cause the same behavior in the browser as the mouse does.

      Apologies if I seem really dense here, this web stuff is definitely not my forte.
Re: Further question: automating Javascript with Perl in browser?
by dba (Monk) on Jun 12, 2004 at 13:52 UTC
    WWW::Mechanize is your solution. click and click_button simulates exactly what happens when you 'click' on a button. Read this