团队,我有一个功能,需要找到元素
var EC = protractor.ExpectedConditions;
var busyLoaderEC = EC.stalenessOf(busyLoaderElement);
var eleClickPresenceEC = EC.presenceOf(eleClickObject);
var eleClickableEC = EC.elementToBeClickable(eleClickObject);
var deferred = Q.defer();
try {
browser.wait(EC.and(busyLoaderEC, eleClickPresenceEC, eleClickableEC),
config.timeout, constants.errorMessages.conditionTimedOut).then(function () {
browser.sleep(1000); //Force Delay
commonHelper.highlightElement(eleClickObject);
}, function (error) {
commonHelper.writeLog("ClickObject: " + error);
deferred.reject(["ClickObject: " + error, reportFiller]);
});
}
catch (error) {
deferred.reject(["ClickObject: Element Not found -" + error, reportFiller]);
}
当元素是 id 并且预期找不到它时,它将等待config.timeout
完成,然后返回错误,并显示我在{{1}中定义的错误消息}}
但对于CSS或xpath定位器,如果找不到此元素,则没有超时,它会立即抛出以下错误:
constants.errorMessages.conditionTimedOut
这很烦人,因为我希望我的测试用例继续,即使遇到“element_not_found”错误。
你可以帮我解决这个问题吗?
答案 0 :(得分:0)
当您致电ExpectedConditions
时,似乎stalenessOf
拨打了presenceOf
。 presenceOf
调用ElementFinder.isPresent
方法,如果您查看isPresent
method:
/**
* Returns true if there are any elements present that match the finder.
*
* @alias element.all(locator).isPresent()
*
* @example
* expect($('.item').isPresent()).toBeTruthy();
*
* @returns {Promise<boolean>}
*/
isPresent(): wdpromise.Promise<boolean> {
return this.count().then((count) => {
return count > 0;
});
}
只要调用这些EC方法,就会将它们抛到控制流上并进行评估。因此,当您在顶部初始化这些值时:
var busyLoaderEC = EC.stalenessOf(busyLoaderElement);
var eleClickPresenceEC = EC.presenceOf(eleClickObject);
var eleClickableEC = EC.elementToBeClickable(eleClickObject);
您正在立即解决控制流程中的这些承诺。这意味着当您致电browser.wait
时,它们应该会失败,因为这些元素不会等待config.timeout
等待显示在屏幕上,不会出现在屏幕上,或者被检查为可点击。
所以应该发生的事情是:
browser.wait(EC.and(EC.stalenessOf(busyLoaderElement),
EC.presenceOf(eleClickObject),
EC.elementToBeClickable(eleClickObject)),
config.timeout, constants.errorMessages.conditionTimedOut).then(() => {