Vyacheslav
Vyacheslav

Reputation: 662

Why I couldn't work with a dictionary in different keywords in the Robot framework?

I want to work with the dictionary in different keywords. But I get the error when I try to work with the dictionary in another keyword:

AttributeError: 'str' object has no attribute 'keys'

This is my sample code:

*** Settings ***
Library  Collections

*** Variables ***
&{PRODUCTS}

*** Keywords ***
Set the Dictionary
  &{PRODUCTS} =  create dictionary  OrderN=R0
      ...  OrderPersonal=No_Account OrderPosition=EmptyPosition

Get the Dictionary
  log dictionary  \&{PRODUCTS}

*** Test Cases ***
Using the dictionary type of variables
  Set the Dictionary
  Get the Dictionary
  set to dictionary  &{PRODUCTS}  OrderPosition  TestPosition
  Get the Dictionary

Why is the variable not accessible in another keyword? How can I resolve the issue?

Upvotes: 0

Views: 1245

Answers (1)

A. Kootstra
A. Kootstra

Reputation: 6961

The problem you are facing is because of Robot Framework variable scope. This is explained in the following Robot Framework guide section: Variable priorities and scopes.

In this case I think you should use one of the Set Test/Suite/Global Variable keywords to store the variable outside the keyword scope.

*** Keywords ***
Set the Dictionary
  &{PRODUCTS} =  create dictionary  OrderN=R0
      ...  OrderPersonal=No_Account OrderPosition=EmptyPosition
  Set Test Variable    ${PRODUCTS}    &{PRODUCTS}

Upvotes: 2

Related Questions