如何在 XPATH 中使用变量,应该在关键字或测试中设置哪个值? [英] How to use variable, which value should be set in keyword or test, in XPATH?

查看:20
本文介绍了如何在 XPATH 中使用变量,应该在关键字或测试中设置哪个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据元素包含的值单击元素..但是我想在测试运行或关键字定义中设置这个值(我猜最好的选择是在测试中)我该怎么做?

I need to click on element based on what value it contains..but I want to set this value in test run or keyword definition (best option is in the test I guess) How should I do it?

包含 xpath 的变量应该如下所示:

the variable containing xpath should look like that:

${DROPDOWN ITEMS}    xpath=//*[contains(@class,'listitem-element')]/span[contains(text(),'${second_number}')]

当我用像002"这样的实际数字替换变量时,这个定位器可以工作,但我想让它更通用..

This locator works when I replace the variable with actual number like '002', but I want to have it more general..

在关键字定义中,我像这样使用它:

In keyword definition I use it like:

Choose Value From Dropdown
     focus    ${DROPDOWN ITEMS}
     click element   ${DROPDOWN ITEMS}

在测试中我只调用关键字

and in test I just call the keyword

我的问题是在哪里以及如何设置 xpath 中使用的 ${second_number} 变量的变量值?PS:xpath 定义、关键字和测试分别在单独的文件中谢谢!

my question is where and how to set the variable value of ${second_number} variable used in xpath? PS:the xpath definition, keyword and test are each in separate files thank you!

推荐答案

我在我的 SUT 中使用了类似的方法,因为它适用于相当复杂的对象,包括在测试执行期间预先创建的和动态生成的 - 以及它们的主要用户可识别属性是显示名称.这是我的流程的简化版本,它基于字符串替换.

I use similar approach in my SUT, as it works with fairly complex objects, both precreated and dynamically generated during the tests executions - and their main user-identifiable attribute is the displayed name. Here's simplified version of my flow, and it's based around string substitution.

从变量文件开始 - 一个简单的 selenium 定位器集合,定位器的值有一个特殊"字符串,稍后将被替换:

Starting off from the variables file - a simple collection of selenium locators, the value of the locator has a "special" string, which will later be substituted:

*** VARIABLES ***
    ${DROPDOWN ITEMS}    xpath=//*[contains(@class,'listitem-element')]/span[contains(text(),'SELENIUM_PLACEHOLDER_CHANGE_ME')]

然后,在关键字文件中有用于返回正确定位器的私有关键字,例如:

Then, in the keyword files there are private keywords for returning the proper locators, for example for this one:

*** KEYWORDS ***
    _Return Selenium Locator For The Dropdown Item Named
        [Documentation]    Verifies the desired dropdown item is valid, ando returns its locator (not Webelements!!)
        [Arguments]    ${name}

        # change the placeholder with the actual UI name
        ${loc}=    Replace String  ${DROPDOWN ITEMS}    SELENIUM_PLACEHOLDER_CHANGE_ME    ${name}

        # why? Rationale explained below
        Element Should Be Visible    ${loc}    message=The dropdown does not have an item called ${name}

        [Return]    ${loc}

为什么要进行可见性检查?简单 - 如果当前 SUT 中没有这样的对象,则尽早失败,并具有统一的错误消息,与元素的进一步使用方式(单击、检查是否存在、属性检索等)无关.

Why the visibility check? Simple - to fail as early as possible if there's no such object currently in the SUT, and to have uniform error message, independent of how is the element further used (clicked on, checked for presence, attribute retrieval, etc.)

然后,用于对元素执行操作的后续用户关键字使用前一个:

Then, a follow up user keyword for performing actions on the element uses the previous one:

    # the user keywords
    Choose Value From Dropdown
        [Documentation]    It does what it does :)
        [Arguments]    ${the value}

        ${loc}=    _Return Selenium Locator For The Dropdown Item Named    ${the value}

        # as you can see, no checks is the element real - that'she offloaded to the helper keyword ^
        Focus Element    ${loc}
        Click Element    ${loc}

最后,测试用例使用关键字来处理您认为需要的任何数据:

Finally, the test cases use the keyword to work with any data you deem neaded:

*** TESTCASE ***
The dropdown should do X
    [Documentation]    Steps: 1, 2, 3, etc

    # do the normal steps you'do do
    Choose Value From Dropdown    my current value

这种方法也适用于否定测试——例如,要检查值不存在,测试用例将包含:

This approach applies fairly well for negative tests also - for example, to check a value is not present, a test case would contain:

    Run Keyword And Expect Error    The dropdown does not have an item called no_such_element    Choose Value From Dropdown    no_such_element

因此,我们都使用 selenium 检查元素的缺失,并使测试用例接近现实生活中的表达式 - 对应该发生的情况的描述,没有特殊的语法和 SE 关键字.

Thus we're both using selenium checks for the absence of the element, and keeping the test case close to real-life expression - a description of what should happen, with no special syntax and SE keywords.

请原谅任何错别字和轻微的语法遗漏 - 在手机上打字并不容易,下次我会三思而后行 :D

这篇关于如何在 XPATH 中使用变量,应该在关键字或测试中设置哪个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