robotframework
I used this code:
Page Should Contain Textfield xpath: //*[contains(text(), "${date}")]
But that searches literally (Dutch: letterlijk)
The actual content could be:
value-example1${date}value-example2
So there can be some text in front and some text after the variable...
I tried this but that didn't work
Page Should Contain Textfield xpath: //*[contains(text(), "*${date}"*)]
so * before the ${date} and * after the ${date} ..... (but that doesn't work)
The reason xpath: //*[contains(text(), "*${date}"*)] didn't work is because xpath will look for text with literal asterisks. You don't need the asterisks, contains takes care of that because it looks for your requested string anywhere in the element's text.
Here is an example using contains. First, save the following to /tmp/example.html:
<html><body>
<div><h1>Some text, January 1, 2020, and more text </h1>
</body></html>
Next, save this to example.robot:
*** Settings ***
Library SeleniumLibrary
Suite Setup open browser ${URL} ${BROWSER}
Suite Teardown Close all browsers
*** Variables ***
${URL} file:///tmp/example.html
${BROWSER} chrome
${date} January 1, 2020
*** Test Cases ***
Example
Page should contain element xpath://*[contains(text(), "${date}")]
When you run this test with robot, it should pass because the element contains the string stored in ${date}.
Related
Using Robot Framework's Playwright I am trying to get elements from a page, their href value, like href='http://www.mywebpage.com/customer/create/signup.html' and put them into a list.
From there I will ultimately try to compare if each element is similar to the substring of my base URL (which should be contained in each href link value), and then if each link returns a 200 response.
So far my code does get a list of elements(this is good), but each element looks something like this, "element=5cf0c691-41b1-4a3e-9347-d1a2bf0a48b8" instead of a URL ... I will want to query if each list element is similar to a base URL, so I want each element to be the href URL.
So, how can I evaluate what gets put into the list ${element_href} as the equivalent href URL, instead of something like "element=5cf0c691..." which I am currently getting?
Broken links test
${element_list}= browser.get elements xpath=//*[starts-with(#href, 'http://')] #same as above that finds 45 elements.
Log ${element_list}
Create Session testing ${BASE_URL}
FOR ${element_href} IN #{element_list} #get text gets same as without it.
${uri}= Remove String ${element_href} ${BASE_URL}
${contains_base_url}= Evaluate "${BASE_URL}" in "${element_href}"
${response}= Run Keyword If ${contains_base_url} Get Request testing ${uri}
Run Keyword If ${contains_base_url} Should Be Equal As Strings ${response.status_code} 200
END
I found answer to getting page href URL's into a list. Still struggling to compare each list item with the base URL, and then check if they navigate with a 200 response - but this progress effectively answers specifically what I asked above, how to gather elements on the page, namely the href attribute values, and into a list. The 4 commented lines before "END" are what I'm trying to get to work next, so far unsuccessfully; that said, here is the code:
Broken links test
${element_list}= get elements xpath=//a[#href] #ORIGINAL
Create Session testing ${BASE_URL}
#{element_attribute_list}= Create List
FOR ${element_href} IN #{element_list} #get text gets same as without it.
${element_attribute}= get attribute ${element_href} href
Append To List ${element_attribute_list} ${element_attribute}
# ${uri}= Remove String ${element_attribute_list} ${BASE_URL}
# ${contains_base_url}= Evaluate "${BASE_URL}" in "${element_attribute_list}"
# ${response}= Run Keyword If ${contains_base_url} Get Request testing ${uri}
# Run Keyword If ${contains_base_url} Should Be Equal As Strings ${response.status_code} 200
END
RESULTS HERE - they're not perfect, but code is appending to list each href, or so it appears:
KEYWORD Collections . Append To List ${element_attribute_list}, ${element_attribute}
Documentation:
Adds values to the end of list.
Start / End / Elapsed: 20220130 22:41:18.685 / 20220130 22:41:18.685 / 00:00:00.000
22:41:18.685 TRACE Arguments: [ ['http://dev.mysite.net/PAP-4414/',
'#',
'http://dev.mysite.net/PAP-4414/customer/account/create/',
'http://dev.mysite.net/PAP-4414/customer/account/login/',
'http://dev.mysite.net/PAP-4414/checkout/cart/',
'/courses.html',
'#',
'http://dev.mysite.net/PAP-4414/certification-courses/#swm'] | 'http://dev.mysite.net/PAP-4414/certification-courses/#dwc' ]
22:41:18.685 TRACE Return: None
Any additional thoughts are welcome! I appreciate the 10 Views, although no comments so far.
I'm able to select a random value from a list but I'm not able to enter that value in the search box. The thing is input text requires two arguments i.e the locator & the value. I'm stuck with the value part now
*** Variables ***
#{cin} U74999KA2016PTC096782 U22012CH1993PTC012939 U65999MH2016PTC287488
*** Test Cases ***
Search
input text ${SEARCH_BAR}
${value} Evaluate random.choice($cin) random
log to console \nvalue: ${value}
click element ${SEARCH_BUTTON}
You are trying to generate a random value after you try to input blank text into the box
Change your code to
*** Variables ***
#{cin} U74999KA2016PTC096782 U22012CH1993PTC012939 U65999MH2016PTC287488
*** Test Cases ***
Search
${value} = Evaluate random.choice($cin) random
input text ${SEARCH_BAR} ${value}
log to console \nvalue: ${value}
click element ${SEARCH_BUTTON}
I want to replace ${assertion_Keyword} by Screen Should Contain or Screen Should Not Contain so i want to put an Argument like a keyword
check_element_and_its_representation
[Arguments] ${assertion_Keyword} ${payload} ${on_click_payload}
${assertion_Keyword} ${payload}
SikuliLibrary.Click ${payload}
Screen Should Contain ${on_click_payload}
With this code i code a syntax error no keyword found :
How can this action be possible? is there other ways to do it ?
Thanks.
The Run Keyword command will help you with that. Below is an example inspired on your example code. I've commented out the Sikuli keywords, but kept them in place and added two keywords to mock those found in the Sikuly Library.
*** Test Cases ***
TC Screen Should Contain
Check Element And Its Representation Screen Should Contain payload on_click_payload
TC Screen Should Not Contain
Check Element And Its Representation Screen Should Not Contain payload on_click_payload
*** Keywords ***
Check Element And Its Representation
[Arguments] ${assertion_Keyword}=Screen Should Contain ${payload}=None ${on_click_payload}=None
Run Keyword ${assertion_Keyword} ${payload}
# SikuliLibrary.Click ${payload}
# Screen Should Contain ${on_click_payload}
Screen Should Contain
[Arguments] ${var}
Log Screen Should Contain
Screen Should Not Contain
[Arguments] ${var}
Log Screen Should Contain
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)
I am trying to create a unique string by embedding epoch time stamp after a static string as:
*** Keywords ***
Unique Title
[Return] ${title}
${secs}= Get Time epoch
${time}= Convert To String ${secs}
{title}= Catenate SEPRATOR=- Title ${time}
*** Test Cases ***
Test_Enter_Unique_Text
Open Browser #{urlUnderTest}
Input Text name=alertTitle Unique Title
Its not working I am not able to get the return value of Unique Title keyword, in the input box text is getting entered as "Unique Title".
Any suggestions will be really helpful.
You have to call the keyword and save what it returns, then use the saved value in the test case:
*** Test Cases ***
Test_Enter_Unique_Test
${title}= Unique Title
...
Input Text name=alertTitle ${title}