首页 > 解决方案 > ngOnInit & 量角器

问题描述

我有一个在 ngOnInit 中执行一些初始化逻辑的组件。这种初始化使组件改变了它的状态,我们在模板中反映了这个状态。在这种情况下使用量角器实现一些 e2e 时,我似乎无法检查 ngOnInit 中发生的不同状态变化,因为它们没有反映在量角器中。

让我们想象一下我的 ngOnInit:

  ngOnInit() {
    this.newVariable = 0;

    setInterval(()=>{
      this.newVariable++;
    }, 500);
  }

这是我的模板:

<h2 class="debug">{{ newVariable }}</h2>

如果在我的测试用例中,我会执行以下操作:

let inspector: any;

  beforeEach(async () => {
    page = new AppPage();

    setInterval(()=>{
      if(inspector){
        browser.executeScript("return arguments[0].innerHTML;", inspector).then((value)=> { console.log(value)});
      }      
    }, 100);
  }); 

  it('should print show the value with the header', async () =>{        
    await page.navigateTo();    
    inspector = $("h2.debug");      

    await browser.wait(EC.visibilityOf(inspector), 30000, 'Title not found');        
  }) 

结果是我总是在控制台中得到一个空值。

量角器和检查 ngOnInit 中发生的更改是否有任何限制?

谢谢。

标签: angularprotractorngoninit

解决方案


推荐阅读