[英]Create a loop while on Katalon with Groovy
我正在自动化测试,在某些步骤中我需要多次重复“Enter”键,因此我试图创建一个循环,在该循环中按下“Enter”直到 object 可用或可见。
我已经尝试了很多不同的方法来做到这一点,但它从来没有奏效,通常 while 语句或 if 语句在没有条件被破坏的情况下中断。
在以下示例中,我创建了 object x 和 object y。 我想重复 y 直到到达 x 可用的 window。
这里还有一些我失败的尝试。
TestObject x = findTestObject('path/1')
TestObject y = findTestObject('path/2')
while (true) {
WebUI.click(y)
if (WebUI.verifyElementPresent) break
}
//
//while (WebUI.verifyElementNotPresent(x, 10)) {
// WebUI.click(y)
//}
//while(true) {
// WebUI.click(y)
// if(WebUI.verifyElementVisible(x))
// WebUI.click(y)
//}
Example of what I am trying to avoid.
WebUI.click(y)
WebUI.click(y)
WebUI.click(y)
WebUI.setText(x, '1')
您可以像这样使用WebUI.verifyElementPresent()
方法(注意:您的示例中缺少括号。另外,超时是必需的):
condition = true
while (true) {
WebUI.click(y)
if (WebUI.verifyElementPresent(x, 5)) {
condition = false
}
}
WebUI.setText(x, '1')
在某些步骤中,我需要多次重复“Enter”键,因此我尝试创建一个循环,在该循环中按下“Enter”,直到 object 可用或可见
听起来好像按下“Enter”的那个字段是一个搜索字段,而“对象变得可用或可见”是一个搜索结果......
至于你的重试逻辑,看看我在 Katalon Studio 论坛上的回答:
public class ActionHandler {
public static void HandleRetryableAction(Closure onAction, Closure onDone, long timeOut) {
long startTime = System.currentTimeSeconds();
while (System.currentTimeSeconds() < startTime + timeOut) {
try {
onDone(true, onAction());
return;
} catch (Exception ex) {
onDone(false, ex);
}
}
}
}
您应该像这样使用此自定义关键字:
ActionHandler.HandleRetryableAction({
WebUI.sendKeys(findTestObject('path/2'), // you should REALLY name this something better...something more meaningful...
Keys.ENTER.toString());
final TestObject firstSearchResult = findTestObject('path/1'); // again, fix the naming please!!
// we're waiting on search result to **disappear**, in order to squash any flakiness that comes from the next step...
WebUI.waitForElementNotPresent(firstSearchResult,
1,
FailureHandling.OPTIONAL);
return WebUI.waitForElementPresent(firstSearchResult,
5);
}, { boolean success, _ ->
if (!success) {
// take additional actions, such as refreshing the page, clicking some refresh widget, ...
}
},
15, // I set it to 3 times the wait time specified by Mate Mrse, for good measure
)
这里要注意三点...
1.) 是WebUI.sendKeys()
来……好吧……发送密钥。 此外, String 参数是org.openqa.selenium.Keys.ENTER
的字符串化。
2.) 我们在这里使用WebUI.waitForElementPresent()
。 这是内置关键字。
3.) 如果输入后结果不存在,我看不到我们正在采取的任何行动,除了垃圾输入。 你应该说明在那种情况下我们应该做什么。
在没有任何 onRetry 逻辑的情况下,我认为您使用循环的想法以及我使用ActionHandler
的想法是矫枉过正的。
请在此处回复您的完整用例,也许还有 AUT 本身的一些屏幕截图或链接,我可以调整这个答案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.