1

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.

2 Answers 2

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.

4
  • Above Solution Works absolutely fine. Can i have little bit more details regarding the parameters passed to Yaml Locator Strategy, What are the values for ${criteria}, ${tag} and ${constraints}?. How can I extend Yaml Locator Strategy to accept Link and Id along with XPath?
    – Jagadanna
    Jun 12, 2017 at 13:55
  • In the example the argument ${criteria} would contain the value searchbox, i.e. anything that comes after the yro=. The argument ${browser} will contain a library reference to the active Selenium2Library. This would be of use in case you have a custom python library that uses it. The other two, ${tags} and ${contstraints} I have not seen filled in. So I treat them as empty by design. Jun 12, 2017 at 18:27
  • In case this reply is the answer to your question, can you mark it as such so that those that find this question in the future know what worked for you. Jun 15, 2017 at 5:23
  • 1
    i clicked on up arrow button to say it worked for me but its not accepting it, its say it needs at least 15 reputation to display it people.
    – Jagadanna
    Jun 20, 2017 at 5:05
0

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)

1
  • The Code throws error, No keyword with name 'clear element text= .
    – Jagadanna
    Jun 12, 2017 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.