使用Protractor进行Angular 2的端到端测试

3

我正在进行 Angular 2 的端到端(e2e)测试。我需要有条件地添加 it 块。必须检查计数值,如果其值大于 0,则应运行一个测试,否则应运行另一个测试。以下是代码:

describe('Check insiders:', function () {
  beforeEach(function (done) {
    element(by.css('span.insider-count')).getText().then(function(total){
      count = total;
      console.log(count);
      //browser.pause();
      done();
    });
  });
  if (count > 0) {
    it('insiders found', () => {
      expect(count).toBeGreaterThan(0);
    });
  } else {
    it('No insiders found', () => {
      // do nothing
      //expect(count).toBe(0);
    });
  }
});

即使计数变量的值大于零,它总是运行else部分。非常感谢您的帮助。

问题:CSS选择器 'span.insider-count' 是否总是存在?如果不存在,您可能需要在 then 函数之后添加一个 catch。计数是在哪里/如何定义的? - cnishina
1个回答

4
这是因为在启动测试时,itdescribe和所有jasmine描述符都会被处理,这意味着您的条件将在测试获取值之前进行测试,这就是为什么始终处理相同的值,因此得出相同的结果。
您可以通过将条件移动到公共的it块中来避免这种情况:
    it('insiders test', () => {
      if(count > 0){
          expect(count).toBeGreaterThan(0);
      } else {
          expect(true).toBe(true);
      }
    });

话虽如此,我不理解你的逻辑,因为你基本上是在做“如果计数大于0,则断言它大于0,否则不要”,这是一个不能失败的测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接