I would like to run tests with pybot
, then run more tests with pybot
using the same browser window that the first pybot
opened.
So…
pybot test1.txt
#opens browser window and runs test1.txt and doesn't close the window
#pybot completes execution
pybot test2.txt
#uses the same browser window from test1
#pybot completes execution
pybot test3.txt
#uses the same browser window from test1
#pybot completes execution
can't figure out how to do that….
I've tried Open Browser www.mysite.com alias=1
in the first test and then Switch Browser 1
in the others, but they just error with No browser is open
Actually it is absolute possible to do however you have to organise your tests in testsuites
testsuites/ __init__.robot test1.robot test2.robot test3.robot
in
__init__.robot
you have to open browser in suite setup and destroy it in suite teardown f.e.and run tests
pybot ./testsuites
I don't know how effcient it is but I needed the same process and I used code like this to complete this...
This worked for me, try it out. If you need any information on 'select window' look here: http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Select%20Window
Or you can always try
I'm new to robotframework, but I hope this helped.
There's an open issue for new functionality to re-use existing browser session. There are some workaround mentions and custom code modifications that seem to achieve this.
A selenium driver instance has two properties characterizing its connection to a selenium webdriver - a connection url, and session id. By setting those to the values of an already running one, you effectively "hijack" it, and can use freely.
Disclaimer - the solution uses internal SE structures, so can break on newer versions. Also, as you are connecting to the running webdriver as to a
Remote
one, you cannot close it even if you want to - thus this can lead to resources leakage on the machine that it runs on; e.g. that webdriver has to be eventually terminated manually by a task manager.So first things first - you have a running browser instance, and you need to get its properties for future connections. They are 2 -
driver.command_executor._url
, anddriver.session_id
wheredriver
is the object name of the running instance. This python code will do just that:Importing that file as a Library, by calling the function/method you'll have the 2 props:
Now in the second run that needs to reattach, and with the 2 values in hand, you just use the keyword
Open Browser
and specify a remote connection:The tricky part - the moment you connect to a remote server, selenium automatically starts a new session - which is a second browser instance (this started somewhere around selenium3, though I'm not sure on the exact timing). E.g. if you start using it right now - that is not the browser you wanted, but a brand new. That's also the reason why I gave as target address "about:about" - so it loads a dummy page, very fast.
Two things must happen at this point - a) you have to get rid of the "dummy" SE session, and b) switch to the previous one:
This function/keyword is called with the known session id:
,and voilà, you are now in control of the previous browser, with its full state - the url it was at, cookies, localStorage, etc.
I'm leaving as an exercise to the reader how to automatically pass the url and the session id.
What I myself am doing is storing them in a file in a temp folder after running the first piece, and reading from there in the follow-up runs, with some error handling around it - missing or bad file, the connection cannot happen, and so on, with fallbacks to new instance creation.