When handling such cases, it is important to work with expectations (assertions). Just because a click or other action is not successful does not necessarily mean the test should fail. It is crucial to define when the test should fail.
For instance:
it('should perform a smart action', async (done) => {
await element(by.xpath('dsadadada')).click()
.then(() => {
logObj.info("clicked");
})
.catch(rejected => {
logObj.info("the click was unsuccessful due to rejected promise");
done.fail(rejected); // indicate test failure
});
done();
});
Alternatively, you can use exception handling with 'try catch':
it('should perform a smart action', async (done) => {
try {
await element(by.xpath('dsadadada')).click(); // will throw an error if not clickable
await element(by.xpath('dsadadada')).sendKeys('dasdada'); // will throw an error if unable to send keys
} catch (e) {
done.fail(e); // test will fail if any action throws an error
}
done(); // test will be marked as success if no errors occur
});
or use expectations:
it('should perform a smart action', async (done) => {
await element(by.xpath('dsadadada')).click(); // will throw an error if not clickable
expect(element(by.xpath('some new element on the page after click')).isPresent()).toBeTruthy();
done();
});
Example of encapsulated Page Object Model (POM):
public class PageObjectClass {
// method for clicking x button
public async clickOnXBtn() {
await await element(by.xpath('dsadadada')).click();
}
}
it('perform a dummy action', async (done) => {
try {
// pageobject is a class that contains the actions for this button.
const pageObject = new PageObjectClass();
await pageObject.clickOnXBtn();
} catch (e) {
done.fail(e);
}
});
More information on POM can be found here:
https://github.com/angular/protractor/blob/master/docs/page-objects.md