我正在使用Spectron测试Electron应用程序,使用mocha运行测试套件。 我正在尝试完成运行两个需要相同设置的测试,所以我使用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()
})
})
})
答案 0 :(得分:0)
您不需要在第一个测试中显式调用beforeEach挂钩。
您的beforeEach挂钩将在每个描述之前运行
测试运行程序将自行运行该钩子。