我正在编写一个函数,试图在发生错误时单击重试元素(staleElement,不可见元素等)。显然我不希望它无限循环,以防该元素无法点击一个bug,所以我想插入一个计数器,但我遇到了一个问题:
我的测试代码是
var attempt = 0;
var tryClick = function(id, node)//clicks on the element defined by the given data-dojo-id and node(optional)
{
return function()
{
return this.parent
.setFindTimeout(30000)
.setPageLoadTimeout(30000)
.setExecuteAsyncTimeout(30000)
.sleep(1000)
.then(nodeByDId(id, node)) //this just sets the node as context
.click()
.end();
};
};
var clickByDId = function(id, node)//clicks on the element defined by the given data-dojo-id and node(optional)
{
return function()
{
var remote = this.parent;
console.log('clicking ' + id);
return this.parent
.then(tryClick(id, node))
.catch(function()
{
if (attempt <= 5)
{
attempt++;
console.log('Attempt to click '+ id + ': ' + attempt);
return remote
.sleep(1000)
.then(clickByDId(id, node));
}
else
{
attempt = 0;
throw 'cannot click element';
}
})
.sleep(1000);
};
};
我期待我的测试增加每次捕获的尝试,但是当我运行测试时,例如
'click':function()
{
return this.remote
.setFindTimeout(30000)
.setPageLoadTimeout(30000)
.setExecuteAsyncTimeout(30000)
.then(clickByDId(notVisibleWidgetId));
}
我从日志中得到的是
clicking notVisibleWidgetId
Attempt to click notVisibleWidgetId: 1
clicking notVisibleWidgetId
Attempt to click notVisibleWidgetId: 1
clicking notVisibleWidgetId
Attempt to click notVisibleWidgetId: 1
clicking notVisibleWidgetId
Attempt to click notVisibleWidgetId: 1
clicking notVisibleWidgetId
Attempt to click notVisibleWidgetId: 1
等。 谁能向我解释那些结果? catch部分不应该增加变量吗?有人知道如何实现类似的东西吗?