Forum Discussion

Tommie's avatar
Tommie
New Contributor
5 years ago

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!

  • 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

2 Replies

  • richie's avatar
    richie
    Community Hero
    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
    • Tommie's avatar
      Tommie
      New Contributor

      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 :smileyhappy:
      Kind regards!!