Search code examples
python-2.7splinter

Python splinter keeping browser open after test execution


So I'm trying to get started writing automated testing using the Splinter framework. When I run the first method the browser just closes. What I need is to keep the same browser open and allow me to call another method that will act on that browser as well. I'm confused as to why it's closing because splinter has a browser.quit() which I would think I would have to call to quit but as seen below I'm not calling anything like that.

from splinter import Browser   

def testLogin(ipaddress, username):
    global browser
    with Browser('chrome') as browser:
            url = "http://%s" % ipaddress
            browser.visit(url)
            browser.fill('EmailAddress', username)
            browser.fill('Password','123456789')
            button = browser.find_by_id('login')
            button.first.click()
            if browser.is_text_present('Summary of Orders'):
                print("Pass: Login was succesful")
            elif browser.is_text_present('There are currently no orders that are available for you to browse.'):
                print("Pass: Login was succesful")
            else:
                print("Fail: Login was not succesful")
return

def createOrder():
    browser.find_by_id('nav-OrderModule-Create').first.click()
    browser.fill('nameBox','test')


testLogin("ipaddress","my@email") 
createOrder()

Solution

  • I've encountered this issue before. Here's how I've managed this issue in the past.

    class LoginTest(unitest.TestCase):
        def setUp(self):
            self.browser = Browser('chrome')
    
        def test_login_with_username_and_password(self):
            ...
    
        def tearDown(self):
            self.browser.quit()
    

    @Gerrat's approach should work, but this is an other approach you can take. Note the setUp and tearDown methods, which are run after each test.