How to compare two values as strings using robot framework?

How to Match the values when objects are unequal,but they are strings.

${tab}= Get Text xpath=.//[@id='projectTable_info'] ${selected text}= Fetch From Right ${tab} of ${selected text}= Fetch From Right ${tab} of ${sele}= Fetch From Left ${selected text} entries ${empno}= Get Table Cell
xpath=.//
[@id='projectTable'] 3 6 Get Value ${empno} ${only value}= Fetch from Right ${empno} | Should Be String ${only value} ${sele} Convert To String ${only value} Convert To String ${sele} Should Be Equal ${only value} ${sele}

It gives error in console Fails if objects are unequal after converting them to strings. INFO Argument types are:
FAIL 2 != 2

Upvotes: 1

Views: 54085

Answers (4)

Gurinder
Gurinder

Reputation: 1

def compare(number1, relation, number2):
    if relation == "<":
        assert float(number1) < float(number2)
    if relation == ">":
        assert float(number1) > float(number2)
    if relation == "=>":
        assert float(number1) >= float(number2)
    if relation == "<=":
        assert float(number1) <= float(number2)
    if relation == "=":
        assert float(number1) == float(number2)

Upvotes: 0

LinconFive
LinconFive

Reputation: 2012

Perhaps using evaluate is the easiest, especially when rc need

[Documentation]    = / compare two string \ =
${rc}=    evaluate    'name'=='theon'
Log To Console   \n${rc}

Upvotes: 0

locika
locika

Reputation: 11

This script tries to convert input to float and compares the values in Python 2:

def should_be_x_than (self, number1, relation, number2):
    '''
    This keyword makes relation between 2 numbers (it converts them to number)
    Accepted relations:
    < > <= => =
    '''
    if relation =="<":
        return float(number1) < float(number2)
    if relation ==">":
        return float(number1) > float(number2)
    if relation =="=>":
        return float(number1) >= float(number2)
    if relation =="<=":
        return float(number1) <= float(number2)
    if relation =="=":
        return float(number1) == float(number2)

After that I imported the library and used it like a keyword (Should Be X Than).

Example:

Should Be X Than  ${num1}  <  ${num2}

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385970

Instead of Should be equal, you can use Should be equal as strings which converts the values to strings before doing the comparison.

Should be equal as strings    ${only value}    ${sele}

Your code seems to be attempting to manually convert the values to strings which is also a reasonable solution. Unfortunately, the documentation for Convert to string is a little vague causing you to use it incorrectly. The keyword doesn't change the argument, it returns a new string.

If you want to manually convert your variables, you need to do it like this:

${sele}=          Convert to string    ${sele}
${only value}=    Convert to string    ${only value}
Should be equal   ${only value}    ${sele}

Upvotes: 7

Related Questions