系列内容
- web自动化测试框架-01 搭建基础架构并运行一个样例
- web自动化测试框架-02 快速开发用例文档脚本
- web自动化测试框架-03 介绍标签,背景,场景,场景大纲
- web自动化系列教程- 04 Hooks与TimeOut介绍
- web自动化测试框架-05 创建数据驱动的测试用例,Doc String与Data Table
- web自动化测试框架-06 如何快速编写自动化脚本
备注:配合免费视频教程,获取更佳的学习效果!课程链接: ke.qq.com/course/2815…
在上一讲的内容里,我们讲解了设计场景时不同类型场景的区别。今天我们来介绍一下Cucumber代码执行部分。
主要内容
- hooks 介绍
- 自定义hooks
- 跳过运行设置
- TimeOut介绍
- 自定义TimeOut
- 禁用TimeOut
配合免费视频教程,获取更佳的学习效果!点此 课程链接
Hooks 介绍
Hooks(钩子) 用于在每个场景运行之前和之后的操作,例如,Web自动化测试中场景运行之前一般我们会打开浏览器并最大化窗口,场景运行之后做截屏操作,这些我们都可以定义在hooks中。
Cucumber中的hook提供了如下四个方法:
- BeforeAll
- Before
- After
- AfterAll
BeforeAll
在所有场景执行之前的操作,例如打开浏览器并最大化最大化窗口。每个BeforeAll函数只会执行一次。
const {BeforeAll} = require('cucumber')BeforeAll(async function(){
await driver.manage().window().maximize()
})
Before
每个场景运行之前的操作,例如执行操作之前清空浏览器cookie
const {Before} = require('cucumber')Before(async function () {
await driver.manage().deleteAllCookies()
})
After
每个场景运行之后的操作,例如执行操作后进行截屏
const {After} = require('cucumber')After(async function () {
//After Scenario Hook
//capture screenshot after each scenario
let screenshot = await driver.takeScreenshot();
this.attach(screenshot, 'image/png');
});
AfterAll
运行完所有的场景之后的操作,例如运行完毕后关闭浏览器实例
const {AfterAll} = require('cucumber')AfterAll(function () {
//perform some shared teardown
return driver.quit();
})
自定义Hooks
我们在feature文件中可以为不同的功能或场景设置标签(tag),hook函数可以设定应用在哪些tag上,例如下面的例子:
var {After, Before} = require('cucumber');
Before(function () {
// This hook will be executed before all scenarios
});
Before({tags: "@foo"}, function () {
// This hook will be executed before scenarios tagged with @foo
});
Before({tags: "@foo and @bar"}, function () {
// This hook will be executed before scenarios tagged with @foo and @bar
});
Before({tags: "@foo or @bar"}, function () {
// This hook will be executed before scenarios tagged with @foo or @bar
});
// You can use the following shorthand when only specifying tags
Before("@foo", function () {
// This hook will be executed before scenarios tagged with @foo
});
跳过运行设置
如果某些场景需要跳过运行,直接在hooks中返回 skipped 即可。用法如下:
Before(function() {
// perform some runtime check to decide whether to skip the proceeding scenario
return 'skipped'
});
TimeOut介绍
Node.js中很多操作是异步的,为了解决异步中忘了调用回调,或其它错误如不小心写了一个死循环,在Cucumber中通过设置超时时间来避免这种情况发生,默认的超时时间为5秒。通过调用 setDefaultTimeout 方法更改默认全局超时时间。
var {setDefaultTimeout} = require('cucumber');
setDefaultTimeout(60 * 1000);
自定义TimeOut
有时5秒的超时时间太短,有些浏览器操作如加载页面超过这个时间,那么可以为某些hook或步骤函数定义超时时间,如果不想做全局设定,可以在为hook定义超时中添加tag过滤。
var {Before, Given} = require('cucumber');
Before({timeout: 60 * 1000}, function() {
// Does some slow browser/filesystem/network actions
});
Given(/^a slow step$/, {timeout: 60 * 1000}, function() {
// Does some slow browser/filesystem/network actions
});
禁用超时时间
一般情况下不推荐大家使用
在某些特殊场景中可能需要禁用超时时间,就是无限等待执行完成。这可以通过设置 {timeout:-1} 实现。
var {Before, Given} = require('cucumber');
var Promise = require('bluebird');
Given('the operation completes within {n} minutes', {timeout: -1}, function(minutes) {
const milliseconds = (minutes + 1) * 60 * 1000
const message = `operation did not complete within ${minutes} minutes`
return Promise(this.verifyOperationComplete()).timeout(milliseconds, message);
});
获取更多信息,可以关注公众号,也可以加QQ群:707467292 进行node.js自动化相关技术交流。