【问题标题】:Create a loop while on Katalon with Groovy使用 Groovy 在 Katalon 上创建一个循环
【发布时间】:2023-01-20 04:18:44
【问题描述】:

我正在自动化测试,在某些步骤中我需要多次重复“Enter”键,因此我试图创建一个循环,在该循环中按下“Enter”直到对象可用或可见。

我已经尝试了很多不同的方法来做到这一点,但它从来没有奏效,通常 while 语句或 if 语句在没有条件被破坏的情况下中断。

在下面的示例中,我正在创建对象 x 和对象 y。我想重复 y 直到到达 x 可用的窗口。

这里还有一些我失败的尝试。




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')


【问题讨论】:

  • 这是您正在按 Enter 键的某个搜索字段吗?另外,当您要发送 Enter 键时,是什么让您想要 WebUI.click()

标签: java testing groovy automated-tests katalon-studio


【解决方案1】:

您可以像这样使用 WebUI.verifyElementPresent() 方法(注意:您的示例中缺少括号。另外,超时是必需的):

condition = true
while (true) {
    WebUI.click(y)
    if (WebUI.verifyElementPresent(x, 5)) { 
        condition = false
    }
}
WebUI.setText(x, '1')

【讨论】:

  • 来吧,伙计……这是一个无限的 while 循环,没有退出条件!
【解决方案2】:

并且在某些步骤中我需要多次重复“Enter”键,因此我试图创建一个循环,在该循环中按下“Enter”直到对象可用或可见

听起来好像按下“Enter”的那个字段是一个搜索字段,而“对象变得可用或可见”是一个搜索结果......

至于你的重试逻辑,看看my answer on the Katalon Studio forum

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.) 如果在 Enter 后结果不存在,我看不到我们正在采取的任何行动,除了垃圾 Enter。你应该说明在那种情况下我们应该做什么。

在没有任何 onRetry 逻辑的情况下,我认为您使用循环的想法以及我使用 ActionHandler 的想法是矫枉过正的。

请在此处回复您的完整用例,也许还有 AUT 本身的一些屏幕截图或链接,我可以调整这个答案!

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2016-10-17
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多