- 两个文件: 测试文件 + 参考文件
- 测试文件使用了待测功能
- 参考文件没有使用待测功能,但在视觉上是一样的
- 自描述文本(手工和自动测试都适用)
- 跨浏览器并且跨平台
CSS3 Transforms规范中transform属性的一个基本测试:使用translate()
函数
规范链接:
http://www.w3.org/TR/css3-transforms/#transform-property
http://www.w3.org/TR/css3-transforms/#two-d-transform-functions
测试文件
<!DOCTYPE html>
<html>
<head>
<title>CSS Transforms Test: transform property with translate function</title>
. . .
参考文件
<!DOCTYPE html>
<html>
<head>
<title>CSS Transforms Reference file</title>
. . .
测试文件
<!DOCTYPE html>
<html>
<head>
. . .
<link rel="author" title="Your Name" href="mailto:youremail@address.com">
. . .
参考文件
<!DOCTYPE html>
<html>
<head>
. . .
<link rel="author" title="Your Name" href="mailto:youremail@address.com">
. . .
仅用于测试文件
<!DOCTYPE html>
<html>
<head>
. . .
<link rel="help" href="http://www.w3.org/TR/css3-transforms/#transform-property">
<link rel="help" href="http://www.w3.org/TR/css3-transforms/#two-d-transform-functions">
. . .
仅用于测试文件
<!DOCTYPE html>
<html>
<head>
. . .
<link rel="match" href="reference/ttwf-Reftest-tutorial-ref.html">
. . .
仅用于测试文件
<!DOCTYPE html>
<html>
<head>
. . .
<meta name="assert"
content="This transform moves the element by 100 pixels in both the X and Y directions.">
. . .
大家尽量把这个写得完整一些,便于在测试失败时分析原因
应用transform的测试元素
<style type="text/css">
.greenSquare {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
background: green;
transform: translate(100px,100px);
}
. . .
</style>
参考元素
<style type="text/css">
.greenSquare {
position: absolute;
top: 100;
left: 100;
width: 100px;
height: 100px;
background: green;
}
</style>
元素只在测试失败时可见
<style type="text/css">
. . .
.redSquare {
position: absolute;
top: 101;
left: 101;
width: 98px;
height: 98px;
background: red;
}
</style>
测试失败的例子
当测试通过时页面应该如何渲染的简单句子
测试和参考文件
<body>
<p>The test passes if there is a green square and no red.</p>
. . .
</body>
测试文件
<body>
. . .
<div class="redSquare"></div>
<div class="greenSquare"></div>
</body>
参考文件
<body>
. . .
<div class="greenSquare"></div>
</body>
- 你可能需要给测试中的CSS属性添加提供商前缀
- 如果前缀是必要的,那么没有的话测试显然会失败
- 但是,当把测试提交到W3C时不能包含这些前缀
.greenSquare {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
-webkit-transform: translate(100px,100px);
-moz-transform: translate(100px,100px);
-ms-transform: translate(100px,100px);
-o-transform: translate(100px,100px);
background: green;
}
在测试开发时我们推荐使用的脚本:
http://leaverou.github.com/prefixfree
prefixfree.jsapi.js扩展可用于无前缀的JS API测试:
https://github.com/LeaVerou/prefixfree/tree/gh-pages/plugins
. . .
<-- Remember to remove before pushing to W3C test suite -->
<script src="prefixfree.js"></script>
<script src="plugins/prefixfree.jsapi.js"></script>
. . .
testharness.js 和 testharnessreport.js
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
id="log"
的元素
test(test_function, name, properties)
async_test()
是用于异步测试的test()
的调用test()
调用会显示PASS/FAIL信息
test(test_function, name, properties)
test_function
必须是一个对象,不是一个函数调用test_function
应该包含测试断言方法
test(function() {assert_true(true)}, name, properties)
test(test_function, name, properties)
name
是用于标识测试的字符串,比如“Transform_Translate_100px_100px”name
应该简短,唯一,并且不会改变name
会显示在测试结果表格里
test(test_function, name, properties)
test(function() { assert_true(true) }, "test_name",
{timeout:1000,
help: 'http://www.w3.org/TR/spec#section',
author: ['Bill Gates <bgates@msn.com>', 'Steve Jobs http://apple.com/sjobs'],
assert: 'This test something.', 'This also tests something else.'})
test()的调用中
description
参数,只在断言失败的时候才输出,用于提供额外的调试信息
assert_equals(actual, expected, description)
actual
: 所测功能的实际值expected
: 期望值description
(可选): 仅在断言失败时输出
assert_equals(getActualData("myElement"), 100, "Expected value for myElement is 100")
CSS3 Transforms规范中transform属性的一个基本测试:使用translate()
函数
规范链接:
http://www.w3.org/TR/css3-transforms/#transform-property
http://www.w3.org/TR/css3-transforms/#two-d-transform-functions
<head>
<title>CSS Transforms API Test: transform translate</title>
<link rel="author" title="Your Name" href="mailto:youremail@address.com">
<link rel="help" href="http://www.w3.org/TR/css3-transforms/#transform-property">
<link rel="help"
href="http://www.w3.org/TR/css3-transforms/#two-d-transform-functions">
<meta name="flags" content="dom">
<meta name="assert" content="The transform should be translate(100px,100px)">
...
</head>
flags
中的dom
指明了JavaScript的支持是需要的
<head>
...
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
/resources
目录
<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.getPropertyValue("transform"), //Actual
"translate(100px, 100px)", //Expected
"transform should be translate(100px,100px)")}, //Description
"Transform_Translate_100px_100px"); //name
</script>
</body>
myDiv
用作测试元素 - 通过JS应用translatelog
用作测试结果容器test()
用 assert_equals
作为 test_function
通过
失败