自动化测试testcafe

因为项目迭代太多,而且用户使用的版本不唯一,测试又经常有事,弄得我自己测试还容易出现问题,对此头儿提出我这边弄个自动化测试。

我个人认为这个玩意如果是上手成本,维护成本低还行,高的话明显是要影响我的开发的。。

testcafe github星标5k+

用它官方的话来说他是什么东西

使用nodejs编写的web端ui自动测试框架

说到自动测试化框架现在多的都是Selenium

我用这个主要还是图个方便不用像Selenium 需要配置依赖

这个安装很简单 依赖于nodejs 安装nodejs 这里弄hexo肯定是有这个的不多说了 直接安装testcafe

npm install -g testcafe

注意别开着翻墙 实测出500错误

他官方文档说的很明白了如何搭建一个测试用例https://devexpress.github.io/testcafe/documentation/getting-started/

要创建测试,请创建一个新的.js或.ts文件

整个js 上个模拟登录操作的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Selector } from 'testcafe';
import { RequestLogger } from 'testcafe';
/*创建一个记录仪*/
const logger = RequestLogger('http://localhost/login');

fixture `Getting Started`
.disablePageCaching `My fixture`
.page `http://localhost/login`;

test
.requestHooks(logger)
('test', async t => {

// Ensure that the response has been received and that its status code is 200.
await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();

const logRecord = logger.requests[0];

console.log(logRecord.userAgent); // Chrome 63.0.3239 / Windows 8.1.0.0
console.log(logRecord.request.url); // http://api.example.com
console.log(logRecord.request.method); // get
console.log(logRecord.response.statusCode); // 304
});

test('My first test', async t => {
await t
/*先清空再输入*/
.selectText('.uname')
.pressKey('delete')
.selectText('.pword')
.pressKey('delete')
.typeText('.uname','admin')
.typeText('.pword','admin123')
.click('#btnSubmit');


}).after(async t => {

console.log("after")
});

testcafe chrome testcafe.js -s takeOnFails=true

启动测试用例

-s takeOnFails=true意思是检测到错误截图

官方有文档看就完事了。

这个缺点就是国内学习资料比较少吧。看官网的文档久了没啥问题

然后这个自动化测试碰到验证码问题很头疼,暂且不深入我的测试项目直接把验证码关闭了

看一下效果