2

My tests are written in python using selenium and Chrome, here is what I want to do in a .robot:

Start New Session

Perform test 1

Perform test 2

Is this possible? Because what happens is every subsequent line has forgotten there is a web browser open and crashes. I tried saving it in python as self.driver but that gets erased. To make it work I created a ${DRIVER} variable in the robot file and then have the Suite setup output ${DRIVER} and then I pass ${DRIVER} into every test.

*** Variables ***
${DRIVER}     

*** Keywords ***
Start New Session
    ${DRIVER}    ${fail} =    Open Browser    ${ADDR}    ${DRIVER_LOCATION}
    run keyword if    ${fail}==1    fatal error
    Set Suite Variable    ${DRIVER}
    reset_demo    ${DRIVER}
    begin_demo    ${DRIVER}

Perform Test 1
    abc    ${DRIVER}
    xyz   ${DRIVER}

etc.

It works but it's ugly. I thought the whole point of Robot Framework is to look like natural language, so it seems like there should be a less kludgy way to do this. I've googled it every way I can think of and found nothing, perhaps I don't know the vocabulary.

4
  • Also, all my python functions use self.driver, but then have to accept a second copy ${DRIVER} as a function parameter if I also call that function directly from a .robot. Commented Apr 6, 2018 at 20:56
  • What is your real problem? Are you using either Selenium2Library or SeleniumLibrary, or are you writing your own? Commented Apr 6, 2018 at 21:40
  • Is your custom open browser keyword written in python, or is it a robot keyword? Commented Apr 6, 2018 at 22:51
  • Based on the question (and the comments to @BryanOakley answer) I really want to urge you to explain in simple, functional, terms what it is you want to do. Nothing that you've said suggests to me that what you do requires custom Python development. Virtually all simple open-browser-go-to-site-and-click-around examples can be done with the out-of-the-box keywords. So, please provide an minimal reproducible example of a public site and take it from there. Commented Apr 7, 2018 at 12:07

1 Answer 1

1

I've never seen someone use selenium in this way where you need a reference to the driver object. However, if your goal is to create keywords that look like natural language, they simply need to use the suite variable instead of having it passed to them.

*** Keywords ***
Start New Session
    ${DRIVER}    ${fail} =    Open Browser    ${ADDR}    ${DRIVER_LOCATION}
    run keyword if    ${fail}==1    fatal error
    Set Suite Variable    ${DRIVER}
    reset_demo
    begin_demo

reset_demo
    ${DRIVER}  blah blah
begin_demo
    ${DRIVER}  blah blah

If your reset_demo and begin_demo keywords are written in python, you can call BuiltIn().get_variable_value('${DRIVER}') to get the value

Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps my problem is on the python side then? Virtually everything requires the object, how to I avoid that? Example: driver.find_element_by_link_text('abc123')
@Seth: I'm a bit confused about what your real problem is. Are you writing your own selenium library, or are you using it but have written your own open browser keyword, or is there some other thing I'm not understanding?
I wrote my own open_browser keyword as my code needs to run on several platforms. I'm just trying to do basic stuff with selenium like click on check boxes on a web page and type in some text, then hand that off to the next test. My goal was a bunch of small, manageable tests, and I was hoping to have a robot file that was clean and easily readable, basically make it so simple it was fool proof... even for a customer. It just feels wrong to pass ${DRIVER} around as none of the tutorials or examples I read were like that, everything was behind the scenes. But maybe I'm just wrong.
@Seth: ok, that's a completely different problem. You simply need to pass your custom driver element back to selenium2library. There's no need to do that at the robot level, it should all be done in python. You are correct that you shouldn't need to be passing the driver around. Your problem is not "how do I pass a driver around", it's "how do I create a custom driver that seleniumlibrary can use".

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.