How can I convert a list to string? Tried Convert To String without results

8,534 views
Skip to first unread message

Rafael Bermúdez

unread,
Mar 7, 2017, 3:26:54 PM3/7/17
to robotframework-users
Hello all.

I have the feeling that this is a really dumb question and I am doing something terrible wrong.

Here is what I have:

*** Keywords ***
Validate Page Title
[Arguments] ${PAGE CONTROLLER}
${PAGE TITLE}= Get Title
${PAGE CONTROLLER}= Get Regexp Matches ${PAGE CONTROLLER} (?<=\/)(.*?)(?=\/)
${PAGE CONTROLLER}= Get From List ${PAGE CONTROLLER} 0
Should Contain ${PAGE TITLE} ${PAGE CONTROLLER}

The idea here is that I pass the page controller/action as an argument (ie /foo/bar) so I can extract the controller (foo) to compare it with the title. I'm using a regexp to do this, but I'm unsure if this is a good practice.

The problem I have is that Get Regexp Matches returns a list, which I cannot use with the Should Contain keyword.I tried with Convert To String as follows to no avail:

*** Keywords ***
Validate Page Title
[Arguments] ${PAGE CONTROLLER}
${PAGE TITLE}= Get Title
${PAGE CONTROLLER}= Get Regexp Matches ${PAGE CONTROLLER} (?<=\/)(.*?)(?=\/)
${PAGE CONTROLLER}= Convert To String ${PAGE CONTROLLER}
Should Contain ${PAGE TITLE} ${PAGE CONTROLLER}

This is the output:

Test Robot Framework Logging :: Some info about the test step         | FAIL |
'Some title foo' does not contain '[u'foo']'



It is obvious to me that I'm doing something really wrong, but I just can't put my finger on it. I'm new to the automation world, and specially new to Robot Framework.

Any idea as to why the Convert To String keyword is failing? How should I address this?

Many thanks!

Aurélien Truco

unread,
Mar 8, 2017, 4:38:41 AM3/8/17
to robotframework-users
Hello,

A quick workaround for your Convert To String could be a combination of what you have already done:

Validate Page Title
   
[Arguments]  ${PAGE CONTROLLER}

    $
{PAGE TITLE}=    set variable  Some title foo  # ${PAGE TITLE}=  Get Title

    $
{PAGE CONTROLLER}=  Get Regexp Matches  ${PAGE CONTROLLER}  (?<=\/)(.*?)(?=\/)
    $
{PAGE CONTROLLER}=  Get From List  ${PAGE CONTROLLER}  0

    $
{PAGE CONTROLLER}=  Convert To String  ${PAGE CONTROLLER}
   
Should Contain  ${PAGE TITLE}  ${PAGE CONTROLLER}

And a quicker way to extract this value could be :

Validate Page Title 2
   
[Arguments]  ${PAGE CONTROLLER}
    $
{PAGE TITLE}=    set variable  Some title foo  # ${PAGE TITLE}=  Get Title

    $
{PAGE CONTROLLER}=  Get Regexp Matches  ${PAGE CONTROLLER}  (?<=\/)(.*?)(?=\/)

   
Should Contain  ${PAGE TITLE}  ${PAGE CONTROLLER[0]}

Rafael Bermúdez

unread,
Mar 9, 2017, 11:13:46 AM3/9/17
to robotframework-users
Hello Aurélien, thanks for your reply!

I was confused with the Convert To String keyword, as I was expecting it to convert the list into a one-big string.

For now I'll stick with the second option, but probably in the future I'll implement some pseudo-library in Python for doing in a more transparent way.

Thanks!
Rafa

Kevin O.

unread,
Mar 9, 2017, 12:05:24 PM3/9/17
to robotframework-users
Convert To String will convert anything to string.
It's similar to this Python:

>>> foo = [u'dog', u'pig']

>>> str(foo)

"[u'dog', u'pig']"


If you have a list of strings you want to convert to one giant string, you could:

${big_string}    Catenate    @{list_of_strings}

For ${list_of_strings} == [u'foo'], the result would actually be just the string 'foo'.

Rafael Bermúdez

unread,
Mar 9, 2017, 12:36:40 PM3/9/17
to robotframework-users
It turns out that I was completely misunderstanding what Convert To String does. My apologies.

I'm new to all this, and I would like very much to form proper coding practices and do it right. Probably there are a million ways to make it run, but I'm sure many of them will be just wrong.

Thanks!
Reply all
Reply to author
Forward
0 new messages