### Repeat on error until true

The next looping container is called repeat-on-error-until-true. This container repeats a group of actions in case one embedded action failed with error. In case of an error inside the container the loop will try to execute all embedded actions again in order to seek for overall success. The execution continues until all embedded actions were processed successfully or the ending condition evaluates to true and the error-loop will lead to final failure.

XML DSL

    <testcase name="iterateTest">
        <actions>
            <repeat-onerror-until-true index="i" condition="i = 5">
                <echo>
                    <message>index is: ${i}</message>
                </echo>
                <fail/>
            </repeat-onerror-until-true>
        </actions>
    </testcase>

Java DSL designer

    @CitrusTest
    public void repeatOnErrorTest() {
        repeatOnError(
            echo("index is: ${i}"),
            fail("Force loop to fail!")
        ).until("i = 5").index("i");
    }

Java DSL runner

    @CitrusTest
    public void repeatOnErrorTest() {
        repeatOnError().until("i = 5").index("i"))
            .actions(
                echo("index is: ${i}"),
                fail("Force loop to fail!")
            );
    }

In the code example the error-loop continues four times as the action definitely fails the test. During the fifth iteration The condition "i=5" evaluates to true and the loop breaks its processing leading to a final failure as the test actions were not successful.

Note The overall success of the test case depends on the error situation inside the repeat-onerror-until-true container. In case the loop breaks because of failing actions and the loop will discontinue its work the whole test case is failing too. The error loop processing is successful in case all embedded actions were not raising any errors during an iteration.

The repeat-on-error container also offers an automatic sleep mechanism. This auto-sleep property will force the container to wait a given amount of time before executing the next iteration. We used this mechanism a lot when validating database entries. Let's say we want to check the existence of an order entry in the database. Unfortunately the system under test is not very well performing and may need some time to store the new order. This amount of time is not predictable, especially when dealing with different hardware on our test environments (local testing vs. server testing). Following from that our test case may fail unpredictable only because of runtime conditions.

We can avoid unstable test cases that are based on these runtime conditions with the auto-sleep functionality.

XML DSL

    <repeat-onerror-until-true auto-sleep="1000" condition="i = 5" index="i">
        <echo>
            <sql datasource="testDataSource">
                <statement>
                  SELECT COUNT(1) AS CNT_ORDERS
                  FROM ORDERS
                  WHERE CUSTOMER_ID='${customerId}'
                </statement>
                <validate column="CNT_ORDERS" value="1"/>
            </sql>
        </echo>
    </repeat-onerror-until-true>

Java DSL designer and runner

    @CitrusTest
    public void repeatOnErrorTest() {
        repeatOnError().until("i = 5").index("i").autoSleep(1000))
            .actions(
                query(action -> action.dataSource(testDataSource)
                    .statement("SELECT COUNT(1) AS CNT_ORDERS FROM ORDERS WHERE CUSTOMER_ID='${customerId}'")
                    .validate("CNT_ORDERS", "1"))
            );
    }

We surrounded the database check with a repeat-onerror container having the auto-sleep property set to 1000 milliseconds. The repeat container will try to check the database up to five times with an automatic sleep of 1 second before every iteration. This gives the system under test up to five seconds time to store the new entry to the database. The test case is very stable and just fits to the hardware environment. On slow test environments the test may need several iterations to successfully read the database entry. On very fast environments the test may succeed right on the first try.

Important We changed auto sleep time from seconds to milliseconds with Citrus 2.0 release. So if you are coming from previous Citrus versions be sure to now use proper millisecond values.

So fast environments are not slowed down by static sleep operations and slower environments are still able to execute this test case with high stability.