⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testharness.js

📁 AJAX 应用 实现页面的无刷新
💻 JS
📖 第 1 页 / 共 4 页
字号:
                    testInterface.finishTestSuite();
                    runNextTestSuite();
                    return;
                }            
            }
            else
            {
                frame.registerTests(testHarness);
            }
            runNextTestCase();
        }
        else
        {
            // If there is no RegisterTests method, then we display
            // an error and move on to the next Test Suite
            fail('No registerTests method defined');
            testInterface.finishTestSuite();
            runNextTestSuite();
        }        
    }
        
    // Pull the next Test Case from the remaining list of Test Cases and
    // execute the Test Case by continually polling for its completion
    function runNextTestCase() {
        if (resumeFunctionRegistrationID != -1) {
        
              // Execute the test
            
            if (!getDebugMode()) {   
                // Continue executing the test
                try {
                    var testCase = testCases[currentTestCase];
                    testCase.generate();
                    var nextStep = registeredFunctions[resumeFunctionRegistrationID];
                    nextStep();
                } catch (ex) {
                    fail(ex);
                } finally {
                    waitForTestCase();
                }
            }
            else {
                    // Continue executing the test
                try {
                    var testCase = testCases[currentTestCase];
                    testCase.generate();
                    var nextStep = registeredFunctions[resumeFunctionRegistrationID];
                    nextStep();
                } 
                finally {
                    waitForTestCase();
                }
            }
    
    
        }  else if (currentTestCase + 1 < testCases.length && testCases.length > 0) {
            // Get the next Test Case
            currentTestCase++;
            var testCase = testCases[currentTestCase];
            
            // Create report elements for this Test Case
            testInterface.startTestCase(testCase);
            
            // Execute the test
            if (!getDebugMode()) {            
                try {
                    // Start the TestCase and keep checking until it's finished
                    testCase.execute();
                } catch (ex) {
                    // Report any error
                    fail(ex);
                } finally {
                    waitForTestCase();
                }
            }
            else {
                try {
                    // Start the TestCase and keep checking until it's finished
                    testCase.execute();
                } finally {
                    waitForTestCase();
                }
            }
            
        } else {
            currentTestCase = -1;
        
            // Update the report status when the entire Test Suite is finsihed
            testInterface.finishTestSuite();
            
            // Start running the next Test Suite
            runNextTestSuite();
        }
    }
    
    // Wait for the currently executing Test Case to complete any asynchronous
    // operations (by checking its completed flag)
    function waitForTestCase() {
        var testCase = testCases[currentTestCase];
        if (testCase.getCompleted()) { 
            
            window.clearInterval(waitIntervalID);
            waitIntervalID = null;
            
            testInterface.finishTestCase(testCase);
            
            resumeFunctionRegistrationID = -1;
            if (currentTestCase < testCases.length) {
                reloadTestWindow();
            }
        
            // Move to the next test case when this one is finished
            // runNextTestCase();
            // inc the counter and reload the page
        } else if (!waitIntervalID) {
            waitIntervalID = window.setInterval(waitForTestCase, constants.PollingInterval);
        }
    }
    
    // Report a test cancellation 
    function cancel(message) {
        // Mark the test as an unknown
        var testCase = testCases[currentTestCase];
        if (testCase != null) {
            testCase.setResult(testCase.ResultType.Unknown);
            testCase.setCompleted(true);
        }
        
        testInterface.cancel(message);
    }
    
    this.cancel = cancel;
   
    // Report a failure message and update the report status accordingly
    function fail(message) {
        // Mark the test as a failure
        var testCase = testCases[currentTestCase];
        if (testCase != null) {
            testCase.setResult(testCase.ResultType.Failure);
            testCase.setCompleted(true);
        }
                
        testInterface.fail(message);
    }
    
    this.fail = fail;
    
    function getFrame() {
        if (wndTest.registerTests) { 
            return wndTest;
        } else if (frameTest.registerTests) {
            return frameTest;
        } else {
            return null;
        }
    }
    
    this.getWindow = function() {
        var frame = getFrame();
        if (frame && frame.window) {
            return frame.window;
        }
        return null;
    }
    
    this.getDocument = function() {
        var frame = getFrame();
        if (frame && frame.document) {
            return frame.document;
        }
        return null;
    }
    
    // Lookup an element and raise an error if it was not found
    this.getElement = function(id) {
        var element = null;
        var frame = getFrame();
        if (frame && frame.document && frame.document.getElementById) {
            element = frame.document.getElementById(id);
        }
        this.assertNotNull(element, "Could not find control '" + id + "'");
        return element;
    }
    
    // Lookup an ASP.NET AJAX object and raise an error if it was not found
    this.getObject = function(id) {
        var element = null;
        var frame = getFrame();
        if (frame && frame.Sys && frame.Sys.Application &&
            frame.Sys.Application.findComponent) {
            element = frame.Sys.Application.findComponent(id);
        }
        this.assertNotNull(element, "Could not find object '" + id + "'");
        return element;
    }
    
    // Fire the specified event from the given source
    this.fireEvent = function(element, eventName)
    {
        if (document.createEventObject)
        {
            element.fireEvent(eventName);
        }
        else if (document.createEvent)
        {
            var eType;
            
            if (eventName === "onclick" || eventName.indexOf("mouse") >= 0) {
                eType = "MouseEvents";
            }
            else if (eventName.indexOf("key") >= 0) {
                eType = "KeyEvents";
            }
            else {
                eType = "HTMLEvents";
            }
            
            var e = document.createEvent(eType);
            e.initEvent(eventName.replace(/^on/, ""), true, true);
            element.dispatchEvent(e);
        }
        else
        {
            throw "This browser does not support event firing";
        }
    }
    
    // Assert that the condition is true
    this.assertTrue = function(condition, message) {
        if (!condition)
            throw (message ? 'Assertion failed: ' + message
                : 'Assertion failed');
    }
   
    // Assert that the condition is false
    this.assertFalse = function(condition, message) {
        this.assertTrue(!condition, message);
    }
    
    // Assert that object a equals object b
    this.assertEqual = function(a, b, message) {
        this.assertTrue(a === b,
            (message ? message : a + ' should equal ' + b));
    }
    
    // Assert that object a is not equal to object b
    this.assertNotEqual = function(a, b, message) {
        this.assertTrue(a !== b,
            (message ? message : a + ' should not equal ' + b));
    }
    
    // Assert that the value is null
    this.assertNull = function(value, message) {
        this.assertEqual(value, null,
            (message ? message : value + ' should be null'));
    }
    
    // Assert that the value is not null
    this.assertNotNull = function(value, message) {
        this.assertNotEqual(value, null,
            (message ? message : 'Value should be not be null'));
    }
}

