简体   繁体   中英

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"></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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM