in Katalon is a very nice way to parameterize the selectors for GUI elements, so that you can easily select very similar elements with the help arguments. I want to do something similar like that in Robot Framework.
EDIT: Better example, that is more easy to understand:
We have several GUI elements we have to interact with when testing. As the elements' selectors are very similar, we want to parameterize specific parts of it. In this case, we want to parametrize the $(selector) part of the selector:
*** Variables ***
$(overview.element} //div[contains(#class, $(selector)')]
We want to be able to do that, so that we can avoid something like that
*** Variables ***
$(overview.home} //div[contains(#class, home')]
$(overview.settings} //div[contains(#class, settings')]
$(overview.overview} //div[contains(#class, overview')]
We want to give that parameter within the test cases. Means: We can specify which element we want to select. Something like that:
[Arguments] ${selector}
Click $(overview.element)(${selector})
Is that possible? And if yes: How?
You can use the built-in keyword Replace variables to perform the substitution before using the locator. For this to work you'll have to escape the variable reference when defining ${overview.element}
Example:
*** Variables ***
${overview.element} //div[contains(#class, \${selector}')]
*** Keywords ***
Example keyword
[Arguments] ${selector}
${locator}= Replace variables ${overview.element}
log locator is ${locator}
*** Test cases ***
Example
example keyword settings
When you run the above, the log should show this:
Related
I've a situation in my app where I need to click on file name from a /div table which will save the file name internally and gets displayed in a /div grid. Every time I run the automation, I need to click on a new file.
I don't have any problem in clicking the file to be saved.
But after the click, the app stores the file in a /div grid and I want to select the file that I added.
For this I used this approach:
I am storing the file name in a global variable ${g_ExtractedFileName}
For eg: XX_YY_Response_IdNum_48015_2020-07-27T12-18-44.334442.txt
My complete Xpath to the file is something like this
xpath://div[#role='row']//span[contains(text(),'XX_YY_Response_IdNum_48015_2020-07-27T12-18-44.334442.txt')]
For this I have the below:-
${attachedFileXpath1}= set variable xpath://div[#role='row']//span[contains(text(),'
${attachedFileXpath2}= set variable ${g_ExtractedFileName}
${attachedFileXpath3}= set variable ')]'
${attachedFileXpath}= set variable ${attachedFileXpath1}${attachedFileXpath2}${attachedFileXpath3}
click element ${attachedFileXpath}
This throws an error
SyntaxError: Failed to execute 'evaluate' on 'Document': The string
'//div[#role='row']//span[contains(text(),'XX_YY_Response_IdNum_48015_2020-07-27T12-18-44.334442.txt
')]'' is not a valid XPath expression.
When I don't use the variables and simply use (hard coded value for file name)
click element xpath://div[#role='row']//span[contains(text(),'XX_YY_Response_IdNum_48015_2020-07-27T12-18-44.334442.txt')]
It works correctly.
I also tried this but same error
${attachedFileXpath}= catenate ${attachedFileXpath1}${attachedFileXpath2}${attachedFileXpath3}
I am not sure when the 3 variables are concatenated, it cuts off the variable value to a new line causing this problem.
Any help is much appreciated.
The simplest approach is to simply use the replace functionality that is already present in Robot Framework variables. The below example will result in an Element not found error, but that is expected.
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${g_ExtractedFileName} XX_YY_Response_IdNum_48015_2020-07-27T12-18-44.334442.txt
*** Test Cases ***
Use Global String in Locator
Open Browser https://google.com chrome
click element xpath://div[#role='row']//span[contains(text(),'${g_ExtractedFileName}')]
[Teardown] Close All Browsers
Simply create keyword which accept the file name as argument and returns complete valid xpath expression. It will be re-usable and elegant solution -
*** Test case ***
Select file saved in div grid
${attached_file_xpath}= GetAttachedFileXpath ${File_Name}
Click Element ${attached_file_xpath}
*** Keywords ***
GetAttachedFileXpath [Arguments] ${File_Name}
return from keyword xpath://div[#role='row']//span[contains(text(),'${File_Name}')]
As part of a Robot Framework test I would like to access multiple objects from my page with the same keyword. The xpath of the objects looks like this:
//div[#class='col-sm-4 ng-scope']//h1[#class='ng-binding'][contains(text(),'Bot1')]
//div[#class='col-sm-4 ng-scope']//h1[#class='ng-binding'][contains(text(),'Bot2')]
//div[#class='col-sm-4 ng-scope']//h1[#class='ng-binding'][contains(text(),'Botx')]
I've tried to set a list variable with the name of my test objects (elements)
*** Variables ***
#{TESTBOTS} = Bot1 Bot2 Botx
*** Keywords ***
Delete Bots
go to ${LANDINGURL}
Sleep 3s
Click Element //div[#class='col-sm-4 ng-scope']//h1[#class='ng-binding'][contains(text(), #{TESTBOTS})]
However, I get the following error:
FAIL Element with locator '//div[#class='col-sm-2 ng-scope']//h1[#class='ng-binding'][contains(text(),'${TESTBOTS')]' not found.
I'd really appreciate if you could point me to the right direction. Thanks!
The error message still does not seem to fit the snippet, I believe.
But anyway: with #{TESTBOTS} you unwrap the complete list. I would have expect an error like No element found with text 'Bot1 Bot2 Botx'
I think this should work iterating the list of testbots:
FOR ${testbot} IN #{TESTBOTS}
Click Element //div[#class='col-sm-4 ng-scope']//h1[#class='ng-binding'][contains(text(), ${testbot})]
END
Library REST ${base_url}
*** Keywords ***
Get Requests
GET ${rest_of_the_url}
Output response body
*** Test Cases ***
Do some searching
Get Requests
*** Variables ***
${base_url} https://business.com
${rest_of_the_url} /api/${department}/${person_name}
How can I assign values to ${department} and ${person_name}? I don't want to set those inside Variables because then I cannot write multiple scenarios inside one .robot file. Is it possible to do the assignment as arguments?
i do not think there is a way to pass arguments within the variables,
The below section is straight from the documentation of Robotframework,
where you can create Variables inside variables
Variables inside variables
Variables are allowed also inside variables, and when this syntax is used, variables are resolved from the inside out. For example, if you have a variable ${var${x}}, then ${x} is resolved first. If it has the value name, the final value is then the value of the variable ${varname}. There can be several nested variables, but resolving the outermost fails, if any of them does not exist.
In the example below, Do X gets the value ${JOHN HOME} or ${JANE HOME}, depending on if Get Name returns john or jane. If it returns something else, resolving ${${name} HOME} fails.
*** Variables ***
${JOHN HOME} /home/john
${JANE HOME} /home/jane
*** Test Cases ***
Example
${name} = Get Name
Do X ${${name} HOME}
E.g.,
${person_name}= Set Variable Matt
${department}= Set Variable R&D
# then your code continues
${rest_of_the_url} /api/${department}/${person_name}
Set Variable documentation.
Try to see this using the Set Test / Suite / Global Variable keywords here:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
Use "Set Suite Variable" keyword then enter the variables $ {person_name} e
$ {department} inside * Variables * then you should read the value inside the test.
Is there any possible to change testcase name with variable like below?
(I don't want to change name from python side)
*** Variables ***
${country} US
*** Test Cases ***
test_${country}
As far as I know, it isn't possible to use a variable inside a test case name. It follows the same logic as normal Python functions, so normally, it isn't possible.
Instead, you can use the variable in the setup or in the test case directly to modify it's behaviour.
If you want to generate test cases based on a variable, you can write a (python) script that can generate the needed file/test cases with the corresponding values. Or, even better, use an Model-Based Testing tool to produce them.
Yes, you can. The way you have shown it should work. Are you facing any issue with that? If yes, pls provide the detailed error.
Yes this is supported.
example:-
*** Test Cases ***
Test title ${name}
[Tags] DEBUG
Log Welcome ${name}
output:-
robot --variable name:sample eg.robot
I would like use KW "Set test documentation" with multiline with RobotFW
The return to the line (\n) does not work with this KW
Someone have solution?
Setting multiline documentation is tricky because robot has some odd rules about its documentation. From the user guide section titled Documentation Formatting:
Starting from Robot Framework 2.7.2, all regular text in the formatted HTML documentation is represented as paragraphs. In practice, lines separated by a single newline will be combined in a paragraph regardless whether the newline is added manually or automatically. Multiple paragraphs can be separated with an empty line (i.e. two newlines) and also tables, lists, and other specially formatted blocks discussed in subsequent sections end a paragraph.
In a nutshell, that means that each line needs to end with two newlines.
Example:
*** Variables ***
${var1} this is var1
${var2} this is var2
*** Test Cases ***
Example of setting multiline test documentation
set test documentation
... var1: ${var1}\n\nvar2: ${var2}
The above will appear in log.html like this:
If your goal is to document the values of variables, robot also supports a simple markup for creating tables. The following example shows how to create a table. In this example I use the ability to append to the documentation rather than replace it:
*** Variables ***
${var1} this is var1
${var2} this is var2
*** Test Cases ***
Example 2: documentation with embedded table
set test documentation
... | var1 | ${var1} | \n
set test documentation
... | var2 | ${var2} | \n append=True
The above will appear in log.html like this:
I am not sure if we can do that directly, i found a workaround after taking reference from Bryan's reply on this thread.
Testcase level variables in [Documentation] for robot framework
Here multi lines are first mentioned in [Documentation] section and then this section can be used by Set test documentation kw.
*** Variables***
${SystemUnderTest} Staging
*** Test cases***
Device Test
Set Test Variable ${device} iPhone
[Documentation] Device is:
... System is: ${SystemUnderTest}
Substitute vars in documentation
*** Keywords ***
Substitute vars in documentation
${doc}= replace variables ${test documentation} # This will substitute the variables in documentation with their values , ${test documentation} is an inbuilt keyword which actually parse the content of [Documentation]
set test documentation ${doc} append=True #now the set test docuemntation will take the multi line input from documentation section
refer below link for further details http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Test%20Documentation