Robot Framework - 根据输入更改变量 [英] Robot Framework - Change variables depending on input

查看:152
本文介绍了Robot Framework - 根据输入更改变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 css 选择器

I have the following css selector

${Table_Row}      css=.tr > td:nth-child(2)

这个选择器会让我得到表中的第一个实例.问题是该表可能包含数百个实例,而我不想拥有数百个变量.如何使变量更具动态性,以便我可以传递另一个变量来确定nth-child"计数,而不将其设为关键字?

This selector will get me the first instance in a table. The problem is that the table may contain hundreds of instances and I don't want to have hundreds of variables. How can I make the variable more dynamic, that I can pass another variable to determine the 'nth-child' count, without making it a keyword?

这是我的意思的python示例:

Here is a python example of what I mean:

table_row = ".tr > td:nth-child(%s)"

然后如果我调用这个变量

Then if I call this variable

table_row % 5

结果是

.tr > td:nth-child(5)

推荐答案

如果这是经常重复的事情,并且您想集中逻辑而根本不必处理变量,那么 自定义定位器策略

If this is something that is repeated often, and you want to centralize the logic and not have to deal with variables at all, then a Custom Locator Strategy,

受您问题启发的示例:

*** Test Cases ***
Test Case
    Add Location Strategy   table   Custom Locator Strategy
    Page Should Contain Element table=3

*** Keywords ***
Custom Locator Strategy 
    [Arguments]    ${browser}    ${criteria}    ${tag}    ${constraints}
    ${element}=    Get Webelement   css=.tr > td:nth-child(${criteria}) 
    [Return]    ${element}

这将适用于所有将定位器作为输入参数的关键字.自定义定位器策略只需要返回一个 Web 元素.

This will then work for all keywords that takes a locator as an input argument. The Custom Locator Strategy only needs to return an Web element.

在我看来满足内联标准但在我看来不是更易读的另一种选择(留给读者)是使用字符串对象函数.它们在高级变量语法部分及其周围进行了描述.机器人框架指南:

An alternative that in my view fulfills the inline criteria, but in my opinion is not more readable (leave that to the reader) is to use the string object functions. They are described in and around the Advanced Variable Syntax section of the Robot Framework Guide:

*** Variables ***
${locator_template}    css=.tr > td:nth-child(%) 

*** Test Cases ***
TC
    Log    Locator Template: "${locator_template}"
    ${locator}    Set Variable    ${locator_template.replace("%", "9")}

    Log    Locator Variable: "${locator}"
    Log    Inline Variable: "${locator_template.replace("%", "9")}"
    Log    Locator Template: "${locator_template}"

这个例子展示了如何使用内联的对象函数.由于 Python String 对象具有方法 replace,它将提供一种稳定的方法来替换相同的变量并使用它的替换输出来进一步分配关键字.

This example shows how to use the object functions inline. As the Python String object has the method replace, it will provide for a stable way of replacing the same variable and using it's replace output for further assignment in keywords.

结果如下:

Starting test: Robot.String Replace.TC
20180513 12:25:21.057 : INFO : Locator Template: "css=.tr > td:nth-child(%)
20180513 12:25:21.058 : INFO : ${locator} = css=.tr > td:nth-child(9)
20180513 12:25:21.059 : INFO : Locator Variable: "css=.tr > td:nth-child(9)"
20180513 12:25:21.060 : INFO : Inline Variable: "css=.tr > td:nth-child(9)"
20180513 12:25:21.061 : INFO : Locator Template: "css=.tr > td:nth-child(%)"
Ending test: Robot.String Replace.TC

正如您所知,替换函数返回结果,并且不更新原始字符串.这使其可用作可重用模板.

As you can tell the replace function returns the result, and does not update the original string. This makes it useful for using as as reusable template.

这篇关于Robot Framework - 根据输入更改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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