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

📄 testharness.js

📁 AJAX 应用 实现页面的无刷新
💻 JS
📖 第 1 页 / 共 4 页
字号:
                            remaining -= step.Interval;
                            if (!invoke(step.CheckComplete) && remaining > 0) {
                                if (!completed) {
                                    window.setTimeout(f, step.Interval);
                                }
                                return;
                            } else if (remaining <= 0) {
                                fail('Timeout exceeded');
                            }
                            if (!completed) {
                                invoke(step.Verification);
                                last();
                            }
                        };
                        window.setTimeout(f, step.Interval);
                    }
                };
                break;
            case StepType.PostBack :
                if (step.Element) {
                    test = function() {
                        if (!completed) {
                            harness.registerOnResumeFunction(last);
                            currentStep = steps.length + 1;
                            if (step.Element.click) {
                                // calling safeInvoke here would blow up Firefox
                                step.Element.click();
                            } else if (step.Element.submit) {
                                // calling safeInvoke here would blow up Firefox
                                step.Element.submit();
                            } else if (window && window.forms &&
                                window.forms[0] && window.forms[0].submit) {
                                // calling safeInvoke here would blow up Firefox
                                window.forms[0].submit();
                            } else {
                                fail('Unable to force a postback');
                            }
                        }
                    };
                } else {
                    test = function() {
                        if (!completed) {
                            harness.registerOnResumeFunction(last);
                            currentStep = steps.length + 1;
                            if (window && window.forms &&
                                window.forms[0] && window.forms[0].submit) {
                                // calling safeInvoke here would blow up Firefox
                                window.forms[0].submit();
                            } else {
                                fail('Unable to force a postback');
                            }
                        }
                    };
                }
                break;
            default :
                test = last;
                break;
        }
        
        if (test != last) {
            harness.registerTestFunction(test);
        }
        
        return buildTest(steps, test);
    }
    
    // The Completed property is a flag used to determine whether or not an 
    // asynchronous test has fully completed its execution
    this.getCompleted = function() { return completed; };
    this.setCompleted = function(value) { completed = value; };
    
    // The Name of the TestCase
    this.getName = function() { return name; };
    this.setName = function(value) { name = value; };
    
    // The Result of the TestCase
    this.getResult = function() { return result; };
    this.setResult = function(value) { result = value; };
}

