Error Message: Tried to run command without establishing a connection When running multiple tests with unit test

Viewed 18783

I am having an issue in which all of my tests function correctly when run individually. However, when I try to run all the tests in the file, I get the error Message: Tried to run command without establishing a connection. I know the tests work because they run individually, but I need to be able to run all the tests without running them one by one. My code currently looks like this:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = driver

    def test_business_excellence_opens(self):
        driver.get("http://url.network.com")
        self.assertIn("Page Title", driver.title)

    def test_home_links(self):
        driver.find_element_by_id('webapps').click()
        self.assertTrue('SPT facilitates the cultural change' in self.driver.page_source)
        time.sleep(1)
        driver.find_element_by_id('complexity').click()
        self.assertTrue('Thank you again' in self.driver.page_source)
        time.sleep(1)

    def test_contact_page(self):
        driver.get("http://url.network.com/Home/Contact")
        time.sleep(2)
        driver.find_element_by_id('name').send_keys('name')
        driver.find_element_by_id('ID').send_keys('123456')
        driver.find_element_by_id('email').send_keys('email@email.com')
        driver.find_element_by_id('message').send_keys('Unit testing form')
        driver.find_elements_by_css_selector("button[type='submit']")[0].click()

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

To run them one by own I add driver=self.driver and driver.get("url.network.com") at the top of the function

1 Answers
Related