1

I have a component which perform some intialization logic within ngOnInit. This initialization makes the component change its state and we reflect this state in the template. When implementing some e2e with protractor for this situation, it seems that I cannot check the different state changes that happen within ngOnInit, as they are not reflected in protractor.

Let's imagine this the my ngOnInit:

  ngOnInit() {
    this.newVariable = 0;

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

And this is my template:

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

If whithin my test case I do something like:

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');        
  }) 

The result is that I always get an empty value in the console.

Is there any limitation about protractor and checking changes that happens within ngOnInit?

Thanks.

David
  • 41
  • 3
  • 2
    Protractor is purely used for UI functional e2e testing. In your case, you should perform unit testing with `karma` js for validating the angular code. – Sudharsan Selvaraj Jun 20 '18 at 09:19
  • Thanks for your reply. This is UI testing, it is information shown to the end user reflecting the state of the component (performing request to the backend, downloading content, and so on) – David Jun 20 '18 at 11:13
  • 1
    Protractor by default will wait for all internal angular operations to get complete. So your test won't execute until `ngOnInit ` method is executed. To make protractor not to wait for angular operations, you can set `browser.ignoreSynchronization = true`; refer https://stackoverflow.com/questions/28808463/what-is-browser-ignoresynchronization-in-protractor. – Sudharsan Selvaraj Jun 20 '18 at 12:24

0 Answers0