Nightwatch 简介
Nightwatch 是一个自动化测试框架,为基于 Web 的应用程序提供端到端测试解决方案。它在后台使用 W3C WebDriver API(以前称为 Selenium WebDriver)打开 Web 浏览器,对 DOM 元素执行操作和断言。如果你想使用浏览器测试你的 Nuxt 应用程序,这是一个很棒的工具。但是在 Nuxt 应用程序中使用它之前,让我们在以下步骤中单独使用它编写一些简单的测试,以便你对它的工作方式有一个基本的了解:
-
通过
npm安装Nightwatch,并将其保存到package.json文件中的devDependencies选项:$ npm i nightwatch --save-dev -
通过
npm安装GeckoDriver,并将其保存到package.json文件中的devDependencies选项:$ npm install geckodriver --save-devNightwatch依赖于WebDriver,因此我们需要根据你的目标浏览器安装特定的WebDriver服务器——例如,如果你只想针对Firefox编写测试,那么你需要安装GeckoDriver。在本书中,我们专注于针对单个浏览器编写测试。但是,如果你想并行针对多个浏览器(如
Chrome、Edge、Safari和Firefox),那么你需要安装Selenium Standalone Server(也称为Selenium Grid),如下所示:$ npm i selenium-server --save-dev请注意,本书中我们将测试
Firefox和Chrome,因此不会使用此selenium-server包。 -
将
nightwatch添加到package.json文件中的test脚本:// package.json { "scripts": { "test": "nightwatch" } } -
创建一个
nightwatch.json文件来配置Nightwatch,如下所示:// nightwatch.json { "src_folders" : ["tests"], "webdriver" : { "start_process": true, "server_path": "node_modules/.bin/geckodriver", "port": 4444 }, "test_settings" : { "default" : { "desiredCapabilities": { "browserName": "firefox" } } }, "launch_url": "https://github.com/lautiamkok" }在这个简单的练习中,我们想要测试
github.com上特定贡献者Lau Tiam Kok的存储库搜索功能,因此我们在配置的launch_url选项中设置了 https://github.com/lautiamkok。我们将在
/tests/目录中编写测试,因此我们在src_folders选项中指示了目录位置。我们将在端口4444(服务器端口)上仅针对Firefox进行测试,因此我们在webdriver和test_settings选项中设置了此信息。你可以在 https://nightwatchjs.org/gettingstarted/configuration/ 上找到其余测试设置(例如
output_folder)的选项。如果你想了解Selenium Server的测试设置,请访问 https://nightwatchjs.org/gettingstarted/configuration/selenium-server-settings。 -
在项目根目录下创建一个
nightwatch.conf.js文件,用于将驱动程序路径动态设置为服务器路径:// nightwatch.conf.js const geckodriver = require("geckodriver") module.exports = (function (settings) { settings.test_workers = false settings.webdriver.server_path = geckodriver.path return settings })(require("./nightwatch.json")) -
在
/tests/目录中创建一个.js文件(例如,demo.js),并在其中准备以下Nightwatch测试模板:// tests/demo.js module.exports = { 'Demo test' : function (browser) { browser .url(browser.launchUrl) // 在这里编写你的测试... .end() } } -
在
/tests/目录中创建一个github.js文件,其代码如下:// tests/github.js module.exports = { 'Demo test GitHub' : function (browser) { browser .url(browser.launchUrl) .waitForElementVisible('body', 1000) .assert.title('lautiamkok (LAU TIAM KOK) · GitHub') .assert.visible('input[type=text][placeholder=Search]') .setValue('input[type=text][placeholder=Search]', 'nuxt') .waitForElementVisible('li[id=jump-to-suggestion-search-scoped]', 1000) .click('li[id=jump-to-suggestion-search-scoped]') .pause(1000) .assert.visible('ul[class=repo-list]') .assert.containsText('em:first-child', 'nuxt') .end() } }在这个测试中,我们想要断言存储库搜索功能按预期工作,因此我们需要确保某些元素和文本内容存在且可见,例如
<body>和<input>元素,以及文本nuxt和lautiamkok (LAU TIAM KOK) · GitHub。当你使用npm run test运行它时,如果测试通过,你应该会得到以下结果:[Github] Test Suite =================== Running: Demo test GitHub ✓ Element <body> was visible after 34 milliseconds. ✓ Testing if the page title equals "lautiamkok (LAU TIAM KOK) · GitHub" - 4 ms. ✓ Testing if element <input[type=text][placeholder=Search]> is visible - 18 ms. ✓ Element <li[id=jump-to-suggestion-search-scoped]> was visible after 533 milliseconds. ✓ Testing if element <ul[class=repo-list]> is visible - 25 ms. ✓ Testing if element <em:first-child> contains text: "nuxt" - 28 ms. OK. 6 assertions passed. (5.809s)你可以在我们的
GitHub仓库中的/chapter-13/nightwatch/找到上述测试。有关Nightwatch的更多信息,请访问 https://nightwatchjs.org/。
与 AVA 相比,Nightwatch 并不那么简洁,因为它需要一些可能冗长而复杂的配置,但是如果你遵循最简单的 nightwatch.json 文件,你应该可以很快开始使用 Nightwatch。因此,让我们在下一节中将你刚才在本节中学到的知识应用到 Nuxt 应用程序中。