有没有办法将定位器 "by" 作为量角器上的字符串传递?
Is there a way to pass locator "by" as a string on protractor?
我试图避免使用 switch-case 来验证定位器然后创建元素。
我的代码是这样的:
switch (name.by) {
case "class":
return element(by.className(name.value));
case "cssselector":
return element(by.cssSelector(name.value));
case "id":
return element(by.id(name.value));
case "linktext":
return element(by.linkText(name.value));
case "name":
return element(by.name(name.value));
case "partiallinktext":
return element(by.partialLinkText(name.value));
case "tagname":
return element(by.tagName(name.value));;
case "xpath":
return element(by.xpath(name.value));;
default:
throw new Error ('Invalid option.');
}
它接收一个像这样的 json 结构作为参数:
"rangeFrom": {
"by": "id",
"value": "txtRentFrom"
},
我正在寻找类似的东西:
element(by[name.by](name.value));
有谁知道是否有办法避免使用 switch-case?
const dict = {
"class": by.classname,
"cssselector": by.cssSelector
...
}
return element(dict[name.by](name.value));
我试图避免使用 switch-case 来验证定位器然后创建元素。
我的代码是这样的:
switch (name.by) {
case "class":
return element(by.className(name.value));
case "cssselector":
return element(by.cssSelector(name.value));
case "id":
return element(by.id(name.value));
case "linktext":
return element(by.linkText(name.value));
case "name":
return element(by.name(name.value));
case "partiallinktext":
return element(by.partialLinkText(name.value));
case "tagname":
return element(by.tagName(name.value));;
case "xpath":
return element(by.xpath(name.value));;
default:
throw new Error ('Invalid option.');
}
它接收一个像这样的 json 结构作为参数:
"rangeFrom": {
"by": "id",
"value": "txtRentFrom"
},
我正在寻找类似的东西:
element(by[name.by](name.value));
有谁知道是否有办法避免使用 switch-case?
const dict = {
"class": by.classname,
"cssselector": by.cssSelector
...
}
return element(dict[name.by](name.value));