I have that kind of string :
"car model-8789 blue green"
"car model-879 blue green"
"car model-87897189 blue green"
And what I want is to extract model-xxxx where x are the numbers. I thought to split every string and to do a if and then using a regex but I wonder if there is a better solution to do that directly using RobotFramework ?
Thank you very much !
If you need to do string manipulation there usually are no easy ways around it. In this case, if the formatting is reliable, you can get by relatively easily. But if its not, then you'll either need some regexp or for-loops to find that data reliably.
You should only need to split the string once, no regexp needed. You'll need to use String Library and Collections Library.
*** Settings ***
Library BuiltIn
Library Collections
*** Test Cases ***
Get Model From Line
${STRING}= Set Variable car model-8789 blue green
#{LINE}= Split String ${STRING} ${SPACE} # = ['car', 'model-8789', 'blue', 'green']
FOR ${WORD} IN #{LINE}
IF 'model' in ${WORD}
${MODEL}= Set Variable ${WORD}
Exit For Loop
END
END
Log ${MODEL}
If possible you might want to look for a way to make the input in some structured format, like csv, to make this more robust.
You could try using regex.
*** Settings ***
Library String
Library Selenium2Library
*** Test Cases ***
TC1
${String} Set Variable car model-8789 blue green
${Match} Get Regexp Matches ${String} model-[0-9]*
Log ${Match}
Related
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.
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}
colleagues!
Sometimes I have a very long string in the code, e.g. more than 200 symbols in a string.
Could I carry some trail to the new line in the Robot Framework code?
Thank you for your attention!
You can use the built-in keyword Catenate and the line continuation syntax (the triple dots ...)
${long var}= Catenate This is a long string
... that will be concatenated
# you can combine multiple Catenate calls, for formatting clarity:
${long var}= Catenate ${long var}, and it continues on
... multiple lines.
By default the different portions will be concatenated with a space, you can change that with the SEPARATOR argument (the case matters).
I have written below code in robot framework. How to verify if that ${args} is in ${Mylist}
Create Article
[Arguments] ${args}
${MyList}= Create List 'Federal News' 'EY News'
Run Keyword If ${args} in ${MyList} Run keywords Click Element ${tab1_Xpath} AND Wait Until Page Contains Federal News
And I am calling Create Article ${arg} from another file. If I use the above script I am getting the error.
Any help would be appreciated
The first argument to Run keyword if must be a valid expression. Because you have more than one space around "in", robot thinks ${args} is the expression, and in and ${MyList} are additional arguments. TO fix this you need to have a single space on either side of "in".
The second problem is that you're creating a list of strings that includes quote marks. I'm guessing that you don't actually want that. If you want your list to contain the string Federal News you need to omit the quotes.
Finally, the expression needs to be valid python syntax after variable substitution. Since you're comparing strings, you need to either quote the arguments or use the special variable syntax $args. Using the special syntax is usually the best solution, since you don't h ave to worry about whether the variable itself contains quotes.
Finally, I would recommend using ${arg} rather than ${args} since it's a scalar rather than a list.
Assuming your data does not actually include the quote marks, here's how I would do it:
Create article
[Arguments] ${arg}
${MyList}= Create List Federal News EY News
Run Keyword If $arg in $MyList Run keywords
... Click Element ${tab1_Xpath}
... AND Wait Until Page Contains Federal News
With that, you can run the keyword like this:
Create article Federal News
Bryan has the perfect answer, here is a workaround to do the same task by using below keywords from collections library
1)Run Keyword And Return Status - Returns True or False for a keyword success or failure
2)List Should Contain Value - Check items in list
Create article
[Arguments] ${arg}
${MyList}= Create List Federal News EY News
${status}= Run Keyword And Return Status List Should Contain Value ${mylist} ${arg}
Run Keyword If ${status}==True Run keywords
... Click Element ${tab1_Xpath}
... AND Wait Until Page Contains Federal News
How Can we use value returned from one keyword as input argument to other keyword directly (without assigning return value to variable)
Currently I am maintaining all Web Elements or Variable names and corresponding xpath in an Excel Sheet. I get XPath using keyword read_xpath by passing web element/variable name as argument.
I store the xpath in separate variable then make use of it for other or Next line keyword. Since I need to use a variable for each XPath access, i am trying to find out, whether is there any way to directly use one keyword output as input to other keyword without assigning it to variable.
The main purpose of storing XPath in Excel sheet is to avoid multiple test cases change with single Element XPath change, making just single change on Excel Sheet should be sufficient.
For Example:
read_xpath reads values from a Excel sheet, Excel sheet has two columns, one variable name and second one is xpath. Function read_xpath(element) takes variable name as input and gives back xpath.
xlread.py file looks like xlread.py code Excel Sheet look like Excel sheet
sample.robot file looks as below
${login_user_textctrl}= read_xpath username_textctrl
clear element text ${login_user_textctrl}
Input text ${login_user_textctrl} admin
Now let us check how I used read_xpath keyword in my robot file
I called Keyword read_xpath with argument username_texctrl, which returns xpath for username text Control which is stored in variable ${login_user_textctrl}. Now Input Text ${login_user_textctrl} admin works fine.
For below code
clear element text read_xpath username_textctrl
I am getting, clear element text requires 1 argument but two arguments provided, is there any way i can use the the value returned from Keyword(read_xpath) as input to other keyword directly, without assigning it to any variable?
Thanks in advance.
Separating your locators from your robot code through an Object Repository is often done when the locators are reused a lot. An alternative implementation paradigm is to use the Page Object Model. An example of such an implementation can be found in the Robotframework-PageObjectLibrary.
In case you still prefer the Object Repository approach, then using the Selenium2Library keyword Add Location Strategy might be of interest to you. Below is a working example which uses a YAML file as it's OR to fetch the search input box and search button from Google.
ObjectRepo.yaml
OR:
searchbox: '//input[#id="lst-ib"]'
searchbutton: '//button[#id="_fZl"]'
Robot Script
*** Settings ***
Library Selenium2Library
Variables ObjectRepo.yaml
*** Test Cases ***
Yaml Object Repository
[Setup] Add Location Strategy yro Yaml Locator Strategy
Open Browser http://www.google.com Chrome
Input Text yro=searchbox Robot Framework
Click Element yro=searchbutton
Sleep 3s
[Teardown] Close All Browsers
*** Keywords ***
Yaml Locator Strategy
[Arguments] ${browser} ${criteria} ${tag} ${constraints}
${xpath}= Set Variable ${OR.${criteria}}
${retVal}= Get Webelement xpath=${xpath}
[Return] ${retVal}
As you can see the abstraction through a custom locator keyword allows for clearner code and not require you to fetch into a variable for later reuse.
You can use Evaluate to call python functions directly. Example:
${my element to clear}= Evaluate read_xpath("username_textctrl") xlread
See the documentation here
(Note: Example is untested and you should check if xlread is in PYTHONPATH, or found at test run time)