How to make Robot Framework functions accept arguments as strings instead of the default unicode type

StackOverflow https://stackoverflow.com/questions/20329202

Pregunta

I am writing a new RF library which are expected to take string arguments because the (pre-existing) Python library that I use is expecting strings, not unicode. Ofcourse I can convert each unicode to string before calling my existing function which supports only strings.

import ConfigParser

class RFConfigParser:

def get (self,section, option):
    print type (section) #prints unicode
    section = str (section) #works but I dont want to do this
    return self._config.get (section, option) #this pre-existing function expect a string input

The problem is I have lots of similar functions and in each of these functions I will have to call this unicode to string conversion circus.

Is there a straight forward way of doing this, so that the RF function will directly accept in string format

Another question is the default unicode support a Robot Framework feature or a RIDE feature ? (I am using RIDE, is that why I am getting this problem)

¿Fue útil?

Solución

You can cast those Unicode strings to normal strings using Evaluate keyword before you pass them to your library.

Something like this:

lib.py:

def foo(foo):
    print type(foo)

test.txt

*** Settings ***
Library           lib.py

*** Test Cases ***
demo
    ${bar}    Evaluate    str('bar')
    foo    ${bar}

What the best solution is depends on the exact situation. Maybe one solution is to write a keyword that does that casting for you and then calls the library function. Maybe the best option still is to just modify your library to accept Unicode strings. It depends.

Otros consejos

If you are using the remote library, please be aware that RobotFramework checks the content of the data to be transported via XmlRpc:

def _handle_binary_result(self, result):
    if not self._contains_binary(result):
        return result
    try:
        result = str(result)
    ...

If the data happens to only contain ASCII it will transport a string, if the data cannot be encoded in ASCII it will transport binary data. I am not aware how you could enforce a unicode result type and avoid the complaint from a comparison operation such as Should Be Equal about different data types:

Argument types are: <type 'str'> <type 'unicode'>

If in my Remote Library I do something like:

return unicode(result, 'utf-8')

for the given input, results of varying type are received

return unicode(u'test', 'utf-8')  --> 'test'
return unicode(u'ü', 'utf-8')     --> u'ü'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top