RobotFrameWork: Should be equal as Strings to use regular expressions - robotframework

So I have two possible valid values for Should Be Equal As Strings.
Is there a way to do something like
Should Be Equal As Strings
... Create create|update
Doesn't necessarily need to be using that specific keyword but I want to be able to accept one of the other

You can use Should match regexp
Here's an example:
*** Variables ***
${action1} create
${action2} update
${action3} delete
*** Test Cases ***
Example
Should match regexp ${action1} create|update
Should match regexp ${action2} create|update
Run keyword and expect error 'delete' does not match 'create|update'
... Should match regexp ${action3} create|update

Related

How to pass only required variable value and get other values as default in robot framework

Here I want to pass only specific variable value and should get other values as default but I am not getting that...
Ex: I want to pass only word2 variable value and want to get word1 value as default Like "Hello See"
*** Keywords ***
Check order
[Arguments] ${word1}=Hello ${word2}=World
log to console word1 is : ${word1}
log to console word2 is : ${word2}
*** Test Cases ***
Test case 1
Check order ${word2}=See
Test case 1 | FAIL |
Variable '${word2}' not found.
The way to do this is to use only the variable name to the left of the equals:
Check order word2=see
This is from the robot framework user guide, in a section titled Named arguments
When the named argument syntax is used with user keywords, the argument names must be given without the ${} decoration. For example, user keyword with arguments ${arg1}=first, ${arg2}=second must be used like arg2=override.

Robot Framework : BuiltIn library : Should Match : How to pass a pattern parameter correctly?

Given the following code:
*** Test Cases ***
Use "Should Match"
[Documentation] Should Match string, pattern, msg=None, values=True, ignore_case=False
... Fails if the given string does not match the given pattern.
... Pattern matching is similar as matching files in a shell with *, ? and [chars] acting as
... wildcards. See the Glob patterns section for more information.
Should Match string=Can find me here pattern=me msg='The overwriting error' # fails unexpectedly
Should Match string='Will match with star' pattern=*
Should Match string='Will match this' pattern=[atx]his # fails unexpectedly
Should Match string='Will match with keyword' pattern=?eyword # fails unexpectedly
The goal is to make all the statements in the test case pass. Currently, the first statement fails with the error:
'The overwriting error': 'Can find me here' does not match 'me'
When using Should Match the pattern needs to match the whole string, not just part of the string. If you want the first pattern to pass, you need to change it to *me*. The first star will match everything up to the word "me", and the second star will match everything after.
The same is true for the other patterns. If you're looking for a pattern inside a larger string you need to add * on either side of the pattern to match all of the other characters.
*** Test Cases ***
Use "Should Match"
Should Match Can find me here pattern=*me*
Should Match 'Will match with star' pattern=*
Should Match 'Will match this' pattern=*[atx]his*
Should Match 'Will match with keyword' pattern=*?eyword*

How to find open and closing braces present in a string (a list converted into string)?

