首页 > 解决方案 > 单独文件中的机器人框架变量属性不存储要在其他地方使用的属性

问题描述

变量属性未传递到其他文件。

我将元素定位器的变量存储在一个文件中,并且我在另一个文件中完成了断言,该文件到目前为止运行良好,并且很好地分离了事物。我正在做一个断言来检查一个元素是否存在并且它的属性(值)不为空。如果我将其写在一页上,则效果很好。这使用 selenium 库关键字should not be equalGet 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那么它就可以工作,所以我知道我正在正确地拉入其他资源。我有一种感觉,我可能需要以某种方式将属性存储在另一个变量中并实际断言。任何想法都会很棒。提前致谢。

标签: seleniumtestingrobotframework

解决方案


假设您在其他问题中给出了这样的 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.robotassertion.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 AttributeShould 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}
    

输出 在此处输入图像描述


推荐阅读