Saturday 2 January 2021

How to fix Protractor script timeout issue

 In this article, I will show how to assert asynchronous APIs using Jasmine callbacks in Protractor test.

 Consider the following protractor test, I want to assert if an element is present on page using protractor API.

I am going to use  "element(locator).isPresent" API which is asynchronous and returns "webdriver.promise.Promise<boolean>".

The test code before the fix:

I have written following test where I am asserting that the isPresent API should return true value.



  it('should have a button element present', function() {
    
	browser.get('http://juliemr.github.io/protractor-demo/');

        var gobtn = element(by.id('gobutton'));
	
	gobtn.isPresent().then( (result) =>
            expect(result).toBe(true);	    
	});    
  }); 

After  running the protractor tests with the above code, I am gettng timeout errors while running Protractor tests.

Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at listOnTimeout (internal/timers.js:554:17)
        at processTimers (internal/timers.js:497:7)


Solution:

 We can use Jasmine Callbacks to assert asynchronous behaviour. For more details go through the documentation https://jasmine.github.io/tutorials/async#callbacks

Following is the updated test code. Notice that I have added 'done' as additonal parameter for my testcase. Once the assertion is complete then I have explicitly called the done() API. This will complete the current test,

If you miss to write "done()" after your assertion then your test will behave like hung and will give you time out error.

 


  it('should have a button element present', function(done) {
    
	browser.get('http://juliemr.github.io/protractor-demo/');

        var gobtn = element(by.id('gobutton'));
	
	gobtn.isPresent().then( (result) =>
            expect(result).toBe(true);	
	    done();
	});    
  });

 

Conclusion: 

I have shown how to perform assertion for asynchronous APIs using Jasmine call back parameter.