简体   繁体   中英

Create a loop while on Katalon with Groovy

I am automating a test, and there are some steps where I need to repeat the 'Enter' key many times, so I am trying to create a loop where 'Enter' is pressed until an object becomes available, or visible.

I already tried quite a few different ways to do it, but it never works, normally the while statement or the if statement breaks without the condition being broken.

On the following example, I am creating object x and object y. I want to repeat y until I get to a window where x is available.

Also here are a few of my failed attempts.




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


You can use WebUI.verifyElementPresent() method like this (Note: you are missing the parenthesis in your example. Also, timeout is required):

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

and there are some steps where I need to repeat the 'Enter' key many times, so I am trying to create a loop where 'Enter' is pressed until an object becomes available, or visible

It sounds like that field that "Enter" is being pressed on, is a search field, and that "object becoming available or visible" is a search result...

As for your retry logic, take a look at 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);
            }
        }
    }
}

You should use this custom keyword like this:

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
)

Take note of three things here...

1.) It's WebUI.sendKeys() to...well...send the keys. Also, the String argument is the stringification of org.openqa.selenium.Keys.ENTER .

2.) We are using the WebUI.waitForElementPresent() here. This is built-in keyword.

3.) I don't see any action we're taking if the result is NOT present after Enter, except spamming Enter. You should spell out what we should do in that case.

In the absence of any onRetry logic, I think your idea to use loop, and my idea to use ActionHandler , is overkill.

Please respond back with your full use case here, and maybe some screenshots of or link to the AUT itself, and I can adjust this answer to that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM