How to test if a string contains one value of theses values

16,284 views
Skip to first unread message

Cyril H

unread,
Oct 21, 2014, 2:45:21 PM10/21/14
to robotframe...@googlegroups.com
Hi,

Having a string value, how to test if it contains one value of a list?

Here's what I'm trying to test :

*** Variables ***
@{list}    value1    value2

*** Test Cases ***
(...)
${mystring} = Get Table Cell tableId 1 1
Should Contain ${mystring} @{list}

==> I want this line failed if ${mystring} doesn't contain value1 or value 2


Any idea???

Thanks a lot

Tatu Aalto

unread,
Oct 21, 2014, 4:49:24 PM10/21/14
to sendm...@gmail.com, robotframe...@googlegroups.com
Ugh

Have looked Collections library keyword: List Should Contain Value keyword[1]

-Tatu

[1] http://robotframework.org/robotframework/latest/libraries/Collections.html#List%20Should%20Contain%20Value
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Cyril H

unread,
Oct 22, 2014, 12:23:14 PM10/22/14
to robotframe...@googlegroups.com, sendm...@gmail.com
Thank you for your reply Tatu.
Yes,  I already looked collection library. It doesn't help.  
Keyword like "List Should Contain" looks for value in list. In my case, I don't want to match my entire string with my collection. 

Example: Value1 and Value2 don't have to match string1 (List should contain's fonctionality). 
I want to know if string 1 contains Value1 or Value2. If it doesn't, my test fail.

Regrads



Le mardi 21 octobre 2014 18:49:24 UTC+2, Tatu Aalto a écrit :
Ugh

Have looked Collections library keyword: List Should Contain Value keyword[1]

-Tatu

[1] http://robotframework.org/robotframework/latest/libraries/Collections.html#List%20Should%20Contain%20Value
On 21.10.2014 17:45, Cyril H wrote:
Hi,

Having a string value, how to test if it contains one value of a list?

Here's what I'm trying to test :

*** Variables ***
@{list}    value1    value2

*** Test Cases ***
(...)
${mystring} = Get Table Cell tableId 1 1
Should Contain ${mystring} @{list}

==> I want this line failed if ${mystring} doesn't contain value1 or value 2


Any idea???

Thanks a lot
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Roman Fominych

unread,
Oct 22, 2014, 12:36:10 PM10/22/14
to robotframe...@googlegroups.com
exactly this in the new Robot version 2.8.6:

Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively

вторник, 21 октября 2014 г., 14:45:21 UTC пользователь Cyril H написал:

Kevin O.

unread,
Oct 22, 2014, 5:32:08 PM10/22/14
to robotframe...@googlegroups.com
RF does not have a Contains keyword - only assertions like Should Contain. In this case you are better writing your own keyword for this. Building a regexp based on an unknown number and content of a list is just too painful in RF syntax. So just using evaluate to do in a user keyword what the Python keyword does. FYI namespace arg to Evaluate not available until 2.8.4, and may show in red in RIDE.

def should_contain_one_of_these_substrings(value, *substrings):
    if not any(substring in value for substring in substrings):
        raise AssertionError("'%s' does not contain one of: %s" % (value, substrings))

Should Contain One Of These Substrings
    [Arguments]    ${value}    @{substrings}
    ${ns}=    Create Dictionary    substrings    ${substrings}    value    ${value}
    ${contains}=    Evaluate    any(substring in value for substring in substrings)    namespace=${ns}
    Run Keyword Unless    ${contains}    Fail    '${value}' does not contain one of: ${substrings}

Test
    Should Contain One Of These Substrings    abcde    bc
    Should Contain One Of These Substrings    abcde    zz    de
    Run Keyword And Expect Error    *    Should Contain One Of These Substrings    abcde    bb
    Run Keyword And Expect Error    *    Should Contain One Of These Substrings    asdf

Uri Shatalov

unread,
Oct 23, 2014, 10:15:43 AM10/23/14
to robotframe...@googlegroups.com
You can try to convert your list to string and then use Call Method

${myList}  Convert To String @{list}
${isFound} Call Method ${myList} find ${mystring}
Return From Keyword If ${isFound}>=0 True

Cyril H

unread,
Oct 23, 2014, 12:42:13 PM10/23/14
to robotframe...@googlegroups.com
Kevin,

That is very, very interesting ! 
This is the solution!!!

Big thank you
Reply all
Reply to author
Forward
0 new messages