简体   繁体   English

Mocha测试设置可运行两个需要相同测试的测试

[英]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.

相关问题 如何设置 mocha 以在 Gatsby 存储库中运行测试 - How to setup mocha to run tests in a Gatsby repo angularjs和mocha test Uncaught TypeError:(window.beforeEach || window.setup)不是函数 - angularjs and mocha test Uncaught TypeError: (window.beforeEach || window.setup) is not a function beforeEach具有用于Mocha测试的套件语法 - beforeEach with suite syntax for mocha tests 在观察模式下进行任何摩卡测试之前,如何运行全局设置脚本 - How can I run a global setup script before any mocha test in watch mode 如何在Mocha测试中要求相同的文件 - How to require same file in Mocha test 尝试使用mocha,babel和es6模块设置测试 - Trying to setup tests with mocha, babel & es6 modules 我正在尝试使用 mocha 运行我的测试脚本,但出现“参考错误:beforeEach 未定义”的错误 - I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined" 在尝试运行Mocha测试时,require.context不是函数 - require.context not a function when trying to run Mocha tests 在验收测试之前运行一次设置 - Run setup once before acceptance tests beforeEach不能在CoffeeScript中使用Require.js在模块化QUnit / Sinon测试中运行 - beforeEach does not run in modular QUnit/Sinon tests with Require.js in CoffeeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM