Is it possible to reuse SST's browser session for direct selenium actions?

Asked by Konstantin on 2012-03-07

Is it possible to reuse SST's browser session for direct selenium actions?

For example:

from sst.actions import *
go_to('website')

from selenium import webdriver
webdriver.Firefox().get("someotherwebsite")

Was planning to use it for WebDriver ActionChains API

Question information

Language:
English Edit question
Status:
Solved
For:
selenium-simple-test Edit question
Assignee:
No assignee Edit question
Solved by:
Corey Goldberg
Solved:
2012-03-08
Last query:
2012-03-08
Last reply:
2012-03-07
Best Corey Goldberg (coreygoldberg) said : #1

you can do what you proposed, but I'm not sure that is what you need.

doing this:
webdriver.Firefox().get("someotherwebsite")

... will launch a new Firefox instance and new browser and load "someotherwebsite" .
If that's all you need, go for it :)

if you wanted to use the existing SST browser session, you could do:

from sst.actions import browser

... which would give you access to 'browser', which is a webdriver Firefox() instance at run-time.

consider this example:

--------------------------------
from sst.actions import *
from sst.actions import browser

go_to('http://www.google.com')
browser.get('http://www.yahoo.com')
--------------------------------

... both pageloads will use the same browser session.

does that help?
(have a look at actions.py in the SST source to see how/what/when 'browser' is used.)

a better solution (if possible) is to modify SST to be able to handle ActionChains nicely.

please let me know your progress on this. Sounds like an interesting area, and it would be really cool if SST could support things like drag-n-drop.

-Corey

Konstantin (konstantin1979) said : #2

Thanks Corey Goldberg, that solved my question.

Florian Sesser (fs-8) said : #3

Thanks for above answer Corey!

However I have an issue with the code when running multiple tests with a common library in shared/ that should use the selenium session (to take screen shots).

In tests/shared/vd.py:
-----------------------
from sst.actions import browser
[...]
def take_snap(page):
    browser.get_screenshot_as_file(os.path.join(folder, filename))
-----------------------

In tests/access_page.py:
-----------------------
from sst.actions import *
from sst.actions import browser
import vd
[...]
go_to('http://localhost:3000')
vd.take_snap("homepage")
[...]
-----------------------

(there are more tests like access_page.py)

Above code works for the first test (file) executed or when SST is called with one specific test as parameter. But I intend to use SST with the -d parameter.

Expected behavior: SST starts a new browser session for every test and vd.py use that session to take screen shots

Actual behavior: First test works (and creates screen shots); Later tests will not work but throw a "URLError: <urlopen error [Errno 111] Connection refused>", maybe (guessing here) because they will not use the current (new) browser object, but the old one.

Probably I am misunderstanding some generic python concept or have created circular dependency or so, but ATM I am clueless why sst/action.py/start(), when updating the "global browser", the change won't reach my tests/shared/vd.py.

Corey Goldberg (coreygoldberg) said : #4

Florian. why not use SST's 'take_screenshot()' action ?

Florian Sesser (fs-8) said : #5

Because it doesn't give us enough freedom on naming files/directories. Our "vd.take_snap()" method should take one argument (a snapshot name) and then create a directory with that name and save the screenshot as <pagename>/<BUILD_NUMBER>.png. We'd also like SST to not clear that directory at the next test run. This should give us a nice stop-motion movie over the lifetime of our project :)

Several workarounds are of course possible, like moving/renaming the files with shell scripts as a post-build action. But that's not as chique, and I'd like to understand what's the issue here.

We have another kind of workaround in place now: We changed the signature of take_snap(page) to take_snap(page, browser). The second argument takes the browser instance from the different tests. This made this work for us; Alas, I don't know why. Shouldn't the "global browser" update in actions.py/start() also reach the helpers in our "shared" directory?

RĂ³bert Malovec (malovec) said : #6

I can't import "browser". Am I doing something wrong?
I have shared module called common.py, its 1st line is:
from sst.actions import browser

importing * from sst.actions works fine.

When I run the test I am getting this error:

from sst.actions import browser
ImportError: cannot import name browser

$ sst-run -V
SST version: 0.2.4

Vincent Ladeuil (vila) said : #7

@malovec: Don't hijack questions please, this makes it harder to properly track them. There is no 'browser' symbol in the sst.actions module, there is a sst.browsers module and a *private* sst.actions._test_browser but since the later is private, you're not supposed to use it as there is no guarantee that it will remain available in future sst versions.