I’m running into an error with my program. Each of the elements I am looping through do not have a unique-tested. The only thing that distinguishes them is their text, which I am trying to verify if any of them matches with the provided argument.
I am using a for loop to iterate across each of the messages to look for the desired text. I have used first-of-type and nth-child to try to specify the index (amongst other things like nth-of-type(${index})). However I still get the same strict mode error.
Sorry, here is the code in the screenshot if you’d like to copy-paste it
Verify Toastify Message Appears
[Arguments] ${text}
${count}= Get Element Count ${TOAST_BODY}
${found}= Set Variable False
FOR ${index} IN RANGE 1 ${count}+1
IF ${index} == 1
${toast_text}= Get Text ${TOAST_BODY}:nth-of-type(1)
ELSE
${toast_text}= Get Text ${TOAST_BODY}:nth-child(${index})
END
Run Keyword If ‘${text}’ in ‘${toast_text}’
… Set Variable ${found} True
END
IF ${found}
Toastify message with text “${text}” WAS found
ELSE
Toastify message with text “${text}” WAS NOT found
END
An easier approach might be to use Get Elements with ${TOAST_BODY} and then for item in the list you got from Get Elements, something like this:
Verify Toastify Message Appears
[Arguments] ${text}
${alerts}= Get Elements ${TOAST_BODY}
${found}= Set Variable False
FOR ${alert} IN @{alerts}
${toast_text}= Get Text ${alert}
Run Keyword If ‘${text}’ in ‘${toast_text}’
… Set Variable ${found} True
END
IF ${found}
Toastify message with text “${text}” WAS found
ELSE
Toastify message with text “${text}” WAS NOT found
END
Thanks so much for the help Dave your code works (with a couple minor fixes)!
Here is the final code I used.
Verify Toastify Message Appears
[Arguments] ${text}
${alerts}= Get Elements ${TOAST_BODY}
${found}= Set Variable False
FOR ${alert} IN @{alerts}
${toast_text}= Get Text ${alert}
Run Keyword If '${text}' in '${toast_text}'
... Set Variable {found} True
END
IF ${found}
Log Toastify message with text “${text}” WAS found
ELSE
Log Toastify message with text “${text}” WAS NOT found
END