简体   繁体   中英

How to compare two strings equal or not in Robot Framework

How to compare two strings equal or not in Robot Framework. For example:

${xyz}=    Get Text    xpath=/html/body/div/div[2]/div[3]/div/div/div/div/h3
${abc}=    Get Text    xpath=/html/body/div/div[2]/div[4]/div/div/div/div/h3

These xpath values are getting different strings. So how to compare there strings equal or not?

Is it correct way to storing the values in variable in Robot Framework?

Yes, that is the correct way of storing variables. Though you can also do it without the equal sign:

${xyz}    Get Text    xpath=/html/body/div/div[2]/div[3]/div/div/div/div/h3
${abc}    Get Text    xpath=/html/body/div/div[2]/div[4]/div/div/div/div/h3

Now that you have the two different strings assigned to variables, you can simply do:

Should Be Equal As Strings    ${xyz}    ${abc}

You can see the documentation for Should Be Equal As Strings here .

An alternative to Should Be Equal As Strings is to use Should Be True - it evaluates the provided argument as python expression (eg "something that can be substituted with a value") and fails if it turns up False :

Should Be True     """${variable 1}""" == """${variable 1}"""

It's a good practice to use triple quotes when you don't have control over the values (eg when taken from external source - a web page) - this construct permits the value to have quotes or newlines in it, without causing syntax error.

Though this approach can be a bit cumbersome for simple checks, it is quite powerful - you can use whatever python provides. For example, if the check should be case insensitive:

Should Be True     """${variable 1}""".lower() == """${variable 1}""".lower()

Or if any starting/trailing whitespace is insignificant:

Should Be True     """${variable 1}""".strip() == """${variable 1}""".strip()

Or, is one string a part of another:

Should Be True     """${variable 1}""" in """${variable 1}"""

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