使用 Headless Chrome 进行自动化测试

如果你想使用 Headless Chrome 进行自动化测试,那么就往下!这篇文章将让你完全使用 Karma 作为运行器runner,并且使用 Mocha+Chai 来编撰测试。

这些东西是什么?

Karma、Mocha、Chai、Headless Chrome,哦,我的天哪!

Karma 是一个测试工具,可以和所有最流行的测试框架(JasmineMocha、 QUnit)配合使用。

Chai 是一个断言库,可以与 Node 和浏览器一起使用。这里我们需要后者。

Headless Chrome 是一种在没有浏览器用户界面的无需显示环境中运行 Chrome 浏览器的方法。使用 Headless Chrome(而不是直接在 Node 中测试) 的一个好处是 JavaScript 测试将在与你的网站用户相同的环境中执行。Headless Chrome 为你提供了真正的浏览器环境,却没有运行完整版本的 Chrome 一样的内存开销。

 

设置

 

安装

使用 yarn 安装 Karma、相关插件和测试用例:

  1. <span class="pln">yarn add </span><span class="pun">--</span><span class="pln">dev karma karma</span><span class="pun">-</span><span class="pln">chrome</span><span class="pun">-</span><span class="pln">launcher karma</span><span class="pun">-</span><span class="pln">mocha karma</span><span class="pun">-</span><span class="pln">chai</span>
  2. <span class="pln">yarn add </span><span class="pun">--</span><span class="pln">dev mocha chai</span>

或者使用 npm

  1. <span class="pln">npm i </span><span class="pun">--</span><span class="pln">save</span><span class="pun">-</span><span class="pln">dev karma karma</span><span class="pun">-</span><span class="pln">chrome</span><span class="pun">-</span><span class="pln">launcher karma</span><span class="pun">-</span><span class="pln">mocha karma</span><span class="pun">-</span><span class="pln">chai</span>
  2. <span class="pln">npm i </span><span class="pun">--</span><span class="pln">save</span><span class="pun">-</span><span class="pln">dev mocha chai</span>

在这篇文章中我使用 MochaChai,但是你也可以选择自己最喜欢的在浏览器中工作的断言库。

 

配置 Karma

创建一个使用 ChromeHeadless 启动器的 karma.config.js 文件。

karma.conf.js

  1. <span class="kwd">module</span><span class="pun">.</span><span class="pln">exports </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">config</span><span class="pun">)</span><span class="pun">{</span>
  2. <span class="pln">  config</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">({</span>
  3. <span class="pln">    frameworks</span><span class="pun">:</span><span class="pun">[</span><span class="str">'mocha'</span><span class="pun">,</span><span class="str">'chai'</span><span class="pun">],</span>
  4. <span class="pln">    files</span><span class="pun">:</span><span class="pun">[</span><span class="str">'test/**/*.js'</span><span class="pun">],</span>
  5. <span class="pln">    reporters</span><span class="pun">:</span><span class="pun">[</span><span class="str">'progress'</span><span class="pun">],</span>
  6. <span class="pln">    port</span><span class="pun">:</span><span class="lit">9876</span><span class="pun">,</span><span class="pln">  </span><span class="com">// karma web server port</span>
  7. <span class="pln">    colors</span><span class="pun">:</span><span class="kwd">true</span><span class="pun">,</span>
  8. <span class="pln">    logLevel</span><span class="pun">:</span><span class="pln"> config</span><span class="pun">.</span><span class="pln">LOG_INFO</span><span class="pun">,</span>
  9. <span class="pln">    browsers</span><span class="pun">:</span><span class="pun">[</span><span class="str">'ChromeHeadless'</span><span class="pun">],</span>
  10. <span class="pln">    autoWatch</span><span class="pun">:</span><span class="kwd">false</span><span class="pun">,</span>
  11. <span class="pln">    </span><span class="com">// singleRun: false, // Karma captures browsers, runs the tests and exits</span>
  12. <span class="pln">    concurrency</span><span class="pun">:</span><span class="kwd">Infinity</span>
  13. <span class="pln">  </span><span class="pun">})</span>
  14. <span class="pun">}</span>

注意: 运行 ./node_modules/karma/bin/karma init karma.conf.js 生成 Karma 的配置文件。

 

写一个测试

/test/test.js 中写一个测试:

