Javascript 如何与不';t使用typescript进行匹配?
不熟悉脚本语言,在typescript中使用带有if-else语句的枚举时遇到问题。作为替代,我使用的是开关盒而不是if-else条件 我试过了这似乎不起作用。如果条件不等于比较值,则枚举的第一个索引不起作用,除非我创建它Javascript 如何与不';t使用typescript进行匹配?,javascript,typescript,protractor,Javascript,Typescript,Protractor,不熟悉脚本语言,在typescript中使用带有if-else语句的枚举时遇到问题。作为替代,我使用的是开关盒而不是if-else条件 我试过了这似乎不起作用。如果条件不等于比较值,则枚举的第一个索引不起作用,除非我创建它 public static async getElementByLocatorType(locatorType: customLocators, locatorValue: string, expectedText?: string) { if (locato
public static async getElementByLocatorType(locatorType: customLocators, locatorValue: string, expectedText?: string) {
if (locatorType! === customLocators.CSS) {
return element(by.css(locatorValue));
} else if (locatorType === customLocators.XPATH) {
return element(await by.xpath(locatorValue));
} else if (locatorType === customLocators.CSSTEXT) {
return element(by.cssContainingText(locatorValue, expectedText));
} else if (locatorType === customLocators.LINKTEXT) {
return element(by.linkText(locatorValue));
} else if (locatorType === customLocators.BUTTONTEXT ) {
return element(by.buttonText(locatorValue));
} else if (locatorType === customLocators.PARTIALBUTTONTEXT) {
return element(by.partialButtonText(locatorValue));
} else if (locatorType === customLocators.PARTIALLINKTEXT ) {
return element(by.partialLinkText(locatorValue));
} else {
logger.info('Cannot find any locator listed above');
}
}
export enum customLocators {
'CSS', 'XPATH', 'CSSTEXT', 'LINKTEXT', 'BUTTONTEXT', 'PARTIALBUTTONTEXT', 'PARTIALLINKTEXT'
}
没有找到任何元素(by.css)除了(枚举的第一个索引)之外,其他元素工作得非常好
locatorType===customLocators.CSS如上所述,我使用switch case作为替代方案,效果很好
public static async getElementByLocatorType(locatorType: customLocators,
locatorValue: string, expectedText?: string) {
switch (locatorType) {
case customLocators.CSS:
return element(by.css(locatorValue));
break;
case customLocators.XPATH:
return element(by.xpath(locatorValue));
break;
case customLocators.LINKTEXT:
return element(by.linkText(locatorValue));
break;
case customLocators.BUTTONTEXT:
return element(by.buttonText(locatorValue));
break;
case customLocators.CSSTEXT:
return element(by.cssContainingText(locatorValue, expectedText));
break;
case customLocators.PARTIALLINKTEXT:
return element(by.partialLinkText(locatorValue));
break;
case customLocators.PARTIALBUTTONTEXT:
return element(by.partialButtonText(locatorValue));
break;
default:
logger.info('browser : ' + locatorType + ' is invalid, Please check the selector name..');
}
}
请删除
代码>从第一个如果
条件传递给函数的定位器类型的值是多少?严格不等式运算符也是==代码>不是===代码>。您真的应该将其更改为switch语句。这会更容易理解。谢谢Nikhil的建议。!==工作正常,错误消失了。这是使用枚举的一个问题,尤其是枚举的“0”索引似乎不能很好地工作,除非我做了它(不等于比较值)。如果不使用枚举,则(locatorType:string)locatorType=='CSS'