将XPath集中在资源文件中,如何传递机械手文件中的争论? [英] Centralizing XPath in resource file, how to pass arguement from robot file?
问题描述
我将所有特定的XPath字符串集中到一个资源文件中,并将变量导入该资源文件中的测试套件(机器人框架)中.这样,就可以将它们保存在一个地方,并且我可以使用使机器人文件可读的变量名.那是一种好的做法吗?
I am centralizing all particular XPath strings to a resource file and importing the variables in that resource file in my test suite (robot framework). This way, they can be maintained in one place and I can use variable names that make the robot file readable. Is that something that is good practice?
有时我想将参数传递给变量,以使其更动态.但是,变量的值包含和XPath,XPath有时具有//div [path ... etc] [text()='MyString'].
Sometimes I want to pass an argument to the variable, to make it more dynamic. However, the value of the variable contains and XPath, which sometimes has //div[path...etc][text()='MyString'].
问题:在机械手文件中,如何将参数('MyString')传递给使用变量的Click Element方法?
Question: In the robot file, how to pass an argument ('MyString') to the Click Element method that is using a variable?
推荐答案
将UI对象技术参考与测试逻辑分离无疑是一个好习惯.通常,此模式称为对象存储,但也使用其他名称.
It is certainly a good practice to separate your UI objects technical reference from your test logic. Typically this pattern is called an object store, but other names are used as well.
对于分离方法,我建议使用资源文件上的YAML变量文件静态值,并使用命令行参数robot --variablefile MyVariables.yaml MyRobotFile.robot
包含它们,而不是在测试脚本中包含资源文件.这样做还有一个好处,就是如果由于不同的软件版本而要关闭对象存储库,则不需要更改测试脚本.
As for the method of separation, I'd recommend using a YAML variable file over a resource file for static values and including them using the command line argument robot --variablefile MyVariables.yaml MyRobotFile.robot
over including a resource file in your test script. This has the added advantage that if you want to switch out your object store because of a different software release then this doesn't require a test script change.
如果您的变量内容根据启动机器人时已知的某个值而变化,那么Python变量类是一个很好的方法.这是 Python函数或 Python类需要一个参数,您可以使用Python进入数据库,文件,或使用内部逻辑来确定需要返回哪些变量以及它们应保持什么值.
In the event that your variable content changes depending upon some value known when starting robot, then Python Variable Class is a good approach. This is a Python function or Python class that takes an argument and you can use Python to go to a database, file, or use internal logic to determine which variables need to be returned and what value they should hold.
至于在不以大量特定专业关键字结尾的情况下将可变片段添加到xpath中,我使用自定义定位器策略功能.这使我可以在测试逻辑本身中使用普通关键字而无需任何其他关键字.
As for adding variable pieces to an xpath without ending up with a lot of specific specialised keywords, I use Custom Locator Strategy functionality from the SeleniumLibrary library. This allows me to use the normal keywords without any additional keywords in the test logic itself.
在下面的示例中,将创建一个自定义定位器abc=
,并且可以将其用于任何标准SeleniumLibrary关键字而不是xpath=
.在这种情况下,我将Dictionary作为Locator对象存储区来保存ID并使用唯一名称引用它们.请注意,abc=
是从${criteria}
参数中的值中除去的.
In the below example a custom locator abc=
is created and can be used instead of xpath=
for any standard SeleniumLibrary keyword. In this case I use a Dictionary as the Locator object store to hold the ID's and refer to them using a unique name. Note that the abc=
is stripped from the value in the ${criteria}
argument.
*** Variables ***
&{locators}
... myCustomId1=//*[@id='12234']
... myCustomId2=//*[@id='23455']
*** Test Cases ***
Test Case
Add Location Strategy abc Custom Locator Strategy
Page Should Contain Element abc=myCustomId1
*** Keywords ***
Custom Locator Strategy
[Arguments] ${browser} ${criteria} ${tag} ${constraints}
${WebElement}= Get Webelement xpath=${locators['${criteria}']}
[Return] ${WebElement}
这篇关于将XPath集中在资源文件中,如何传递机械手文件中的争论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!