I need to get the number before ':' from a string using robotframework how can I do it?
${str}= Set Variable 7939:customer-Id:123a34ghas
I need to get only 7939 before ':' how can I do it in the robot framework?
${Id}= split string ${str}
log to console ${Id} #Should give only 7939
${str}= Set Variable 7939:customer-Id:123a34ghas
${Id}= split string ${str} :
log to console ${Id}[0] #prints 7939
Related
I'm writing a test case in robot framework. I'm getting the response in below json string:
{"responseTimeStamp":"1970-01-01T05:30:00",
"statusCode":"200",
"statusMsg":"200",
"_object":{"id":"TS82",
"name":"newgroup",
"desc":"ttesteste",
"parentGroups":[],
"childGroups":[],
"devices":null,
"mos":null,
"groupConfigRules" {
"version":null,
"ruleContents":null
},
"applications":null,"type":0
}
}
From that I want to take "_object" using:
${reqresstr} = ${response['_object']}
... but am getting the error "No keyword with name '=' found" error
If I try the following:
${reqresstr}= ${response['_object']}
... I'm getting the error "Keyword name cannot be empty." I tried removing the '=' but still get the same error.
How can I extract '_object' from that json string?
When using the "=" for variable assignment with the space-separated format, you must make sure you have no more than a single space before the "=". Your first example shows that you've got more than one space on either side of the "=". You must have only a single space before the = and two or more after, or robot will think the spaces are a separator between a keyword and argument.
For the "keyword must not be empty" error, the first cell after a variable name must be a keyword. Unlike traditional programming languages, you cannot directly assign a string to a variable.
To set a variable to a string you need to use the Set Variable keyword (or one of the variations such as Set Test Variable). For example:
${reqresstr}= Set variable ${response['_object']}
${reqresstr}= '${response["_object"]}'
wrap it inside quotes and two spaces after =
There is a syntax error in your command. Make sure there is a space between ${reqresstr} and =.
Using your example above:
${reqresstr} = ${response['_object']}
Situation.. I have two tags defined, then I try to output them to the console. What comes out seems to be similar to an array, but I'd like to remove the formatting and just have the actual words outputted.
Here's what I currently have:
[Tags] ready ver10
Log To Console \n#{TEST TAGS}
And the result is
['ready', 'ver10']
So, how would I chuck the [', the ', ' and the '], thus only retaining the words ready and ver10?
Note: I was getting [u'ready', u'ver10'] - but once I got some advice to make sure I was running Python3 RobotFramework - after uninstalling robotframework via pip, and now only having robotframework installed via pip3, the u has vanished. That's great!
There are several ways to do it. For example, you could use a loop, or you could convert the list to a string before calling log to console
Using a loop.
Since the data is a list, it's easy to iterate over the list:
FOR ${tag} IN #{Test Tags}
log to console ${tag}
END
Converting to a string
You can use the evaluate keyword to convert the list to a string of values separated by a newline. Note: you have to use two backslashes in the call to evaluate since both robot and python use the backslash as an escape character. So, the first backslash escapes the second so that python will see \n and convert it to a newline.
${tags}= evaluate "\\n".join($test_tags)
log to console \n${tags}
I tried
random stuff
${commentText} = Generate Random String 100 [LETTERS]
The user enters text to a text field id=textId ${commentText}
I have placed Library String at the suite level too but when ran the test, it says no keyword with name ${commentText} = Generate Random String 100 [LETTERS] found
it says no keyword with name ${commentText} = Generate Random String 100 [LETTERS] found
Take a close look at the error message. I'll add emphasis to make it more clear:
no keyword with name ${commentText} = Generate Random String 100 [LETTERS] found
In other words, it's not complaining about a keyword named Generate Random String, it's complaining about a keyword named ${commentText} = Generate Random String 100 [LETTERS]
This happens when you don't separate the individual parts of the statement with two or more spaces, causing robot to think the entire line is the name of the keyword.
The correct syntax is this:
${commentText}= Generate Random String 100 [LETTERS]
Notice that there are two spaces between ${commentText}, Generate Random String, 100, and [LETTERS]
Could you place exactly the same code that you use? I would say that you are not using proper delimiter, like 4 spaces:
${commentText} = Generate Random String 100 [LETTERS]
I have used ${SUITE NAME} tag. It is giving the Suite name along with the path as
"Robotframework.TestSuites.foldername.testsuitename". I need suite name only. How can I get that?
${the name}= Set Variable ${SUITE NAME}
# split the output on every . character
${the name}= Split String ${the name} separator=.
# get the last member of the split
${the name}= Set Variable #{the name}[-1]
Log To Console ${the name} # prints testsuitename in your example
P.S. It'll get nasty if you use dots in your suite names.
You can do that in-line as well.
${SUITE NAME.rsplit('.')[0]}
I use Run Keyword Unless comparing a variable and a string:
Run Keyword Unless '${text}' == 'HelloWorld' My Keyword ${text}
Sometimes ${text} consists of two lines separated by "\n" (eg. "One line\ntwo lines"). If so, the tests fails with an error:
Evaluating expression ''One line
two lines' == 'HelloWorld'' failed: SyntaxError: EOL while scanning string literal (<string>, line 1)
I solved the problem removing '\n' with String.Replace String as follows:
${one_line_text}= String.Replace String ${text} \n ${SPACE}
Run Keyword Unless '${one_line_text}' == 'HelloWorld' My Keyword ${text}
Is there a way to do it without explicit removing of EOL in a separate keyword?
What about ${text.replace("\n", " ")}?
You can use python's string literals - """ or ''' - and not change the string at all:
Run Keyword Unless '''${text}''' == 'HelloWorld' My Keyword ${text}
They are designed for pretty much this purpose - to hold values having newline characters, plus quotes.