cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script - regex - conditional step (not working)

SOLVED
Highlighted
New Contributor

Groovy script - regex - conditional step (not working)

In a testcase I want to check the XML result. If the regex (6 digits '- ' 7digits) is met then the script needs to jump to a different step then when the condition is not met.
I've tried this:

 

def input = context.expand( '${DataSource#DocumentID}' )

def pattern = /^[0-9]{6}-{1}[0-9]{7}$/

if (input =~ pattern) {
testRunner.gotoStepByName("StepA")
}
else{
testRunner.gotoStepByName("StepB")
}

 

Unfortunately whatever I try my testcase continues with StepA.
help is much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Groovy script - regex - conditional step (not working)

Hey @Tommie,

I'm not a whizzy scripter like the other people on here like Rao, msiadak, avidCoder, etc. So i could be wrong, but i have a couple of thoughts that might help.

1. Have you looked at the conditional goto step? For a simple decision (like the one you specify this may work without having to rely on groovy)

2. You say your script only ever executes stepA (on the if statement). Have you tried swapping stepA with stepB (on the else) to prove that the else is never executed?

3. If the else is never executed (your groovy syntax on your if/else appears correct) this leads me to believe the issue is earlier in your script....have you tried adding some logging statements (log.info(pattern) and log.info(input) to ensure the variables are being assigned correctly?

4. Youre assigning the regex pattern to the 'pattern' variable. Have you tried removing this and just specifying the regex pattern against the input variable? (e.g. if(input ==~ /[0-9]{6}-{1}[0-9]{7}/) )

5. On the line
If(input=~pattern) youre using a single = symbol for assignment. Shouldnt this be equalsto instead? E.g. if(input==~pattern)

UPDATE TO POST. Regarding using =~ or ==~, check out the following link....this makes me think you should use equalsto, not the assignment operator (if i understand it correctly! https://www.quora.com/Whats-the-difference-between-a-and-in-Java )

I cant stand xmas day with my family;), so i'll have a go at playing with this later to see if i can get this to work.

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

2 REPLIES 2
Highlighted
Community Hero

Re: Groovy script - regex - conditional step (not working)

Hey @Tommie,

I'm not a whizzy scripter like the other people on here like Rao, msiadak, avidCoder, etc. So i could be wrong, but i have a couple of thoughts that might help.

1. Have you looked at the conditional goto step? For a simple decision (like the one you specify this may work without having to rely on groovy)

2. You say your script only ever executes stepA (on the if statement). Have you tried swapping stepA with stepB (on the else) to prove that the else is never executed?

3. If the else is never executed (your groovy syntax on your if/else appears correct) this leads me to believe the issue is earlier in your script....have you tried adding some logging statements (log.info(pattern) and log.info(input) to ensure the variables are being assigned correctly?

4. Youre assigning the regex pattern to the 'pattern' variable. Have you tried removing this and just specifying the regex pattern against the input variable? (e.g. if(input ==~ /[0-9]{6}-{1}[0-9]{7}/) )

5. On the line
If(input=~pattern) youre using a single = symbol for assignment. Shouldnt this be equalsto instead? E.g. if(input==~pattern)

UPDATE TO POST. Regarding using =~ or ==~, check out the following link....this makes me think you should use equalsto, not the assignment operator (if i understand it correctly! https://www.quora.com/Whats-the-difference-between-a-and-in-Java )

I cant stand xmas day with my family;), so i'll have a go at playing with this later to see if i can get this to work.

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

Highlighted
New Contributor

Re: Groovy script - regex - conditional step (not working)

Hi @richie,
Thank you very much for your response!
I've tried almost everything you mentioned before I posted my question.
But with all your suggstions I tried a few new things and found the problem!
I'm blaming myself for not seeing it before. There was a space in the name of the second step (the names I posted were simplified). So the name of the second step in my script was not the same of the step name. Removed the space.... working like a charm Smiley Happy
Kind regards!!

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors