[英]Mocha test setup to run two tests who require same beforeEach setup
我正在使用Spectron通過Mocha運行測試套件來測試Electron應用程序。 我正在嘗試完成兩個需要相同設置的測試,因此我使用beforeEach來實現重復的設置代碼。 前一個測試按預期成功運行,但是第二個測試提早兩次失敗,並顯示錯誤消息:
Error: An element could not be located on the page using the given search parameters.
如果添加睡眠功能,我已經達到了預期的效果,但是我寧願不這樣做,不知道這可能是什么問題?
const helpers = require('./global-setup')
var fs = require('fs')
var assert = require('assert')
const path = require('path');
var expect = require('chai').expect
//const chai = require('chai');
var describe = global.describe
var it = global.it
var beforeEach = global.beforeEach
var afterEach = global.afterEach
describe('Launch application', function () {
helpers.setupTimeout(this)
var app = null
function snapshotOnError(picName) {
console.log('taking error snapshot pic')
app.browserWindow.capturePage().then(function (imageBuffer) {
fs.writeFile(picName + '.png', imageBuffer)
})
}
beforeEach(function () {
return helpers.startApplication({
args: [path.join(__dirname, '..')]
}).then(function (startedApp) { app = startedApp });
})
afterEach(function () {
return helpers.stopApplication(app)
})
describe('log into training mode', function() {
beforeEach(function(done) {
app.client.waitUntilWindowLoaded()
.browserWindow.focus()
.getWindowCount().should.eventually.equal(1)
.browserWindow.isMinimized().should.eventually.be.false
.browserWindow.isDevToolsOpened().should.eventually.be.false
.browserWindow.isVisible().should.eventually.be.true
.browserWindow.isFocused().should.eventually.be.true
.click('//*[@id="js-side-nav"]/div[1]/ul/li[1]')
.electron.clipboard.writeText('training')
.click('#username')
.webContents.paste()
.waitForValue('#username', 1000)
.electron.clipboard.writeText('12345')
.click('#password')
.webContents.paste()
.waitForValue('#password', 1000)
.click('//*[@id="main"]/section/div[2]/form/div[4]/button')
.catch(function (err) {
console.log('errorJ: ' + err)
snapshotOnError('log_in_traning');
//done(err)
})
.then(function() { done(); })
})
it('checks the text in left topbar menu', function() {
return app.client
.getText('#js-top-nav > div.nav-left > ul > li:nth-child(1) > span').should.eventually.equal('Actions')
.getText('#js-top-nav > div.nav-left > ul > li.nav-news > span').should.eventually.equal('News')
.getText('#js-top-nav > div.nav-left > ul > li:nth-child(3) > span').should.eventually.equal('Reports')
.getText('#js-top-nav > div.nav-left > ul > li:nth-child(4) > span').should.eventually.equal('Settings')
.getText('#js-activate-help-menu > span').should.eventually.equal('Help')
done()
})
it('checks if icons in the top bar menu are present', function(){
return app.client
.isExisting('#js-top-nav > div.nav-left > ul > li:nth-child(1) > i').should.eventually.be.true
.isExisting('#js-top-nav > div.nav-left > ul > li.nav-news > i').should.eventually.be.true
.isExisting('#js-top-nav > div.nav-left > ul > li:nth-child(3) > i').should.eventually.be.true
.isExisting('#js-top-nav > div.nav-left > ul > li:nth-child(4) > i').should.eventually.be.true
.isExisting('#js-activate-help-menu').should.eventually.be.true
done()
})
})
})
您不需要在第一個測試中顯式調用beforeEach掛鈎。
您的beforeEach掛鈎將在每個描述之前運行
測試運行器將自己運行該鈎子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.