selenium - 单独文件中的机器人框架变量属性不存储要在其他地方使用的属性
问题描述
变量属性未传递到其他文件。
我将元素定位器的变量存储在一个文件中,并且我在另一个文件中完成了断言,该文件到目前为止运行良好,并且很好地分离了事物。我正在做一个断言来检查一个元素是否存在并且它的属性(值)不为空。如果我将其写在一页上,则效果很好。这使用 selenium 库关键字should not be equal
,Get Element Attribute
只是要注意。
${EXAMPLE} get element attribute class=test test-data
should not be equal ${EXAMPLE} ${EMPTY}
但是如果我将它们分成不同的文件。所以一个 locators.robot 文件:
#Locater File
${EXAMPLE} get element attribute class=test[test-data]
还有一个 Assertions.robot:
#Assertion File
should not be equal ${EXAMPLE} ${EMPTY}
它停止工作。如果我使用 selenium 库断言,page should contain element
那么它就可以工作,所以我知道我正在正确地拉入其他资源。我有一种感觉,我可能需要以某种方式将属性存储在另一个变量中并实际断言。任何想法都会很棒。提前致谢。
解决方案
假设您在其他问题中给出了这样的 html 代码 -
<div id="top-list">
<div data-version="12345" data-list="1" data-point="10">
方式 1 -不太推荐- 这就是我的assertion.robot
样子 -
*Settings
Library SeleniumLibrary
Resource Locator.robot
*Test Cases
Test attributes Locator
Open Browser file:///C:Desktop/testxpath.html chrome
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
定位器在locator.robot
文件中。我没有Get Element Attribute
在定位器中调用关键字,因为这样做将没有直接执行它并在测试用例中引用它的返回值的链接......所以只需将定位器保留在定位器文件中即可。当我Resource Locator.robot
在assertion.robot
文件中进行操作时,可以访问此定位器。如您所见,Get Element Attribute
元素采用元素的第一个参数定位器,第二个参数只不过是您需要的值的属性名称。此关键字返回作为第二个参数提供的属性值。-
*Variables
${Datalist_locator_with_all_attribt} xpath://div[@data-version='12345' and @data-list='1' and @data-point='10']
${locator_with_single_attribute} xpath://div[@data-version='12345']
输出
方式 2 -更推荐- 将Get Element Attribute
和Should Not Be Equal
关键字包含在一个关键字中。并将其转储到另一个关键字文件中或在文件本身中创建*keywords
部分。locator.robot
这样做您的assertion.robot
文件将如下所示 -
*Test Cases
Test attributes Locator
Open Browser file:///C:/Desktop/testxpath.html chrome
Attribute values should not be empty
locator.robot
看起来像这样。你可以让它更通用 -
*** Variables
${Datalist_locator_with_all_attribt} xpath://div[@data-version='12345' and @data-list='1' and @data-point='10']
${locator_with_single_attribute} xpath://div[@data-version='12345']
*** Keywords
Attribute values should not be empty
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
推荐阅读
- javascript - 可搜索输入的 SAPUI5 命名模型绑定
- javascript - 如何转换数组中已经在javascript中的嵌套对象中的对象
- openlayers - 如何在 Chrome 中获取示例的源映射
- php - 在 PHP 中转换包含表情符号的请求输入并保存在 MySQL 中
- bash - 如何使用 sed 正则表达式模式匹配
- java - 从具有不同布局的 Activity 访问布局的 TextView
- php - rsa 签名验证使用 phpseclib vs openssl
- python - 获取html动态内容python3
- android - 无法将所选项目从微调器保存到数据库
- javascript - 使用变量Javascript动态更改gridTemplateColumns的重复值的任何方法