Test the Web Forward

How to Write a Javascript Test

JavaScript Test Overview


  • JavaScript tests programmatically verify functionality
  • JavaScript tests have some advantages over Reftests
    • Robust
    • Flexible
    • Performant
    • Powerful
  • Reftests have some advantages over JavaScript tests
    • Verify complete rendering
    • Easy to understand
    • Works for clients that don't support JavaScript
    • Avoid introducing additional technologies (JS)

JavaScript Test Overview (cont'd)


  • JavaScript can be used in conjunction with Reftests
    • Test can have both a reference file and a JavaScript section
    • Reftest portion supports clients without script engines
  • Self-describing Reftests are the preferred test format
  • JavaScript tests should be used in the following scenarios:
    • Testing a JS API or behavior (ex: CSSOM spec)
    • Adding automation to Reftests
  • W3C provides a framework (testharness.js) to simplify and standardize JS test creation

testharness

testharness.js and testharnessreport.js

    			            
    <script src="/resources/testharness.js"></script>

    <script src="/resources/testharnessreport.js"></script>
                            
                        
  • JavaScript API for making common test assertions
  • Should be used by all JavaScript tests
  • Path must be to /resources directory at root level
  • Element with id="log" should exist in test file
    • Test results table will be added to this element, if it exists

Body

			            
<body>
    <div id="myDiv"></div>
    <div id="log"></div>    

    <script>
        // Set the transform 
        document.getElementById("myDiv").style.transform = "translate(100px,100px)";

        // Verify that the transform was set as expected
        test(function() {assert_equals(
            document.getElementById("myDiv").style.getPropertyCSSValue("transform").cssText, //Actual
            "translate(100px, 100px)",  //Expected
            "transform should be translate(100px,100px)")}, //Description
            "Transform_Translate_100px_100px"); //name
    </script>
</body>			                
			            
			        
  • myDiv div used as test element - translate applied in JS
  • log div used as container for test results
  • test() uses assert_equals as test_function
  • assert_equals actual argument supplied by API call

Test Results

PASS

Test Results

FAIL

Questions & Answers