/test/test.js

  1. <span class="pln">describe</span><span class="pun">(</span><span class="str">'Array'</span><span class="pun">,</span><span class="pun">()</span><span class="pun">=></span><span class="pun">{</span>
  2. <span class="pln">  describe</span><span class="pun">(</span><span class="str">'#indexOf()'</span><span class="pun">,</span><span class="pun">()</span><span class="pun">=></span><span class="pun">{</span>
  3. <span class="pln">    it</span><span class="pun">(</span><span class="str">'should return -1 when the value is not present'</span><span class="pun">,</span><span class="pun">()</span><span class="pun">=></span><span class="pun">{</span>
  4. <span class="pln">      </span><span class="kwd">assert</span><span class="pun">.</span><span class="pln">equal</span><span class="pun">(-</span><span class="lit">1</span><span class="pun">,</span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">].</span><span class="pln">indexOf</span><span class="pun">(</span><span class="lit">4</span><span class="pun">));</span>
  5. <span class="pln">    </span><span class="pun">});</span>
  6. <span class="pln">  </span><span class="pun">});</span>
  7. <span class="pun">});</span>

 

运行你的测试

在我们设置好用于运行 Karma 的 package.json 中添加一个测试脚本。

package.json

  1. <span class="str">"scripts"</span><span class="pun">:</span><span class="pun">{</span>
  2. <span class="pln">  </span><span class="str">"test"</span><span class="pun">:</span><span class="str">"karma start --single-run --browsers ChromeHeadless karma.conf.js"</span>
  3. <span class="pun">}</span>

当你运行你的测试(yarn test)时,Headless Chrome 会启动并将运行结果输出到终端:

使用 Headless Chrome 进行自动化测试

 

创建你自己的 Headless Chrome 启动器

ChromeHeadless 启动器非常棒,因为它可以在 Headless Chrome 上进行测试。它包含了适合你的 Chrome 标志,并在端口 9222 上启动 Chrome 的远程调试版本。

但是,有时你可能希望将自定义的标志传递给 Chrome 或更改启动器使用的远程调试端口。要做到这一点,可以通过创建一个 customLaunchers 字段来扩展基础的 ChromeHeadless 启动器:

karma.conf.js

  1. <span class="kwd">module</span><span class="pun">.</span><span class="pln">exports </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">config</span><span class="pun">)</span><span class="pun">{</span>
  2. <span class="pln">  </span><span class="pun">...</span>
  3. <span class="pln">  config</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">({</span>
  4. <span class="pln">    browsers</span><span class="pun">:</span><span class="pun">[</span><span class="str">'Chrome'</span><span class="pun">,</span><span class="str">'ChromeHeadless'</span><span class="pun">,</span><span class="str">'MyHeadlessChrome'</span><span class="pun">],</span>
  5. <span class="pln">    customLaunchers</span><span class="pun">:</span><span class="pun">{</span>
  6. <span class="pln">      </span><span class="typ">MyHeadlessChrome</span><span class="pun">:</span><span class="pun">{</span>
  7. <span class="pln">        base</span><span class="pun">:</span><span class="str">'ChromeHeadless'</span><span class="pun">,</span>
  8. <span class="pln">        flags</span><span class="pun">:</span><span class="pun">[</span><span class="str">'--disable-translate'</span><span class="pun">,</span><span class="str">'--disable-extensions'</span><span class="pun">,</span><span class="str">'--remote-debugging-port=9223'</span><span class="pun">]</span>
  9. <span class="pln">      </span><span class="pun">}</span>
  10. <span class="pln">    </span><span class="pun">},</span>
  11. <span class="pln">  </span><span class="pun">}</span>
  12. <span class="pun">};</span>

 

完全在 Travis CI 上运行它

在 Headless Chrome 中配置 Karma 运行测试是很困难的。而在 Travis 中持续集成就只有几种!

要在 Travis 中运行测试,请使用 dist: trusty 并安装稳定版 Chrome 插件:

.travis.yml

  1. <span class="pln">language</span><span class="pun">:</span><span class="pln"> node_js</span>
  2. <span class="pln">node_js</span><span class="pun">:</span>
  3. <span class="pln">  </span><span class="pun">-</span><span class="str">"7"</span>
  4. <span class="pln">dist</span><span class="pun">:</span><span class="pln"> trusty </span><span class="com">#</span><span class="pln"> needs </span><span class="typ">Ubuntu</span><span class="typ">Trusty</span>
  5. <span class="kwd">sudo</span><span class="pun">:</span><span class="kwd">false</span><span class="pln">  </span><span class="com">#</span><span class="kwd">no</span><span class="pln"> need </span><span class="kwd">for</span><span class="pln"> virtualization</span><span class="pun">.</span>
  6. <span class="pln">addons</span><span class="pun">:</span>
  7. <span class="pln">  chrome</span><span class="pun">:</span><span class="pln"> stable </span><span class="com">#</span><span class="pln"> have </span><span class="typ">Travis</span><span class="pln"> install chrome stable</span><span class="pun">.</span>
  8. <span class="pln">cache</span><span class="pun">:</span>
  9. <span class="pln">  yarn</span><span class="pun">:</span><span class="kwd">true</span>
  10. <span class="pln">  directories</span><span class="pun">:</span>
  11. <span class="pln">    </span><span class="pun">-</span><span class="pln"> node_modules</span>
  12. <span class="pln">install</span><span class="pun">:</span>
  13. <span class="pln">  </span><span class="pun">-</span><span class="pln"> yarn</span>
  14. <span class="kwd">script</span><span class="pun">:</span>
  15. <span class="pln">  </span><span class="pun">-</span><span class="pln"> yarn </span><span class="kwd">test</span>

作者简介

Eric Bidelman 谷歌工程师,Lighthouse 开发,Web 和 Web 组件开发,Chrome 开发


via: https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai

作者:Eric Bidelman 译者:firmianay 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