Reputation: 4062
I have an e2e protractor test in my angular application. I've just run the exact same test several times in a row and it passed about 5% of the time, failing with the error in the screenshot below.
The Test:
it('should open the cart with the cart button in the header', () => {
page.navigateTo('/calendar/day/(dmy:27-9-2018)');
page.cartButton().click();
expect(element(by.css('h2.cart-header')).isPresent()).toBe(true);
});
The chrome instance launched by protractor pauses shows that the button was clicked and the h2 element is present (see image at bottom).
What I have tried
... header', async () => { ...
expect(await element(by.css('h2.cart...
browser.sleep(1000)
.toBe(true)
, .toEqual(true)
, and .toBeTruthy()
What is causing this error, and how can I resolve it?
The element is present in the browser launched by protractor
Upvotes: 6
Views: 1588
Reputation: 233
You can use Jasmine Callbacks to assert asynchronous behavior. Jasmine test provides additional parameter as callback argument. Once you are done with the assertions you can invoke the callback API.
Example:
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();
});
});
Upvotes: 0
Reputation: 123
isPresent() returns promise that you need to resolve. Yes, use async() function + await to resolve it easily More of that, use ExpectedConditions module like that:
let cart = await element(by.css('h2.cart-header'))
await browser.wait(ExpectedConditions.visibilityOf(cart), 5000, "Cart is not visible even in 5 seconds!")
Upvotes: -1