-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to create multiple "sessions" or "contexts" #1166
-
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 assert
ing 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 😅
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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")
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
@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/
Beta Was this translation helpful? Give feedback.
All reactions
-
@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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🎉 1
-
Thanks!
Beta Was this translation helpful? Give feedback.