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

How to create multiple "sessions" or "contexts" #1166

Answered by mdmintz
benjamin-kirkbride asked this question in Q&A
Discussion options

I want to use seleniumbase to emulate two users logging in to the same site, then go back and forth having one user do something and the other asserting some stuff, back and forth for end-to-end type tests.

How would this work? I thought about using windows and tabs, but I assume that the drives don't isolate sessions, cookies, etc by default? I looked through the docs and didn't see anything promising, but I'm sorry if this is a dumb question 😅

You must be logged in to vote

Hi @benjamin-kirkbride There is a way to do that. Here are some methods you may need:

self.get_new_driver(
 browser=None, headless=None, locale_code=None, protocol=None,
 servername=None, port=None, proxy=None, agent=None, switch_to=True,
 cap_file=None, cap_string=None, disable_csp=None, enable_ws=None,
 enable_sync=None, use_auto_ext=None, no_sandbox=None, disable_gpu=None,
 incognito=None, guest_mode=None, devtools=None, remote_debug=None,
 swiftshader=None, block_images=None,
 chromium_arg=None, firefox_arg=None, firefox_pref=None,
 user_data_dir=None, extension_zip=None, extension_dir=None,
 is_mobile=None, d_width=None, d_height=None, d_p_r=None)
self.swi...

Replies: 1 comment 9 replies

Comment options

Hi @benjamin-kirkbride There is a way to do that. Here are some methods you may need:

self.get_new_driver(
 browser=None, headless=None, locale_code=None, protocol=None,
 servername=None, port=None, proxy=None, agent=None, switch_to=True,
 cap_file=None, cap_string=None, disable_csp=None, enable_ws=None,
 enable_sync=None, use_auto_ext=None, no_sandbox=None, disable_gpu=None,
 incognito=None, guest_mode=None, devtools=None, remote_debug=None,
 swiftshader=None, block_images=None,
 chromium_arg=None, firefox_arg=None, firefox_pref=None,
 user_data_dir=None, extension_zip=None, extension_dir=None,
 is_mobile=None, d_width=None, d_height=None, d_p_r=None)
self.switch_to_driver(driver)
self.switch_to_default_driver()

If you call self.get_new_driver() without any arguments, it'll use the same ones that were used during the launching of the original driver.

A sample script could look like this:

from seleniumbase import BaseCase
class MyTestClass(BaseCase):
 def test_two_drivers(self):
 self.open("data:text/html,<p>Driver 1 is active</p>")
 driver2 = self.get_new_driver()
 self.open("data:text/html,<p>Driver 2 is active</p>")
 self.switch_to_default_driver()
 self.assert_text("Driver 1 is active")
 self.switch_to_driver(driver2)
 self.assert_text("Driver 2 is active")
You must be logged in to vote
9 replies
Comment options

I'm working out a way to be able to have an instance that will automatically switch to the requisite driver instance, but not modify the api otherwise. I'm thinking using a property to wrap it is probably the play.

Comment options

The API always takes effect on the active driver. So an API method such as self.click(SELECTOR) will always run on the active driver. If you switch active drivers, that method will run on the new active driver.

Comment options

@benjamin-kirkbride Each driver will also have its own state, accessible by calling dir(self.driver) from within an ipdb breakpoint / debugger. To reach that debug mode, call:
import ipdb; ipdb.set_trace()
More info on ipdb Debug Mode here: https://seleniumbase.com/the-ultimate-pytest-debugging-guide-2021/

Comment options

@benjamin-kirkbride Related, I just shipped https://github.com/seleniumbase/SeleniumBase/releases/tag/v2.3.10 with a key improvement for driver-switching: The active driver window will be brought to the front after a switch is made, which makes it more obvious which driver is the active driver.

Comment options

Thanks!

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 によって変換されたページ (->オリジナル) /