• Share DOM Fixtures between JavaScript Unit Tests - [Javascript]

    2009-12-21

    For the JavaScript unit tests that operate DOM, it's quite handy if the DOM fixtures could be shared between tests. This involves how to load another HTML file from one HTML file for most of the JavaScript testing frameworks (like Screw.Unit and JSUnit), the prior file is the DOM fixture and the later file hosts the test codes.

    This blog demonstrates a way to do it through jQuery synchronized Ajax call. Below is the code skeleton of a test file:

    <html>
      <head>
        <script type="text/javascript" src="lib/jquery-1.3.2.js"></script>
      </head>
    
      <body>
        <div id="fixture">
          <script type="text/javascript">
            $.ajaxSetup({ async: false }); // Ajax will run in sync
            $("#fixture").load("fixtures/DOMFixture.html"); // replace inner html with loaded content
          </script>
        </div>
    
        <script type="text/javascript">
          // tests go here
        </script>
      </body>
    </html>
    

    This works for all the mainstream browsers including Firefox and IE.

    Another possible approach that I haven't verified in detail is that 1) create XML document element dynamically, 2) load fixture file to the element synchronously, then 3) append the element to the place holder DIV in test file.

     


    收藏到:Del.icio.us