问题描述
我尝试使用Locator Builder(https://codecept.io/locators/#locator-builder),并注意到当我使用 withAttr 时,它会进行相等的比较
例如:locate('a').withAttr({href: '/order/offer/'})
翻译成:
.//a[@href = '/order/offer']
我正在寻找 withAttr 转换为“包含”比较的选项。
例如:.//a[contains(@href,'/order/offer')]
由于href末尾具有动态值,因此我不得不在xpath中使用“ contains”。想知道位置生成器是否有类似的选项
注意:我看到 withText 进行了包含比较
解决方法
你可以使用类似的东西
locate('a[href^='/order/offer/']')
这将转化为
.//a[starts-with(@href,'/order/offer')]
不,您不能使用定位器生成器来完成此操作,因为只有该功能的开发人员才能定义其功能。您可以提出功能请求,但是根据我的经验,此功能并未得到广泛使用,因此很可能不会改进或创建新命令。
,您可能正在寻找Custom Locators。
// inside a plugin or a bootstrap script:
codeceptjs.locator.addFilter((providedLocator,locatorObj) => {
if (typeof providedLocator === 'string') {
// this is a string
if (providedLocator[0] === '=') {
locatorObj.value = `.//*[text()="${providedLocator.substring(1)}"]`;
locatorObj.type = 'xpath';
}
}
});
这意味着:
I.click('=Login');
您可能会很有创意。