【问题标题】:Protractor with mocha and chai量角器与摩卡和柴
【发布时间】:2014-12-10 00:40:47
【问题描述】:

我开始使用 Protractor,我尝试做的第一件事是使用 Mocha 和 Chai 而不是 Jasmine。虽然现在我不确定这是否是个好主意。

首先我需要让 Chai 可以从所有规范文件中访问,而不必每次都导入,我发现可以在 protractor.conf 文件中进行:

  onPrepare: ->
      global.chai = require 'chai'
      chai.use require 'chai-string'
      chai.use require 'chai-as-promised'
      global.expect = chai.expect

现在在这样的规范中:

  it "when clicked should sort ",->
      headerColumns.get(0).click()
      firstCellText = $$(".first-cell").getText()
      secondCellText = $$(".second-cell").getText()

      # this won't work
      expect(firstCellText).eventually.be.above(secondCellText)             

为了让它发挥作用,我可以做到:

    # now this works
    $$(".second-cell").getText().then (secondCellText)->
        expect(firstCellText).eventually.be.above(secondCellText)             

但这很丑陋,我不想一直在.then 内包装东西。我在想应该有更好的方法(?)

【问题讨论】:

    标签: jasmine mocha.js protractor chai chai-as-promised


    【解决方案1】:

    我遇到了同样的问题。我的问题是通过 Protractor config.js 为 mocha 添加更长的超时时间。

    之所以可行,是因为 Protractor 测试相对于其他模块(如 supertest)需要更长的时间,因为正在与实际浏览器进行交互。

    我加了

      mochaOpts: {
       timeout: 5000
      }
    

    到我的量角器配置并且测试通过了。

    【讨论】:

      【解决方案2】:

      您需要使用“this.timeout(1000);”为整个测试套件提及超时就在 describe 块下方,或者您可以通过为每个“it”块显式定义超时来为单个测试用例更改它。

      描述块示例

      describe("test-suite",function () {
          this.timeout(5000);
          it("test-case",function () {
             //test case code goes here 
          });
      });
      

      它块的例子

      it("test-case",function () {
              this.timeout("20000");
          });
      

      【讨论】:

        【解决方案3】:

        我在 TypeScript 中尝试做同样的事情时发现了这个问题,但 protractor.conf.js 中的方法是相同的。

        让 chai 在全球范围内可用

        看来要实现这一点,你是对的,它需要在准备中完成。下面是一个相当简洁的例子。据我了解,这是必需的,因为 chai 当然不是 mocha 的一部分,而是一些我们可以与 mocha 一起使用的额外糖果。与 jasmine 不同,所有内容都捆绑到框架中。

        protractor.conf.js 片段

        onPrepare: function() {
          var chai = require('chai'); // chai
          var chaiAsPromised = require("chai-as-promised"); // deal with promises from protractor
          chai.use(chaiAsPromised); // add promise candy to the candy of chai
          global.chai = chai; // expose chai globally
        }
        

        示例规范

        一旦您在全球范围内完成了 chai 和 chai-as-promised 设置,您需要在规范中添加一个“小”样板,以公开来自 chai 的 expect

        example.e2e-spec.ts 片段

        const expect = global['chai'].expect; // obviously TypeScript
        
        
        it('will display its title', () => {
          pageObject.navigateTo();
        
          const title = element(by.css('app-root h1')).getText();
          expect(title).to.eventually.contain('An awesome title');
        });
        

        避免那么

        我不知道您的 $$ 引用是什么,但如果您使用量角器组件 browserby 等,那么事情会稍微清理一下。

        调用const title = element(by.css('app-root h1')).getText(); 似乎返回了一个承诺,jasmine 似乎开箱即用,而 mocha+chai 却没有。这就是 chai-as-promised 的用武之地。

        使用我们的额外语法 candy expect(title).to.eventually.contain('An awesome title'); 非常巧妙地解决了承诺,我们避免了所有那些 then 调用,但我们确实需要 eventually

        我已经为你提供了一个可以工作的 TypeScript example 的链接,它可能有助于演示。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-12
          • 2018-05-24
          • 2015-05-01
          • 1970-01-01
          • 2016-05-07
          • 1970-01-01
          • 2020-07-27
          • 2019-11-15
          相关资源
          最近更新 更多