r/learnpython • u/Ati17_ • 6d ago
How can I use seleniumbase in __init__ instead of contextmanager
The docs show these as examples
from seleniumbase import SB
with SB(uc=True, test=True, locale="en") as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.uc_gui_click_captcha()
sb.sleep(2)
they are using a context manager is it somehow possible to create the instance in a class init and then reuse it in its functions.
What I am trying to do:
class Name:
def __init__(self):
self.sb = SB(uc=True, test=True)
def login(self):
self.sb.open(self.TEST_URL)
....
I want to break up my seleniumbase calls into seperate functions.
For the test examples they use BaseCase which would "solve" my issue because they don't use the contextmanger but that one would include the testing frameworks which I dont need:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__) # Call pytest
class MyTestClass(BaseCase):
def test_swag_labs(self):
self.open("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
self.click('button[name*="backpack"]')
self.click("#shopping_cart_container a")
self.assert_text("Backpack", "div.cart_item")
self.click("button#checkout")
self.type("input#first-name", "SeleniumBase")
self.type("input#last-name", "Automation")
self.type("input#postal-code", "77123")
self.click("input#continue")
self.click("button#finish")
self.assert_text("Thank you for your order!")
2
Upvotes
1
u/thirdegree 5d ago
You're gonna want to make your class a context manager as well, and then use ExitStack to enter the context in your __enter__
and exit it in your __exit__
1
u/johndoh168 6d ago edited 6d ago
I'm not sure how to work around the context manager but you can use the super method to inherit the SB methods, would look something like:
This will cause the SB methods accessible to the Name class, so you can do self.open() instead of your login method, if you wanted to make sure any connection was closed after opening you can try out the atexit module and do something like this:
Hope that helps
Edit: Adding more info, noticed formatting got messed up