Can you test JavaScript / JQuery code embedded in HTML page with Jasmine?
How can I use Jasmine or other tool to test the JavaScript / JQuery that is embedded inside a web page like in the example below?
<html>
<head>
<!-- usual includes -->
</head>
<body>
<span id="myspan1">&l开发者_如何学Got;/span>
<span id="myspan2"></span>
<script type="text/javascript">
$(document).ready(function(){
$('#myspan1').html('something');
$('#myspan1').click(function(){
$('#myspan1').html('something else');
});
doStuff();
function doStuff() {
$('#myspan2').html('stuff done');
}
});
</script>
</body>
</html>
I simply assume that you want to ensure that the "stuff done" appears on the page after a reasonable amount of time.
describe("My page", function () {
it("should say stuff done after max 500 ms", function () {
waits(function () {
return $('#myspan2').html() == 'stuff done';
}, 500);
});
});
http://jsfiddle.net/ahus1/QP2Pk/
Normally you would put your javascript code into a external file.
Then you can setup a test page that includes the minimum elements from the normal page, plus Jasmine.
In this test page I didn't wait to run Jasmine when the page was finally loaded, but works anyway.
Br Alexander.
精彩评论