I'm writing behavioral tests for an Angular2 app using Protractor, and I'm new to async/await syntax. This problem has brought me to a halt. Can someone explain why I get the Typescript error Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. in the isLogoFooterPresent() method, and how to fix it?
I have the following simple Protractor spec that tries to verify that an image is displayed by checking the image's width.
Here's an excerpt from my e2e-spec.ts file:
import { LoginPage, Layout } from '../page_objects';
describe('Login page', () => {
it('Should have the logo in the footer', async () => {
await LoginPage.navigateTo();
await expect(Layout.isFooterLogoPresent).toBe(true);
})
});
And here's a snippet from the my Layout page object that has visibility to the logo image in question. The line with the await element(...).getAttribute(...) chain that causes the error.
import { browser, element, by } from 'protractor/globals';
export class Layout {
static get isFooterLogoPresent() {
return ( async () => { // <-- this wrapping is necessary to use async/await in getter method
return await element(by.css('.content-footer img')).getAttribute('naturalWidth') > 0; // <-- PROBLEM HERE
})
}
}
The following promise-based syntax (which works) is what I'm trying to replace:
static get isFooterLogoPresent() {
return element(by.css('.content-footer img')).getAttribute('naturalWidth') // naturalWidth attr is 0 if image is missing
.then( imgWidth => imgWidth > 0 );
}
BTW, I am using Typscript 2.3.2 and Protractor 5.2.0. I appreciate any wisdom you've got. Thanks!