// The TestInterface contains all the logic used to 
function TestInterface() {
    var harness = null;
    
    var totalResults = null;
    var suiteResults = null;
    
    // References to controls in the page
    var results = null;         // div containing the results of the test run
    var status = null;          // div describing the status of the test run
    var statusPassed = null;    // span with the number of passed tests
    var statusFailed = null;    // span with the number of failed tests
    var statusUnknown = null;   // span with the number of unknown test results
    
    // References to dynamically generated UI elements that are used to report
    // the detailed results of the test run.  These variables are references to
    // the elements for the currently executing Test Cases and Test Suites, and
    // are set to new values as we iterate through lists of each.
    var testSuiteDiv = null;      // div for the currently executing Test Suite
    var testSuiteHeaderDiv = null;// div for the name of the current Test Suite
    var testCaseDiv = null;       // div for the currently executing Test Case
    var testCaseHeaderDiv = null; // div for the name of the current Test Case

    // Initialize the interface
    this.initialize = function(testHarness) {
        harness = testHarness;
        
        totalResults = new TestResults();
        suiteResults = new TestResults();
    
        results = window.document.getElementById('results');
        if (!results) {
            throw "Could not find div 'results' on page!";
        }
        
        status = window.document.getElementById('status');
        if (!status) {
            throw "Could not find div 'status' on page!";
        }
        
        statusPassed = window.document.getElementById('statusPassed');
        if (!statusPassed) {
            throw "Could not find span 'statusPassed' on page!";
        }
        
        statusFailed = window.document.getElementById('statusFailed');
        if (!statusFailed) {
            throw "Could not find span 'statusFailed' on page!";
        }
        
        statusUnknown = window.document.getElementById('statusUnknown');
        if (!statusUnknown) {
            throw "Could not find span 'statusUnknown' on page!";
        }
    }
    
    this.startTestPass = function() {
        // Clear the current test results
        for (var i = results.childNodes.length - 1; i >= 0; i--)
            results.removeChild(results.childNodes[i]);
        
        // Reset the total result tracking
        totalResults.reset();
    
        // Reset the status indicator panel
        statusPassed.innerHTML = "0";
        statusPassed.style.color = harness.Constants.SuccessForeColor;
        statusFailed.innerHTML = "0";
        statusFailed.style.color = harness.Constants.FailureForeColor;
        statusUnknown.innerHTML = "0";
        statusUnknown.style.color = harness.Constants.UnknownForeColor;
        status.style.backgroundColor = 'white';
        status.style.display = 'block';
    }
    
    this.finishTestPass = function() {
        // Update the status mechanisms with numbers and background colors
        this.updateResults();
        
        var backColor;
        if (totalResults.getFailures() > 0) {
            backColor = harness.Constants.FailureBackColor;
        } else if (totalResults.getUnknown() > 0) {
            backColor = harness.Constants.UnknownBackColor;
        } else if (totalResults.getSuccesses() > 0) {
            backColor = harness.Constants.SuccessBackColor;
        }
        status.style.backgroundColor = backColor;
    }
    
    this.updateResults = function() {
        // Update the status counters
        statusPassed.innerHTML = totalResults.getSuccesses();
        statusFailed.innerHTML = totalResults.getFailures();
        statusUnknown.innerHTML = totalResults.getUnknown();
    
        // Change the colors of the current Test Suite or the status indicator
        // panel as soon as any failure occurs
        if (testSuiteHeaderDiv && suiteResults.getFailures() > 0) {
            testSuiteHeaderDiv.style.backgroundColor =
                harness.Constants.FailureBackColor;
        }
        if (totalResults.getFailures() > 0) {
            status.style.backgroundColor =
                harness.Constants.FailureBackColor;
        }    
    }
    
    this.startTestSuite = function(url) {
        suiteResults.reset();
        writeTestSuite(url);
    }
    
    this.finishTestSuite = function() {
        this.updateResults();
    
        var backColor;
        if (suiteResults.getFailures() > 0) {
            backColor = harness.Constants.FailureBackColor;
        } else if (suiteResults.getUnknown() > 0) {
            backColor = harness.Constants.UnknownBackColor;
        } else if (suiteResults.getSuccesses() > 0) {
            backColor = harness.Constants.SuccessBackColor;
        }
        testSuiteHeaderDiv.style.backgroundColor = backColor;
        
        testSuiteDiv = null;
        testSuiteHeaderDiv = null;
    }
    
    this.startTestCase = function(testCase) {
        writeTestCase(testCase);
    }
    
    this.finishTestCase = function(testCase) {
        if (!testCaseHeaderDiv || !testCase) {
            return;
        }
    
        // Mark the test as a success if it didn't fail or cancel
        switch (testCase.getResult()) {
            case testCase.ResultType.Success :
                suiteResults.addSuccess();
                totalResults.addSuccess();
                testCaseHeaderDiv.style.color =
                    harness.Constants.SuccessForeColor;
                this.updateResults();
                break;
            case testCase.ResultType.Failure :
                testCaseHeaderDiv.style.color =
                    harness.Constants.FailureForeColor;
                break;
            case testCase.ResultType.Unknown :
            default :
                testCaseHeaderDiv.style.color =
                    harness.Constants.UnknownForeColor;
                break;
        }
        
        
        // Wipe the report elements for previous test cases
        testCaseDiv = null;
        testCaseHeaderDiv = null;
     }
    
    // writeTestSuite generates the report elements for the currently
    // executing Test Suite
    function writeTestSuite(url) {
        // Create the Test Suite's container
        testSuiteDiv = window.document.createElement("div");
        testSuiteDiv.className = 'testSuite';
        results.appendChild(testSuiteDiv);
        
        // Create the Test Suite's header
        testSuiteHeaderDiv = window.document.createElement("div");
        testSuiteHeaderDiv.className = 'testSuiteHeader';
        testSuiteHeaderDiv.innerHTML = url.replace('.aspx', '').replace(/\?t=(.*)/g,'');
        testSuiteDiv.appendChild(testSuiteHeaderDiv);
        
        // Wipe the report elements for any previous Test Case
        testCaseDiv = null;
        testCaseHeaderDiv = null;
    }
    
    // writeTestCase generates the report elements for the currently
    // executing Test Case
    function writeTestCase(testCase) {
        // Create the Test Cases's container
        testCaseDiv = window.document.createElement("div");
        testCaseDiv.className = 'testCase';
        testSuiteDiv.appendChild(testCaseDiv);
        
        // Create the Test Cases's header
        testCaseHeaderDiv = window.document.createElement("div");
        testCaseHeaderDiv.className = 'testCaseHeader';
        testCaseHeaderDiv.innerHTML = testCase.getName();
        testCaseDiv.appendChild(testCaseHeaderDiv);
    }
    
    // Write a message under the current Test Suite or Test Case, and
    // optionally provide fore and back colors for the message
    this.write = function(message, foreColor, backColor) {
        // Create the message
        var div = window.document.createElement("div");
        div.innerHTML = message;
        if (foreColor) {
            div.style.color = foreColor;
        }
        if (backColor) {
            div.style.backgroundColor = backColor;
        }
        
        // Place it in the deepest nested scope possible
        if (testCaseDiv) {
            div.className = 'testCaseMessage';
            testCaseDiv.appendChild(div);
        } else if (testSuiteDiv) {
            div.className = 'testSuiteMessage';
            testSuiteDiv.appendChild(div);
        } else {
            div.className = 'testSuiteMessage';
            results.appendChild(div);
        }
    }
    
    this.fail = function(message) {
        suiteResults.addFailure();
        totalResults.addFailure();
        this.write(message ? message : "Test failure",
            harness.Constants.FailureForeColor, null);
        
    }
    
    this.cancel = function(message) {
        suiteResults.addUnknown();
        totalResults.addUnknown();
        this.write(message ? message : "Test cancelled");
    }
}

// Create a global Test Harness used to load and execute the selected test
// suites (we use this instance to wireup events in TestHarness.aspx)
var testHarness = new TestHarness();

⌨️ 快捷键说明

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