Continue does not work properly in nested forEach loops

I need to use continue in nested forEach loops.

I want to skip iteration in this case:

outerLoop: outer_loop3
middleLoop: middle_loop3
innerLoop: inner_loop2

after this case I want to go to next iteration whuch is:

outerLoop: outer_loop4
middleLoop: middle_loop1
innerLoop: inner_loop1

I tried to use goToLabel + counitue commands (and separately also)
But looks like continue command does not work as expected to be.

Please advice :pray:

Here’s my example:

{
  "Name": "demo-1",
  "CreationDate": "2024-2-23",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  {\n    \"outer_loop\":\"outer_loop 1\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 2\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 3\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 4\"\n  }\n];",
      "Value": "outerLoops",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  \"middle_loop1\", \n  \"middle_loop2\", \n  \"middle_loop3\"\n];",
      "Value": "middleLoops",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  {\n    \"inner_loop\":\"inner_loop1\"\n  },\n  {\n    \"inner_loop\":\"inner_loop2\"\n  }\n];",
      "Value": "innerLoops",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "outerLoops",
      "Value": "outerLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "outerLoop: ${outerLoop.outer_loop}",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "middleLoops",
      "Value": "middleLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "middleLoop: ${middleLoop}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "innerLoops",
      "Value": "innerLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "innerLoop: ${innerLoop.inner_loop}",
      "Value": "red",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

This output I expect:

[echo] outerLoop: outer_loop1
[echo] middleLoop: middle_loop1
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop2
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop3
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2

[echo] outerLoop: outer_loop2
[echo] middleLoop: middle_loop1
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop2
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop3
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2

[echo] outerLoop: outer_loop3
[echo] middleLoop: middle_loop1
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop2
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop3
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2 ### this step should be skipped

[echo] outerLoop: outer_loop4
[echo] middleLoop: middle_loop1
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop2
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
[echo] middleLoop: middle_loop3
[echo] innerLoop: inner_loop1
[echo] innerLoop: inner_loop2
1 Like

I ran your test macro and got the below output. This looks ok. But the test macro does not have CONTINUE inside? Can you post your CONTINUE test macro?

I added CONTINUE and I think see the issue. CONTINUE skips the remainder of the current loop. In the case of nested loops, it skips to the next iteration of the innermost loop. But it seems it skips the middle loop as well?

Jus to confirm, is this the issue you are seeing?

{
  "Name": "innerloop2",
  "CreationDate": "2024-2-23",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  {\n    \"outer_loop\":\"outer_loop 1\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 2\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 3\"\n  },\n  {\n    \"outer_loop\":\"outer_loop 4\"\n  }\n];",
      "Value": "outerLoops",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  \"middle_loop1\", \n  \"middle_loop2\", \n  \"middle_loop3\"\n];",
      "Value": "middleLoops",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return [\n  {\n    \"inner_loop\":\"inner_loop1\"\n  },\n  {\n    \"inner_loop\":\"inner_loop2\"\n  }\n];",
      "Value": "innerLoops",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "outerLoops",
      "Value": "outerLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "outerLoop: ${outerLoop.outer_loop}",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "middleLoops",
      "Value": "middleLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "middleLoop: ${middleLoop}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "innerLoops",
      "Value": "innerLoop",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "innerLoop: ${innerLoop.inner_loop}",
      "Value": "red",
      "Description": ""
    },
    {
      "Command": "continue",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

Exactly, @admin

middle_loop2 never executed for outer_loop1
middle_loop1 never executed for outer_loop2

and so on

If use your script above with continue I expect this out put


outerLoop: outer_loop1
middleLoop: middle_loop1
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop2
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop3
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
outerLoop: outer_loop2
middleLoop: middle_loop1
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop2
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop3
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
outerLoop: outer_loop3
middleLoop: middle_loop1
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop2
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop3
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
outerLoop: outer_loop4
middleLoop: middle_loop1
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop2
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step
middleLoop: middle_loop3
innerLoop: inner_loop1
innerLoop: inner_loop2 ## skip this step

I agree, this is what it should be. => I added this issue to our “todo” list.

Thank you!
May I ask about estimation of this fix will be released?

There are some items before this on our todo list, but we should be able to work on this in March.

1 Like

Hello,

I was retrofitting our old macro to use loops rather than goto labels and I encountered this problem.
I could confirm that this also affects other loop commands nested within forEach.
The same bug occurs with the break command.

Here is a simple macro that uses a Times loop within a ForEach loop:

{
  "Name": "ForEach-Times-incorrectness",
  "CreationDate": "2024-3-17",
  "Commands": [
    {
      "Command": "executeScript_Sandbox",
      "Target": "for(i=1,n=[];i<=100;i++)n.push(i);return n;",
      "Value": "ARRAYOFNUMBERS",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return 0;",
      "Value": "counter",
      "Description": ""
    },
    {
      "Command": "forEach",
      "Target": "ARRAYOFNUMBERS",
      "Value": "UNUSED_VARIABLE",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${counter} + 1",
      "Value": "counter",
      "Description": ""
    },
    {
      "Command": "times",
      "Target": "4",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "Iteration: ${counter}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "continue",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

Expected Output: This echoes numbers from 1 to 100 but prints each number 4 times.

Real Output (as of 9.0.5): Echoes 1 to 100 BUT without repeating any numbers, while skipping any numbers that are divisible by 5. (i.e., 1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21…99)

Also, it was kind of funny that, changing the continue command to break immediately stops the loop in first iteration. If the continue command is removed entirely, then it would produce the expected output.

It appears that the continue/break commands apply to all loops while it should only apply to the loop it’s currently in.

In any case, looks like I’ll revert it back to the good ol’ gotoLabel and gotoIf for now.

Hope this helps.

1 Like

Thanks for this very useful test case. Here is the good news: We fixed this bug literally just last week :slight_smile: I tested your macro with our internal version and it works:

continue selenium ide

I will update this post once more when the new version is released, hopefully in a few days already :wink:

Both issues (CONTINUE and BREAK) were fixed in V9.1.3 :slight_smile: