Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using the same SB Driver over the couse of multiple files without having to pass the Driver? #3256

Answered by mdmintz
Jediweirdo asked this question in Q&A
Discussion options

Sorry if this sounds incredibly stupid (because it probably is), but is there a way to force SeleniumBase to use the same driver anytime you initiate a new instance of it? For a project I'm making, I have multiple classes that all need to use the same SeleniumBase driver. What I used to do was just manually pass the driver in when I initialized the class like this:

File 1

class classthing1:
 def __init__(self, seleniumbase_driver, ...)

File 2

from selenium_base import SB
with SB(uc=True) as driver:
 classthing_var = classthing1(driver, ...)

However, passing the same instance of the driver like this and doing self.driver.method instead of something like self.driver is a bit annoying. Ideally, I would love to just spin up SeleniumBase from inside the class' __init__ and find/connect to a currently running driver like so...

File 1

class classthing1(BaseCase):
 def __init__(self, ...):
 #Note that the below code is mostly made up because I have no idea how you would properly achieve this 
 running_drivers = self.list_drivers()
 if(len(running_drivers) > 1):
 self.switch_to_driver(running_drivers[0])
...

File 2

# No need for importing anything because it's all handled inside the class!
classthing_var = classthing1(...)

...but this is probably stupid and/or impossible. However, is there a way to achieve something similar to this? Again, this is a pretty stupid question but I wanted to ask it in case something like this was possible to do

You must be logged in to vote

You can reuse the browser session for all tests using the BaseCase format by adding the --rs command-line option to pytest:

SeleniumBase/README.md

Lines 706 to 707 in 2bbb3c3

--rs | --reuse-session # (Reuse browser session for all tests.)
--rcs | --reuse-class-session # (Reuse session for tests in class.)

If you just want tests of the same class to reuse the same browser session, then add --rcs instead.

Here's a basic example:

pytest --rs

Replies: 1 comment 9 replies

Comment options

You can reuse the browser session for all tests using the BaseCase format by adding the --rs command-line option to pytest:

SeleniumBase/README.md

Lines 706 to 707 in 2bbb3c3

--rs | --reuse-session # (Reuse browser session for all tests.)
--rcs | --reuse-class-session # (Reuse session for tests in class.)

If you just want tests of the same class to reuse the same browser session, then add --rcs instead.

Here's a basic example:

pytest --rs
You must be logged in to vote
9 replies
Comment options

Ahh-- that makes sense. I would use one of the SB formats if I could, but the only format (besides Driver) that supports pure python requires you to use with, which is something I'd rather not deal with since I'm working with classes. Since the asserts are the only thing I need from SB (so far), I'll just use that to reimplement, I'll just import unittests and reimplement what I can. Thanks for helping me out! I'll let you know if I have any other questions or start a new disscussion if it's not related

Comment options

When initiating a new instance of Driver(), is there any way to have it start inside a window of an already-running Driver instance? The remote-debugging-port works, but it keeps making new windows that fail 30% of the time with a connection error I can't make heads or tails of:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=44513): Max retries exceeded with url: /session/c12de72e918e229196e7638f4c0b89bb/screenshot (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f5e4d358e50>: Failed to establish a new connection: [Errno 111] Connection refused'))
Comment options

Set headless to True if you don't want to see any extra browser.

Comment options

I still need the headed for debugging purposes, it's just bad that it keeps opening new windows every time the driver initializes despite being in the same port. Also for whatever reason in headless mode, I keep getting the HTTPConnection error whenever I initialize more than 1 driver

Comment options

Tried fixing the error on my own, but I've had no luck. Anytime I do headless and make a new Driver under the same port, it kills the program with the "Connection Refused" error I put in an earlier reply. Any ideas for how to fix it?

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /