NightWatchJS

关于Nightwatch?

Nightwatch.js是一个测试web app和web 站点的自动化测试框架, 使用Node.js编写, 基于Selenium WebDriver API.

它是一个完整的浏览器端真实用户场景测试解决方案, 致力于简化继续集成和编写自动化测试。

Nightwatch got its name from the famous painting The Night Watch by Dutch painter Rembrandt van Rijn. The masterpiece is prominently displayed in the Rijksmuseum, in Amsterdam - The Netherlands.

Overview of Selenium

Selenium 是一个直接运行在浏览器中的非常流行的综合测试工具集, 最初为java 语言编写, 现在已经支持许多语言。

Selenium的主要项目:

* Selenium IDE

* Selenium Remote Control

* Selenium WebDriver

* Selenium Grid

Nightwatch uses the Selenium WebDriver, specifically the WebDriver Wire Protocol to perform the browser automation related tasks.

Nightwatch 使用Selenium WebDriver, 特别是WebDriver Wire Protocol来执行浏览器自动化测试相关任务。

Theory of Operation

Nightwatch works by sending HTTP requests to the Selenium server with the right parameters and interpreting the response. The restful API protocol is defined by the Selenium JsonWireProtocol. See below for an example workflow for browser initialization.

Nightwatch 发送通过 HTTP 请求发送响应的参数到 Selenium 服务器,并解析服务器响应。restful API 文档见Selenium JsonWireProtocol 。浏览器初始化流程见下图。

大部分情况下, Nightwatch 执行一条命令或断言,至少要发送2个请求到 Selenium 服务器。 第一个用来定位元素,可以用 CSS 选择器或者Xpath 表达式。 第二个用来在选中的元素上执行命令或断言操作。

Installation

安装Node.js

From nodejs.org:

“Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”

主流操作系统的安装包都可以在nodejs.org上找到。

Install Nightwatch

使用 npm 安装最新的 Nightwatch, 在命令行运行:

npm install -g nightwatch

运行 Selenium 服务器

Selenium WebDriver server 是一个简单的 Java servlet 程序,分别运行在本地机器上和你要进行测试的浏览器上。(todo 真的是浏览器上?)

Download Selenium

Download the latest version of the selenium-server-standalone-{VERSION}.jar file from the Selenium downloads page and place it on the computer with the browser you want to test. In most cases this will be on your local machine and typically inside your project’s source folder.

Selenium downloads page下载最新版的selenium-server-standalone-{VERSION}.jar 包, (todo)具体放在哪里, 等我试一试再说。

A good practice is to create a separate subfolder (e.g. bin) and place it there as you might have to download other driver binaries if you want to test multiple browsers.

Running Selenium Automatically

如果服务器和Nightwatch 运行在同一台机器上, Nightwatch Test Runner可以启动和停止Selenium 服务器。

Running Selenium Manually

To run the selenium server manually, from the directory with the jar run the following:

要手动启动 server 服务器, 在 jar 包所在目录执行:

java -jar selenium-server-standalone-{VERSION}.jar

更多关于运行 Selenium 服务器的信息参考:

http://code.google.com/p/selenium/wiki/RemoteWebDriverServer

要获取帮助信息, 执行:

java -jar selenium-server-standalone-{VERSION}.jar -help

Configuration

测试需要一个配置文件, 默认会使用当前目录下的 nightwatch.json 文件。nightwatch.json 详细配置如下:

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "",
  "globals_path" : "",

  "selenium" : {
    "start_process" : false,
    "server_path" : "",
    "log_path" : "",
    "host" : "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "",
      "webdriver.ie.driver" : ""
    }
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },

    "chrome" : {
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }
  }
}

Basic settings

Name type default description
src_folders string|array none 测试代码目录(不包含子目录)
output_folder (optional) string tests_output 生成的测试报告存放目录
custom_commands_path (optional) string|array none Location(s) where custom commands will be loaded from.
custom_assertions_path (optional) string|array none 自定义断言路径
page_objects_path (Optional since v6.0.1) string|array none Location(s) where page object files will be loaded from.
globals_path string none Location of an external globals module which will be loaded and made available to the test as a property globals on the main client instance.Globals can also be defined/overwritten inside a test_settings environment.
时间: 2024-08-24 03:07:52

NightWatchJS的相关文章

nightwatchjs 基于nodejs&& webdriver 协议的自动化测试&&持续集成框架

nightwatchjs 是基于nodejs&& webdriver 协议的自动化测试&&持续集成框架 参考架构 参考资料 http://nightwatchjs.org/gettingstarted/#browser-drivers-setup 原文地址:https://www.cnblogs.com/rongfengliang/p/10541106.html

使用Nightwatch.js做基于浏览器的web应用自动测试

1        安装 1.1   安装Node.js 在http://nodejs.org/ 上下载适合本机系统的安装包运行安装,注意安装选项中选择npm tool以用于后续依赖包的安装. 1.2   通过npm工具安装Nightwatch 命令行运行"npm install nightwatch",如下的提示表明安装成功. 1.3   Npm相关目录结构 所有npm安装的模块都会基于当前cmd窗口的目录,也就是说如果cmd的工作目录是在c:\根目录,则会在该目录下创建node_mo

Web自动化测试工具调研

背景 Web自动化测试越来越被重视, 因为现在Web已经是工程化的状态. 如何通过工具测试, 保证Web开发的质量,提升开发效率,是Web工具的诞生的来由. Web测试分为以下几个方面: 1. 界面测试 测试界面是否正常,这是前端测试最基础的环节. 2. 功能测试 测试功能操作是否正常,由于涉及交互,这部分测试比界面测试会更复杂 3. 性能测试 页面性能越来越受到关注,并且性能需要在开发过程中持续关注,否则很容易随着业务迭代而下降. 4. 安全性测试 测试Web界面和WebServer的安全性,

前端测试

最受欢迎的 5 款 Node.js 端到端测试框架 测试,尤其是自动化测试在现代 WEB 工程中有着非常重要的角色,与交付过程集成良好的自动化测试流程可以在新版发布时帮你快速回归产品功能,也可以充当产品文档测试因粒度不同又可以分为单元测试.接口测试.功能测试. 在 WEB 领域,功能测试亦称为端到端测试(End to End Test,简称 E2E 测试). E2E 测试的常用操作如下: 打开网页,跳转网页:打开 Github 的首页: 填写输入框,提交表单:键入搜索词,提交搜索表单: 元素单击