• 页面UI测试
    • 使用 torchjs(推荐)
      • 安装
      • 配置
    • 使用 UITest
      • 安装
      • 用法
      • Node.js
      • Gulp

    页面UI测试

    使用 torchjs(推荐)

    Test framework to light up the world.

    页面UI测试 - 图1

    安装

    1. $ npm i torchjs --save-dev

    配置

    1. $ torch --help
    2. Usage: torch [options]
    3. Options:
    4. -h, --help output usage information
    5. -V, --version output the version number
    6. -C, --no-colors force disabling of colors
    7. -O, --reporter-options <k=v,k2=v2,...> reporter-specific options
    8. -R, --reporter <name> specify the reporter to use
    9. -S, --sort sort test files
    10. -b, --bail bail after first test failure
    11. -g, --grep <pattern> only run tests matching <pattern>
    12. -f, --fgrep <string> only run tests containing <string>
    13. -i, --invert inverts --grep and --fgrep matches
    14. -r, --require <name> require the given module
    15. -s, --slow <ms> "slow" test threshold in milliseconds [75]
    16. -t, --timeout <ms> set test-case timeout in milliseconds [2000]
    17. -u, --ui <name> specify user-interface (bdd|tdd|exports)
    18. --check-leaks check for global variable leaks
    19. --compile compile with babel
    20. --compile-opts <path> path of compile options
    21. --compilers <ext>:<module>,... use the given module(s) to compile files
    22. --coverage report coverage
    23. --debug enable Electron debugger on port [5858]; for --renderer tests show window and dev-tools
    24. --debug-brk like --debug but pauses the script on the first line
    25. --globals <names> allow the given comma-delimited global [names]
    26. --inline-diffs display actual/expected differences inline within each string
    27. --interactive run tests in renderer process in a visible window that can be reloaded to re-run tests
    28. --interfaces display available interfaces
    29. --no-timeouts disables timeouts
    30. --notify-on-fail notify on failures
    31. --notify-on-success notify on success
    32. --opts <path> specify opts path
    33. --preload <name> preload the given script in renderer process
    34. --recursive include sub directories
    35. --renderer run tests in renderer process
    36. --require-main <name> load the given script in main process before executing tests
    37. --source-pattern <sources> glob pattern of source files
    38. --watch watching source file changes
    39. --watch-aggregate-timeout delay time for re-run test cases after files changed

    更多配置

    页面UI测试 - 图2

    页面UI测试 - 图3


    使用 UITest

    安装

    1. $ npm i uitest --save-dev

    用法

    页面UI测试 - 图4

    直接引入 uitest-mocha-shim.js 文件即可在浏览器中运行。

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title>macaca mocha test</title>
    5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <link rel="stylesheet" href="./node_modules/mocha/mocha.css" />
    8. </head>
    9. <body>
    10. <div id="mocha"></div>
    11. <script src="./node_modules/mocha/mocha.js"></script>
    12. <script src='./node_modules/uitest/uitest-mocha-shim.js'></script>
    13. <script src="./node_modules/should/should.js"></script>
    14. <script>
    15. _macaca_uitest.setup({
    16. ui: 'bdd',
    17. timeout: 5000,
    18. slow: 2000
    19. });
    20. </script>
    21. <script>
    22. describe('sample', function() {
    23. beforeEach('init', function() {
    24. });
    25. it('#case_1', function() {
    26. });
    27. });
    28. </script>
    29. <script>
    30. _macaca_uitest.run();
    31. </script>
    32. </body>
    33. </html>

    Node.js

    页面UI测试 - 图5

    在命令行里运行

    1. const uitest = require('uitest');
    2. uitest({
    3. url: 'file:///Users/name/path/index.html',
    4. width: 600,
    5. height: 480,
    6. hidpi: false,
    7. useContentSize: true,
    8. show: false,
    9. }).then(() => {
    10. console.log('uitest success')
    11. }).catch(() => {
    12. console.log('uitest error')
    13. });

    Gulp

    也可以通过 Gulp 引入:

    1. $ npm i gulp-uitest --save-dev
    1. const uitest = require('gulp-uitest');
    2. //test
    3. gulp.task('test', function() {
    4. return gulp
    5. .src('test/html/index.html')
    6. .pipe(uitest({
    7. width: 600,
    8. height: 480,
    9. hidpi: false,
    10. useContentSize: true,
    11. show: false,
    12. }));
    13. });
    • 更多关于 UITest
    • 示例:uitest-sample
    • 游戏框架Hilo测试示例: Hilo Game
    • 游戏框架pillow测试示例: Hilo Game
    • canvas框架monitor.js测试示例: Hilo Game

    原文: https://macacajs.github.io/zh/pageuitest