I am new to scripting languages and encountered an issue while using enums with if-else statements in TypeScript. To work around this problem, I have decided to use switch-case instead of if-else conditions.
Despite trying !== and !===, they do not seem to be effective. The first index of the enum does not work unless I specify the if condition as not equal to the compared value.
public static async getElementByLocatorType(locatorType: customLocators, locatorValue: string, expectedText?: string) {
switch(locatorType) {
case customLocators.CSS:
return element(by.css(locatorValue);
case customLocators.XPATH:
return element(await by.xpath(locatorValue));
case customLocators.CSSTEXT:
return element(by.cssContainingText(locatorValue, expectedText));
case customLocators.LINKTEXT:
return element(by.linkText(locatorValue));
case customLocators.BUTTONTEXT:
return element(by.buttonText(locatorValue));
case customLocators.PARTIALBUTTONTEXT:
return element(by.partialButtonText(locatorValue));
case customLocators.PARTIALLINKTEXT:
return element(by.partialLinkText(locatorValue));
default:
logger.info('Cannot find any locator listed above');
}
}
export enum customLocators {
'CSS', 'XPATH', 'CSSTEXT', 'LINKTEXT', 'BUTTONTEXT', 'PARTIALBUTTONTEXT', 'PARTIALLINKTEXT'
}
I am facing an issue where no element is found when using (by.css), while other elements work perfectly fine. This occurs specifically with the locatorType === customLocators.CSS, which is the first index of the enum.