javascript代码实例教程-NightWatchJS

发布时间:2019-01-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-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 。浏览器初始化流程见下图。

javascript代码实例教程-NightWatchJS

大部分情况下, 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 (optional) string none 外部模块路径,为测试一共全局变量. 也可在test_settings中重写
selenium (optional) object Selenium Server相关的设置
test_settings (optional) object 与测试相关的测试,下面有详细描述
live_output (optional) boolean false 是否缓存并持续输出结果
disable_colors (optional) boolean false 控制命令行输出是否带有高亮颜色
parallel_process_delay (optional) integer 10 在并行模式下启动子进程的时间,单位毫秒
test_workers boolean|object false 是否为运行单个文件测试启动并行模式,如果设置为true,会进入并行模式,并自动设置线程数。如果设置为对象,要指定enable和workers参数,workers接受数值和’auto’。 例如:”test_workers” : {“enabled” : true, “workers” : “auto”}
test_runner (optional) string|object “default” 用什么工具运行测试。值可以是”default”或”mocha”

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! @L_304_1@,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-NightWatchJS全部内容,希望文章能够帮你解决javascript代码实例教程-NightWatchJS所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。