学习 NightWatch - 测试钩子

测试钩子

例子

import { NightwatchBrowser, NightwatchTests } from 'nightwatch'

const Test: NightWatchTests = {
    before: (browser: NightWatchBrowser) => {
        console.log('开始之前...')
    },
    after: (browser: NightWatchBrowser) => {
        console.log('结束之后')
    },
    beforeEach: (browser: NightWatchBrowser) => {
        // ...
    },
    after: (browser: NightWatchBrowser) => {
        // ...
    },
    'test 1': (browser: NightWatchBrowser) => {
        // ...
    },
    'test 2': (browser: NightWatchBrowser) => {
        // ...
    }
}
export default Test

// 调用顺序
// before -> beforeEach -> 'test 1' -> afterEach -> 
// beforeEach -> 'test 2' -> afterEach -> after

异步 beforeEach afterEach

必须调用 回调函数 done() 作为该函数的最后一步,否则将会导致超时报错

import { NightwatchBrowser, NightwatchTests } from 'nightwatch'

const Test: NightWatchTests = {
    beforeEach: (browser: NightWatchBrowser, done: (err?: any) => any) => {
        setTimeout(() => done(), 1000)
    }
}
export default Test

默认情况下,调用超时设置为 10s (单元测试为 2s ),可在全局文件中定义 doneasyncHookTimeout 字段修改设置

需要故意使得测试失败可以通过调用参数进行

import { NightwatchBrowser, NightwatchTests } from 'nightwatch'

const Test: NightWatchTests = {
    beforeEach: (browser: NightWatchBrowser, done: (err?: any) => any) => {
        setTimeout(() => done(err), 1000)
    }
}
export default Test

全局测试钩子

export default {
    'Default': {
        isLocal: true
    },
    'integration': {
        isLocal: true
    },
    before(done) {
        if (this.isLocal) App.startServer(() => done())
        else done()
    },
    after(done) {
        if (this.isLocal) App.stopServer(() => done())
        else done()
    },
    beforeEach(browser, done) {
        browser.status(result => console.log(result.value))
        done()
    },
    afterEach(browser, done) {
        console.log(browser.currentTest)
        done()
    },
    async onBrowserNavigate(browser) {
        return Promise.resolve()
    },
    async onBrowserQuit(browser) {
        return Promise.resolve()
    }
}

学习 NightWatch - 测试钩子
http://localhost:8080/archives/59d92097-49c9-4e13-86d5-42735fa899c3
作者
inksha
发布于
2024年09月14日
许可协议