简体   繁体   中英

How to compare two string in robot framework using "Evaluate" keyword

I want to compare two string using Evaluate (builtIn keyword in robotframework). In this test I am connecting to router and trying to execute cisco command ( show route local), and I want to compare the output of this command to a given string in order to do the if statement.

Write   show route local
${output} =    Read    delay=0.5s
${output1} =    Read    delay=0.5s
${status} =   Evaluate     "${output1}" = "% Ambiguous command:  "show route local""
IF   ${status}= True

     Write   show route local connected
     ${output} =    Read    delay=0.5s
     Set Suite Variable    ${G_stdout}    ${output}
ELSE 
    Log to Console   ${output1}
    Set Suite Variable    ${G_stdout}    ${output1}        
END

This error appears when I execute the test:

ModuleNotFoundError: No module named '"showroutelocal""'

The error is because your string has more than one consecutive space, so robot thinks "show route local" is an extra argument representing the name of a module to be imported.

To fix that, you need to escape one of the spaces in the string on the right hand side of the equation. There are other problems with your expression as well. Here's a version that works:

${status}=  Evaluate  $output1 == '% Ambiguous command: \ "show route local"'

The things I changed:

  • == instead of =
  • $output1 instead of "${output1}" , since ${output1} itself has double quotes in it which would cause a syntax error
  • single quotes around the string on the right, since the string itself contains double quotes which would cause a syntax error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM