In Robot Frame Work How Can we use value returned from one keyword as input argument to other keyword directly(without use of variable)

空扰寡人 提交于 2019-12-11 08:00:01

问题


How Can we use value returned from one keyword as input argument to other keyword directly (without assigning return value to variable)

Currently I am maintaining all Web Elements or Variable names and corresponding xpath in an Excel Sheet. I get XPath using keyword read_xpath by passing web element/variable name as argument.

I store the xpath in separate variable then make use of it for other or Next line keyword. Since I need to use a variable for each XPath access, i am trying to find out, whether is there any way to directly use one keyword output as input to other keyword without assigning it to variable.

The main purpose of storing XPath in Excel sheet is to avoid multiple test cases change with single Element XPath change, making just single change on Excel Sheet should be sufficient.

For Example: read_xpath reads values from a Excel sheet, Excel sheet has two columns, one variable name and second one is xpath. Function read_xpath(element) takes variable name as input and gives back xpath.

xlread.py file looks like xlread.py code Excel Sheet look like Excel sheet

sample.robot file looks as below

${login_user_textctrl}=  read_xpath    username_textctrl

clear element text  ${login_user_textctrl}
Input text    ${login_user_textctrl}    admin

Now let us check how I used read_xpath keyword in my robot file

I called Keyword read_xpath with argument username_texctrl, which returns xpath for username text Control which is stored in variable ${login_user_textctrl}. Now Input Text ${login_user_textctrl} admin works fine.

For below code

      clear element text    read_xpath     username_textctrl           

I am getting, clear element text requires 1 argument but two arguments provided, is there any way i can use the the value returned from Keyword(read_xpath) as input to other keyword directly, without assigning it to any variable?

Thanks in advance.


回答1:


Separating your locators from your robot code through an Object Repository is often done when the locators are reused a lot. An alternative implementation paradigm is to use the Page Object Model. An example of such an implementation can be found in the Robotframework-PageObjectLibrary.

In case you still prefer the Object Repository approach, then using the Selenium2Library keyword Add Location Strategy might be of interest to you. Below is a working example which uses a YAML file as it's OR to fetch the search input box and search button from Google.

ObjectRepo.yaml

OR: 
 searchbox: '//input[@id="lst-ib"]'
 searchbutton: '//button[@id="_fZl"]'

Robot Script

*** Settings ***
Library    Selenium2Library
Variables    ObjectRepo.yaml

*** Test Cases ***
Yaml Object Repository
    [Setup]    Add Location Strategy    yro    Yaml Locator Strategy
    Open Browser    http://www.google.com    Chrome
    Input Text        yro=searchbox    Robot Framework
    Click Element     yro=searchbutton
    Sleep     3s
    [Teardown]    Close All Browsers

*** Keywords ***
Yaml Locator Strategy
    [Arguments] ${browser}  ${criteria}     ${tag}  ${constraints}
    ${xpath}=     Set Variable    ${OR.${criteria}}
    ${retVal}=     Get Webelement    xpath=${xpath}
    [Return]    ${retVal}

As you can see the abstraction through a custom locator keyword allows for clearner code and not require you to fetch into a variable for later reuse.




回答2:


You can use Evaluate to call python functions directly. Example:

${my element to clear}=    Evaluate    read_xpath("username_textctrl")    xlread  

See the documentation here

(Note: Example is untested and you should check if xlread is in PYTHONPATH, or found at test run time)



来源:https://stackoverflow.com/questions/44451379/in-robot-frame-work-how-can-we-use-value-returned-from-one-keyword-as-input-argu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!