// The TestResults class wraps the result tracking mechanism so we can
// easily report the 
function TestResults() {
    var successes = 0;
    var failures = 0;
    var unknown = 0;
            
    this.getSuccesses = function() { return successes; };
    this.addSuccess = function() { successes++; };
    
    this.getFailures = function() { return failures; };
    this.addFailure = function() { failures++; };
    
    this.getUnknown = function() { return unknown; };
    this.addUnknown = function() { unknown++; };
    
    this.reset = function() {
        successes = 0;
        failures = 0;
        unknown = 0;
    }
}


Function.prototype.registrationID = -1;

// TestCase represents an individual unit test comprised of a series of steps
// to be executed.  TestCases are generated through a sequence of calls to
// addStep or addPostback and then combined via buildTest into a new function
// that can be executed.  The steps can be one of the following types:
//  - Sync: Perform a simple atomic action
//  - Async Wait: Perform a simple atomic action that initiates an asynchronous
//    operation, allow a specified wait duration to elapse, and then perform
//    an atomic verification action of the asynchronous operation
//  - Async Check: Peform a simple atomic action that initiates an asynchronous
//    operation, continue to check if the operation has completed at a
//    specified interval, and then perform an atomic verification action of the
//    asynchronous operation.  There is also a specified timeout that prevents
//    the completion check from running forever.
//  - Postback: Force a postback and resume the test at the next step
// The result of running the TestCase is either Success, Failure, or Unknown.
// Since some of the TestCase steps run asynchronously, the result of the
// execution might not be available immediately afterwards.  You should instead
// wait until the Completed property has been set to true before assuming the
// test has finsihed.  If a postback occurs, execution will begin at the start
// of the next step. 
function TestCase() {
    // The ResultType "enumeration" describes the result of the TestCase's
    // execution.  A value of Unknown signifies that the test was not able to
    // run properly in the current browser.
    this.ResultType = {
        'Success' : 0,
        'Failure' : 1,
        'Unknown' : 2
    };

    // The StepType "enumeration" defines the possible types of steps that we
    // can use to build the test case
    var StepType = {
        'Sync' : 0,
        'AsyncWait' : 1,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -