无法在Nightwatch的Chai.js中使用期望

时间:2019-03-29 06:44:18

标签: javascript chai nightwatch.js

我使用Nightwatch构建简单的e2e测试。此简单的e2e测试已成功通过。
接下来,我构建第二个测试以使用Chai.js / Mocha.js。但是此测试引发了异常。

// First(successfully passed)
module.exports = {
  'increment counter': browser => {
    browser
      .url(process.env.VUE_DEV_SERVER_URL)
      .click("button#plus-btn")
      .assert.containsText('h1', '1')
      .end()
  },
  "decrement counter": browser => {
    browser
      .url(process.env.VUE_DEV_SERVER_URL)
      .click("button#minus-btn")
      .assert.containsText('h1', '-1')
      .end()
  }
}
// Second
describe('counter testing', () => {

  describe('press plus button', () => {

    it('increments counter', (browser) => {
      browser
        .url(process.env.VUE_DEV_SERVER_URL)
        .click("button#plus-btn")
        .expect.element('h1').text.to.equal('1')
        .end()
    })
  })

  describe('press minus button', () => {

    it('decrements counter', (browser) => {
      browser
        .url(process.env.VUE_DEV_SERVER_URL)
        .click("button#minus-btn")
        .expect.element('h1').text.to.equal('-1')
        .end()
    })
  })
})

异常消息

TypeError: browser.url(...).click(...).expect.element(...).text.to.equal(...).end is not a function

下一步我该怎么做?

1 个答案:

答案 0 :(得分:1)

根据docs,使用import { Component, OnInit, Inject } from '@angular/core'; import { Router } from '@angular/router'; import { DashboardService } from './dashboard.service'; import { Observable, of, timer } from 'rxjs'; import 'rxjs/add/operator/takeWhile'; import 'rxjs/add/observable/timer'; @Component({ templateUrl: 'dashboard.component.html', styleUrls: ['./dashboard.component.css'] }) export class DashboardComponent implements OnInit { alive = true; countOnProgress:number = 0; max: number = 200; value: number = 100; stacked: any[] = []; constructor(@Inject (DashboardService) private dashboardService: DashboardService){ } ngOnInit(): void { Observable.timer(0, 30000) .takeWhile(() => this.alive) .subscribe(() => { this.dashboardService.getCountProgress().subscribe(resp => { this.countOnProgress = resp.d; console.log(this.countOnProgress); //It found the data }) }); this.progressLastYear(); } progressLastYear(): void{ const types = ['success', 'info', 'warning', 'danger']; const values = [this.countOnProgress]; console.log(values); this.stacked = []; for (let i = 0; i < 5; i++) { this.stacked.push({ value: values[1], type: types[i], label: values[1] }); console.log(this.stacked); //The datas: 0, succes, 0 (didnt get countOnProgress' value) } } } 进行断言时,应以不同的方式调用end。我认为这是因为expect的方式是基于expect的。更改测试以使其看起来像这样

chai