1

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()

3 Answers 3

4

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.

1
  • Yes. My initial thinking was in line with @Gerrat's approach. But this is interesting and might work better long run. Thanks. Nov 13, 2015 at 22:07
2

Since you're using Browser as a context manager, as soon as that block ends, your browser will close. If you want to run multiple tests with the same browser, you should call the tests while the same instance is still open. Something like:

def my_tests():
    with Browser('chrome') as browser:
        testLogin(browser, "ipaddress","my@email") 
        createOrder(browser)

With this, you'd need to change the signatures of your other functions to accept the browser parameter then.

0

I have encountered the same issue. but i have solved it as below: The key point is "global b",if i remove this statement, browser will be closed automatically, but if with this, browser will retain. hope it helps.

global b
b=Browser("chrome")

And I double checked in Splinter's official site docs, it explained that if you use "with" style, the browser will be closed automatically when it reach the code outside the "with" statement.

please visit below for details:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.