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>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.