seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5k stars 944 forks source link

I don't want to close my browser every time, is there any way?Because I don't want to log in again #411

Closed meilisong1 closed 4 years ago

meilisong1 commented 4 years ago

class TestCaseClass(WebBaseCase):

@classmethod
def setUpClass(self):
    print("start")

def test_something1(self):
    self.open("http://www.baidu.com/")
    time.sleep(3)
    self.assertTrue(True)

def test_something2(self):
    self.open("http://www.qq.com/")
    time.sleep(2)
    self.assertTrue(True)

@classmethod
def tearDownClass(cls):
    print("end")

It's not uncommon to not want to write all validation into one testcase, but not to close the browser every time

in addition: Since we are using intelligent validation, --user-data-dir also tried and failed

mdmintz commented 4 years ago

Hi @meilisong1 Each test method needs to be independent from other test methods, therefore each one will handle spinning up and spinning down web browsers on their own to maintain a clean state. If you need to share a browser window for multiple "tests", consider having those tests be separate methods that are called from the same test method. Example:

from seleniumbase import BaseCase

class MyTestClass(BaseCase):

    def do_something(self):
        self.open("http://www.baidu.com/")
        # Do something here

    def do_something_2(self):
        self.open("http://www.qq.com/")
        # Do something here

    def test_some_things(self):
        self.do_something()
        self.do_something_2()
meilisong1 commented 4 years ago

Hi @meilisong1 Each test method needs to be independent from other test methods, therefore each one will handle spinning up and spinning down web browsers on their own to maintain a clean state. If you need to share a browser window for multiple "tests", consider having those tests be separate methods that are called from the same test method. Example:

from seleniumbase import BaseCase

class MyTestClass(BaseCase):

    def do_something(self):
        self.open("http://www.baidu.com/")
        # Do something here

    def do_something_2(self):
        self.open("http://www.qq.com/")
        # Do something here

    def test_some_things(self):
        self.do_something()
        self.do_something_2()

thx