My requirement is to check whether the input is list or not using robot framework.
I tried using type(${temp}).name with evaluate function, which works in case of list and fails for string type.
Below is the error message -
Evaluating expression 'type(testdata).name' failed: SyntaxError: invalid token (, line 1)
Tried to use regex, but no luck.:(
Code :-
testRegEx
${match} Run Keyword Should Match Regexp ["swerwv","sfsdfdsf","edsfdf"] \[\s\S\]
Log To Console ${match}
output:-
FAIL : '["swerwv","sfsdfdsf","edsfdf"]' does not match '[\s\S]'
I'm new to robotframework. Any help would be appreciated.
My requirement is to check whether the input is list or not using robot framework.
You can use robot's special syntax for evaluate and the various keywords which accept expressions, where you omit the brackets in the variable reference to pass the actual variable to the expression (versus passing the value of the variable).
Example:
*** Variables ***
#{a_list} one two three
*** Test Cases ***
Test that variable is a list
run keyword unless type(a_list) == list
... Fail not a list
This feature is mentioned in the Evaluating expressions section of the built-in library.

RobotFrameWork: how can I use a function in a function?

So there's a nice keyword in RFW which is:
Table Cell Should Contain,
But now It should check the tabletext against a regexp. but it doesn't seem to work that way, because it says,
text not found
*** Variables
${pattern2} ^[0-9]{1}[0-9]{1}
Table Cell Should Contain xpath=//div[#id='components_block']/table 2 6 ${pattern2}
this doesn't work: either i guess:
Table Cell Should Contain xpath=//div[#id='components_block']/table 2 6 regexp:^[0-9]{1}[0-9]{1}
Regretfully, no, in Robot Framework you cannot chain keywords - to use the output of one directly as an input for another. You have to have intermediate/temp variables for that.
For the case here, you want to see does a table cell has text, matching a specific regular expression. As the keyword Table Cell Should Contain does not support regexp in the looked-for argument, but only normal strings, you can't use it.
Break down this verification in 2 steps - first get the text in the cell (in a temp variable), and then see does it match the regular expression with the Should Match Regexp keyword:
*** Variables ***
${pattern2} ^[0-9]{1}[0-9]{1}
*** Testcases ***
A case
${temp variable}= Get Text xpath=locator_that_will_return_the_specific_cell
# alternatively, you can use this keyword - if the table is an actual <table> element in the html
# ${temp variable}= Get Text xpath=locator_that_will_return_the_table 2 5
# now having the text in the cell, see does it match the regexp
Should Match Regexp ${temp variable} ${pattern2}
As to what are the values of "locator_that_will_return_the_specific_cell" and "locator_that_will_return_the_table" - I can't tell you; nor anyone else, without a sample of the HTML.
I found I direct match/hit to the table location like so: xptah=//tr[1]/td[4]
then I could do this:
${gettext} Get Text xpath=//tr[1]/td[4]
Should Match Regexp ${gettext} ^[0-9]{1}[.]{1}[0-9]{1}[0-9]{1}

robotframework: what is the easiest way to verify a value is bigger than 10

so there is a website: https://www.guruwatch.nl/Aandelen/Default.aspx
I click the element 'koop' and then I want to verify that the value in the top is bigger then 10
id="ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy"
what is the fastest way to do this?
i used
Element Text Should Be ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy 24
but that is for the exact match, i just want to verify the integer is bigger then 10.
there is also an
Should Be Equal As Integers first second
builtin
but not a
Should Be Bigger As Integer
Should be Smaller As Integer
p.s. why are those not builtin? is it so strange to use this?
There are plenty of ways of checking if one value is larger than another with the * If keywords that can be found in the BuiltIn Library. Below is an example of how you can craft the larger than keyword:
*** Test Cases ***
Test Positive
${value} Set Variable 24
Should Be Larger Than ${value} 1
Test Negative
${value} Set Variable 24
Run Keyword And Expect Error * Should Be Larger Than ${value} 100
*** Keywords ***
Should Be Larger Than
[Arguments] ${value_1} ${value_2}
Run Keyword If ${value_1} <= ${value_2}
... Fail The value ${value_1} is not larger than ${value_2}
In order to do this, you need to find the xpath locator first. Since the xpath is quite long so, I just assign it to a variable as below.
${top_position_xpath}= set variable //span[#id='ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy'
${get_number}= GET TEXT xpath=${top_position_xpath} ## --> This is xpath locator for that top column
${check}= SHOULD BE TRUE ${get_number} > 10 # --> The current test will fail if the result is false..
Okay, so I summarize the steps I use above here:
Get the xpath locator for the specific column that you want to verify.
Use the GET TEXT keyword to get the exact output (number) of that column.
Verify using the SHOULD BE TRUE keyword. This keyword is sufficient to verify the condition.
But just to highlight, that if you use the SHOULD BE TRUE keyword as above, the test will fail immediately, so a good approach is to use with keyword RUN AND RETURN STATUS and assign a variable to tell either the condition is true or false, so that you can proceed with your next code or statements..
${result}= RUN KEYWORD AND RETURN STATUS SHOULD BE TRUE ${get_number} > 10 #
Before I read the messages I solved it myself this way:
Should Match Regexp ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy [0-9]{1}[0-9]{1}

Resources