1

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    your code is garbled. – Bryan Oakley May 22 '15 at 10:57
  • Here are more and better options to compare strings - https://stackoverflow.com/questions/52952283/how-to-search-for-empty-string-compare-two-strings-in-robot-framework/52952887#52952887 – Dev May 19 '19 at 19:43

4 Answers4

7

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}
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • "Should be equal as strings" may not solve the problem when values are and ; \n However, "Should be equal as integers" will not have this side effect, which is recommended. \n I'm using it to equals the HTTP STATUS CODE. – Ranger Way Feb 26 '16 at 02:15
  • @GaGa_Ek: correct. However, this question was about comparing strings, not integers. – Bryan Oakley Feb 26 '16 at 02:39
0

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}
perror
  • 7,071
  • 16
  • 58
  • 85
locika
  • 11
  • 1
  • 3
0

Perhaps using evaluate is the easiest, especially when rc need

[Documentation]    = / compare two string \ =
${rc}=    evaluate    'name'=='theon'
Log To Console   \n${rc}
LinconFive
  • 1,718
  • 1
  • 19
  • 24
0
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)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '21 at 11:22