Jagadanna
Jagadanna

Reputation: 43

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

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.

Upvotes: 1

Views: 3120

Answers (2)

Helio
Helio

Reputation: 3737

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)

Upvotes: 0

A. Kootstra
A. Kootstra

Reputation: 6961

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.

Upvotes: 1

Related Questions