YUI Library JavaScript DHTML





    
Asynchronous Testing

/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}










Asynchronous Testing



  

This example shows how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code. 
  A TestCase object is created with a test that waits for a
  few seconds before continuing. The TestRunner
  is then used to run the tests once the page has loaded.

      




    YAHOO.namespace("example.yuitest");
    
    YAHOO.example.yuitest.AsyncTestCase = new YAHOO.tool.TestCase({
    
        //name of the test case - if not provided, one is auto-generated
        name : "Asynchronous Tests",
        
        //---------------------------------------------------------------------
        // setUp and tearDown methods - optional
        //---------------------------------------------------------------------
        
        /*
         * Sets up data that is needed by each test.
         */
        setUp : function () {
            this.data = {
                name: "yuitest",
                year: 2007,
                beta: true
            };
        },
        
        /*
         * Cleans up everything that was created by setUp().
         */
        tearDown : function () {
            delete this.data;
        },
                
        //---------------------------------------------------------------------
        // Test methods - names must begin with "test"
        //---------------------------------------------------------------------
        
        testWait : function (){
            var Assert = YAHOO.util.Assert;
            
            //do some assertions now
            Assert.isTrue(this.data.beta);
            Assert.isNumber(this.data.year);
            
            //wait five seconds and do some more
            this.wait(function(){
            
                Assert.isString(this.data.name);                
            
            }, 5000);
        
        }
                    
    });
     
    YAHOO.util.Event.onDOMReady(function (){
        //create the logger
        var logger = new YAHOO.tool.TestLogger("testLogger");
        YAHOO.tool.TestRunner.add(YAHOO.example.yuitest.AsyncTestCase);
        //run the tests
        YAHOO.tool.TestRunner.run();
    });





   
  
yui_2.7.0b.zip( 4,431 k)