[Keywords] Map variable

In the beginning, I created a new custom keyword:@Keyworddef testMapVariable (Map <String, String> myMap) {}Then I created a new test case and using the manual mode.I add the custom keyword that I created.When I try to specify a value in the input field by double click,Param type is Map and Value type is test object.And each time have to replace Test object by selecting Map from the drop-down list.Maybe I'm wrongly passing the Map variable as a parameterand would be very grateful if you could give me a solution.

My CustomKeyword

Add it in Test Case and

after double click on Input field

and change value type to Map

image.png

map_no_to.png

myMap.png

manual_replaca_value_type.png

Hi,
You may create keyword in this way also,

public class testOfTest {

@Keyword

public void map(Map<String, String> myMap){

}

}

Hi abhishek kumar,

thanks of course, but the access modifiers do not change anything. The value type is still test object.

xio,

Please correct me if I am wrong. Did you try to set “findTestObject()” into the Map variable? findTestObject return an Object, not a Map.

Screen Shot 2018-10-30 at 16.53.57.png

Screen Shot 2018-10-30 at 16.54.09.png

Brian Ducson said:

xio,

Please correct me if I am wrong. Did you try to set “findTestObject()” into the Map variable? findTestObject return an Object, not a Map.

Of course not. I never tried to pass the findTestObject () method to the Map variable.

My goal is to transfer Map to a custom method as a parameter and later use it in a test case by setting parameters in manual mode.

And when I open the test case in manual mode, the value type turns out to be a String (used to be TestObject) in the place of Map. Therefore, you have to specify the correct value type Map each time, choosing from the drop-down list.

In the task, I ask that the Map variable when editing the parameters of the custom method have the correct Value Type

I guess that the dialog titled Input is designed simple (stupid, buggy … whatever you may say).

  • The Input dialog does not recognize your my_map param is defined to be of Map type.
  • It just assumes every param should be of String type as default.
  • It expects users to manually change the Value Type if he/she requires some other types.

as you see.

The Input dialog seems not to be capable of implementing what you want.


Possible workaround:

  1. Stick to the idea of using the Input dialog to set/change values of params.
  2. Abandon single param of Map type.
  3. Use multiple params of String named lastName and firstName.

Changing your approach:

  1. Abandon the idea of using the Input dialog to set/change values of params.
  2. Stick to the param type to be Map
  3. Employ some other way of expressing your data. But how? … there are many options for you, for example …
  • Have you tried Execution Profile (GlobalVariables)? it might be the same, though.
  • How about creating a JSON file <project dir>/Data Files/myParams.json. You edit it with any text editor, your test case reads the file, parse the text to find the value, pass the values as arguments to your custom keywords.
1 Like

Thank you for your reply kazurayam.

I tried everything that you described above.
And it works well if there are not too many parameters that I need to pass to the method.

A json file is also a good idea.
I did this by adding a json file in “Data Files” directory.
File with structure in this format:

{ 
  "TestName" : "test name", 
  "TestNumber" : 1,
  "TestDate" : "test date",
  "TestChecked" : false,   
  "TestDataArray" :   {
                       "data1" : "<data1>",
                       "data2" : "<data2>",
                       "data3" : null,
                       "data4" : false
                      },  
  "TestResultArray" : {
                       "result1" : "<result1>",
                       "result2" : "<result2>"
                      }
 }

Then in the “Test case” I created new variable “requestBody” in which passed the contents of the file.

String requestBody = new File ('Data Files / Test.json').text

And then I changed the values ​​of the fields I needed, for what I need using:

requestBody .replace ("<data1>", "some test value") 

But unfortunately this approach does not suit others.

Your text is not a valid JSON. Syntactically wrong.

I changed it as follows. This is a valid JSON. :

{
    "TestName" : "test name",
    "TestNumber" : 1,
    "TestDate" : "test date",
    "TestChecked" : false,
    "TestData" : {
        "data1" : "<data1>",
        "data2" : "<data2>",
        "data3" : null,
        "data4" : false
    },
    "TestResult" : {
        "result1" : "<result1>",
        "result2" : "<result2>"
    }
 }

Please check it and find differences for yourself.

I made a test case as follows:

import groovy.json.JsonSlurper

String text = new File('Data Files/Test.json').text
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(text)
println "object.TestData.data1 is '${object.TestData.data1}'"
object.TestData.data1 = "some test value"
println "object.TestData.data1 is '${object.TestData.data1}'"

When I ran it, I got the following messages in the console:

...
object.TestData.data1 is '<data1>'
...
object.TestData.data1 is 'some test value'

Hope this helps.

1 Like

You can pass the JSON object instance (the object variable in the above sample code) as a parameter to your keyword instead of Map, you know.

“some test value” above is a simple String.

You can give the value (in place of “some test value”) through the Input dialog, you know.

1 Like

At the moment things are as follows:

  • there is a test class “test.groovy” in which Map model defined
  • there is a CustomKeyword that accepts TestObject and a previously defined Map as a parameter

image

And when we add the created CustomKeyword to TestCase, we get the following:

The only way to solve this problem is to write the values ​​manually in the test case itself using Script mode looks like this:

And if after this switch to Manual mode, everything will be as it should

And the question is, can there be any way to transfer the Map in this form as a parameter at once?

Thank you kazurayam!
You right wrong brackets “[” and “]” in my example.
I fixed it.

I suppose
a) You have a property named testMap of Map type in the class test.test located at Keywords/test.groovy file. The testMap is initialized with a Map literal.
b) You have similar Map literal in the test case located at Test Cases/test.groovy as well
c) You want to keep the Map literal in a) and you want to transfer it (or silently copy it somehow) into b)

Am I right?

I do not think c) is possible.

What should you do?

Possibly, you should remove a) and forget about it. You should be satisfied with b). What do you want more?

I believe you understand it but let me note to make things clear for those who happened to read this thread …

In JSON syntax a JSON object should be in the format of {name:value}.
In Groovy syntax a Map literal should be in the format of [name:value].
What xio has in his/her code is NOT a JSON string.
What xio has in his/her code is a Map literal in Groovy syntax.