[英]Mocha test setup to run two tests who require same beforeEach setup
I'm testing an Electron app using Spectron, using mocha to run the test suite. 我正在使用Spectron通过Mocha运行测试套件来测试Electron应用程序。 I'm trying to accomplish running two tests that require the same setup, so I'm using beforeEach to achieve the repeated setup code.
我正在尝试完成两个需要相同设置的测试,因此我使用beforeEach来实现重复的设置代码。 The former tests runs successfully through as expected, but the second test bails out two early with error message:
前一个测试按预期成功运行,但是第二个测试提早两次失败,并显示错误消息:
Error: An element could not be located on the page using the given search parameters.
I have achieved desired result if I add sleep function, but I'd rather not do that, any idea what could be the problem here? 如果添加睡眠功能,我已经达到了预期的效果,但是我宁愿不这样做,不知道这可能是什么问题?
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()
})
})
})
You dont need to explicitly call the beforeEach hook in your first test. 您不需要在第一个测试中显式调用beforeEach挂钩。
Your beforeEach hook will run before each describe 您的beforeEach挂钩将在每个描述之前运行
Test runner will run the hooks by itself. 测试运行器将自己运行该钩子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.