简体   繁体   中英

Run Keyword If key exists in dictionary (Robot Framework)

I am currently using Robot Framework to automate tests for a form. To feed the form data I am using dictionaries like these:

*** Variables ***
&{TestCase1}    key1=a    key2=b    key3=c    key4=d
&{TestCase2}    key2=x    key3=y

What I am trying to do is condition the filling of certain fields on if the matching key exists in the dictionary for that test case, so that optional fields can be left blank. I tried running the the following keyword:

*** Keywords ***
Fill Form
    [Arguments]    &{TestCase}
    Run Keyword If    &{TestCase}[key1]    Input Text    id=field1    &{TestCase}[key1]
    Run Keyword If    &{TestCase}[key2]    Input Text    id=field2    &{TestCase}[key2]
    Run Keyword If    &{TestCase}[key3]    Input Text    id=field3    &{TestCase}[key3]
    Run Keyword If    &{TestCase}[key4]    Input Text    id=field4    &{TestCase}[key4]

...but to no avail. I'm getting this error:

FAIL: Dictionary &{TestCase} has no key 'key1'.

...which makes sense to some extent, because it doesn't, but that was the point. I expected this to make the condition evaluate to False and make RF skip the keyword.

Can anyone explain why it doesn't work that way and if there is another way to make this happen? Any help is greatly appreciated!

You need to check for the existence of the key, not the value. In python this would look like if 'key1' in TestCase , so in robot syntax it would look like this:

Run keyword if  'key1' in $TestCase  Input Text  ...

Here is a complete example. When run, it should add "key1 is in the log as expected" but not "bogus is unexpectedly in the log"

*** Variables ***
&{TestCase1}    key1=a    key2=b    key3=c    key4=d
&{TestCase2}    key2=x    key3=y

*** Test Cases ***
Example
    Run keyword if  'key1' in $TestCase1  log  key1 is in the log as expected
    Run keyword if  'bogus' in $TestCase1  log  bogus is unexpectedly in the log

As highlighted by both @Swapnil and @Bryan, there are several ways to achieve this. The below example uses a mapping for the key/field Id combination and then a test case that provides the key value pairs. The fill script just executes a for loop and doesn't care about the number of fields. The Input Text keyword is simulated by a custom one for logging to console.

*** Settings ***
Library    Collections    

*** Variables ***
&{mapping}
    ...    key1=field1    key2=field2
    ...    key3=field3    key4=field4

*** Test Cases ***
Test Case 1
    &{testcase}    Create Dictionary
    ...    key1=value1   key3=value3
    ...       
    Log to Console    \n    #For Formatting purpose
    Fill Form    ${testcase}    ${mapping}

Test Case 2
    &{testcase}    Create Dictionary
    ...    key1=value1    key3=value3   
    ...    key2=value2

    Log to Console    \n    #For Formatting purpose
    Fill Form    ${testcase}    ${mapping}

*** Keywords ***
Fill Form
    [Arguments]    ${fields}    ${mapping}
    ${keys}    Get Dictionary Keys    ${fields}    

    FOR    ${key}    IN    @{keys}
        Input Text    id=${mapping}[${key}]    ${fields}[${key}]
    END 

Input Text
    [Arguments]    ${locator}    ${value}
    Log To Console   Test Name: "${TEST NAME}" Locator: [${locator}] Value: "${value}"

Will yield the following result:

==============================================================================
Test Case 1                                                           

Test Name: "Test Case 1" Locator: [id=field1] Value: "value1"
Test Name: "Test Case 1" Locator: [id=field3] Value: "value3"
| PASS |
------------------------------------------------------------------------------
Test Case 2                                                           

Test Name: "Test Case 2" Locator: [id=field1] Value: "value1"
Test Name: "Test Case 2" Locator: [id=field2] Value: "value2"
Test Name: "Test Case 2" Locator: [id=field3] Value: "value3"
| PASS |
------------------------------------------------------------------------------

Though others have already answered this questions. I tried with a different approach and it worked.

Run keyword if  '${TestCase.get("key1")}'!='${None}'  <your keyword>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM