Nightwatch 异步操作

在自动化测试中常常需要通过一个 command(或 function )中返回的值来进行下一步的操作,JavaScript 与 JAVA 在调用返回值时有所不同,JS 中需要特定的写法来进行这种异步操作。

以下面的 get License 数量为例,首先需要 get 一次 License 数量,然后进行一些列操作之后,再一次 get License 数量,比较这两次 License 数量值。

getLicenseNum 方法:

getLicenseNum: function (cb) {
const license = 'ul > li.license-id.ng-binding';
this.waitForElementVisible(license, 5000);
this.api.elements('css selector', license, function (result) {
cb(result.value.length);
return this;
});
}

对两次得到的 License num 进行比较:

'JavaScrpit asynchronous operation': function(client) {
const license = client.page.license();
let num1, num2;
license.getLicenseNum(function(num) {
num1 = num;
});
license.getLicenseNum(function(num) {
num2 = num;
});
client.perform(function() {
client.assert.equal(num2 - num1, 1, 'license number increase 1');
});
}

Nightwatch 模拟键盘操作

在自动化测试中有这样一个场景,在一个输入框中输入一串字符,然后执行敲回车键,验证搜索结果,以 Google 搜索为例,代码如下:

'search nightwatch and click ENTER key': function(client) {
client
.url('http://google.com')
.expect.element('body').to.be.present.before(1000);
client.setValue('input[type=text]', ['nightwatch', client.Keys.ENTER])
.pause(1000)
.assert.containsText('#main', 'Night Watch');
}

Read More

Nightwatchjs 中文参考手册

Nightwatch中文参考手册

为何放弃 JAVA 改用 Nightwatch

  • 项目初期用的是 Java + Selenium + TestNG 自动化框架,由于之前推行的力度不够,加上繁重的功能测试和频繁的项目变更导致自动化测试代码跟不上开发的进度,大量的测试代码无法正在运行。
  • 我们的产品采用的 AngularJS 开发,前端开发人员对js对Java更精通,以后的自动化脚本开发也可以一起编写。
  • Nightwatch 的环境配置和执行简单,只要 npm install、npm test 就可以运行起来,方便配置,运行和继续集成。

因此,与其维护不可用的代码不如好好整理,不如在项目领导和开发的强力支持下重新开始做一套可用的 E2E 测试。

关于 Nightwatch 翻译

学习中发现 Nightwatch 没有比较完整的中文参考手册,因此决定学习之余翻译下官方文档,如有问题,欢迎纠正。

JST automation framework

This automation framework was design by Java+Selenium+TestNG when I did automation test work, so I called it JST-automation.

Directory Structure

├──JST-automation
| ├── src
| | └── main
| | | └── java
| | | | └── com
| | | | | └── action
| | | | | | └── case01
| | | | | | └── case02
| | | | | | └── common
| | | | | | └── .....
| | | | | └── config
| | | | | | └── UserConfig
| | | | | | └── DriverConfig
| | | | | | └── UrlConfig
| | | | | | └── ......
| | | | | └── page
| | | | | | └── LoginPage
| | | | | | └── HomePage
| | | | | | └── ......
| | | | | └── verify
| | | | | | └── case01
| | | | | | └── case02
| | | | | | └── ......
| └── testng.xml
| └── pom.xml
  1. action: all test function write in this folder
  2. config: all config file put in this folder
  3. page: all page element write in this folder
  4. verify: all verify test case write in this folder
  5. testng.xml: test suit file, config all verify test case in this file
  6. pom.xml: configuration need package files

Download Code

git clone https://github.com/shenxianpeng/JST-automation.git

Any suggestion and questions please feel free to create issue here

Hello Hexo

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start(4 Steps)

hexo new "My New Post"    #Create a new post
hexo server #Run server
hexo generate #Generate static files
hexo deploy #Deploy to remote sites

Also

hexo new "My New Post"    #Create a new post
hexo server #Run server
hexo generate -deplogy #Generate then deploy
or
hexo g -d #Simple write this