I wrote the previous posts in this series over on EvilTester:
- Test Driven JavaScript using QUnit
- Test Driven JavaScript Code Coverage using JSCoverage
- Functional testing JavaScript with QUnit - initial steps
The following code only works in IE and generates a click event to trigger my button:
test("process eprime text by clicking on button",function(){
expect(3);
document.getElementById("inputtext").value=
"Surely and \nIsn't this being" +
" some bad text or rather Bob's WASn't he's being good";
var clickevent=document.createEvent('MouseEvents')
clickevent.initEvent('click', true, true)
document.getElementById("CheckForEPrimeButton").dispatchEvent(clickevent)
equals(document.getElementById("wordCount").innerHTML,
15,"displayed wordCount = 15");
equals(document.getElementById("discouragedWordCount").innerHTML,
4,"displayed discouragedWordCount = 4");
equals(document.getElementById("possibleViolationCount").innerHTML,
2,"displayed possibleViolationCount = 2");
});
Sadly it fails in FireFox, but when I introduce the JQuery
test("process eprime text by clicking on button using JQuery",function(){
expect(3);
document.getElementById("inputtext").value=
"Surely and \nIsn't this being" +
" some bad text or rather Bob's WASn't he's being good";
$("#CheckForEPrimeButton").click();
equals(document.getElementById("wordCount").innerHTML,
15,"displayed wordCount = 15");
equals(document.getElementById("discouragedWordCount").innerHTML,
4,"displayed discouragedWordCount = 4");
equals(document.getElementById("possibleViolationCount").innerHTML,
2,"displayed possibleViolationCount = 2");
});
Voila, cross-browser functional testing and less code to boot.
No comments:
Post a Comment