After working extensively with protractor, I've encountered an issue where a thrown error for timeout is displayed if an element is not found within 60 seconds. This default error message doesn't provide much insight into the actual problem. I'm seeking advice on how to customize error messages for scenarios when a specific element is not found.
Here's a snippet of my code:
Test case class:
const userData = require("../globalContent.json");
const Page = require("../objects/ikeaProductPage.obj");
describe("Product page", function () {
ikeaPage = new Page();
for (let [link, amount] of Object.entries(userData[browser.baseUrl])) {
// The Ikea page is accessible by the specified URL
it(`Is defined by the URL: ${link}`,
async function() {
await Page.navigateDesktop(`${link}`);
});
// page has a quantity label and it can be filled out with user data
it("Has a label quantity that can receive user data",
async function() {
await Page.fillFormWithUserData(`${amount}`);
});
// Details page allows the user to add to cart
it("Enables resolution of added to cart",
async function() {
await Page.hasAddToShoppingCart();
});
// Details page allows the user to proceed to the next stage when page has been resolved
it("Allows the user to proceed to the next stage of add to cart",
async function() {
await Page.hasAddedToBag();
await browser.sleep(1000);
});
}
});
Object class:
const utils = require("../utils/utils");
const Specs = require("../specs/ProductPage.specs");
module.exports = class Page {
constructor() {
const _fields = {
amountInput: Specs.productAmount
};
const _formButtons = {
addToCart: ikeaSpecs.addToCart
};
const _productFrame = {
cartIcon: ikeaSpecs.cartIcon,
addedToCartIcon: Specs.addedToCart,
};
this.getFields = function() {
return _fields;
};
this.getFormButtons = function() {
return _formButtons;
};
this.getFrame = function() {
return _productFrame;
};
}
getForm() {
return {
fields: this.getFields(),
buttons: this.getFormButtons(),
};
}
getPageFrame() {
return {
buttons: {
iconFrames: this.getFrame()
}
};
}
//Navigate for Desktop
async navigateDesktop(URL) {
await browser.waitForAngularEnabled(false);
await browser.manage().window().maximize();
await browser.get(URL);
}
//Fill qty from globalContent.json
async fillFormWithUserData(amountQuantity) {
const formFields = this.getForm().fields.amountInput;
await formFields.clear();
await utils.sendKeys(formFields, amountQuantity);
}
//Check if we can add to shopping cart
async hasAddToShoppingCart() {
const formButton = this.getForm().buttons.addToCart;
await utils.elementToBeClickable(formButton);
await utils.click(formButton);
}
//Check if the product has been added
async hasAddedToBag() {
const frameCartIcon = this.getPageFrame().buttons.iconFrames.cartIcon;
const frameAddedToCart = this.getPageFrame().buttons.iconFrames.addedToCartIcon;
await utils.presenceOf(frameCartIcon);
await utils.elementToBeClickable(frameAddedToCart);
}
};
utils:
const utils = function () {
var EC = protractor.ExpectedConditions;
this.presenceOf = function (params) {
return browser.wait(EC.presenceOf(params));
};
this.elementToBeClickable = function (params) {
return browser.wait(EC.elementToBeClickable(params));
};
this.sendKeys = function (params, userData) {
return params.sendKeys(userData);
};
this.click = function (params) {
return browser.executeScript("arguments[0].click();", params.getWebElement());
};
this.switch = function (params) {
return browser.switchTo().frame(params.getWebElement());
};
this.switchDefault = function () {
return browser.switchTo().defaultContent();
};
};
module.exports = new utils();
I'm interested in learning how I can handle errors more effectively instead of just relying on timeouts?