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

16,927

I also had the same problem , I understood that you haven’t started a Marionette session before trying to speak to the port. @After(annotation), If you are using this annotation and driver is closed and Marionette/Gecko is not able to initiate again.Solution:Remove driver .close(); after each test case option. 1)for individual test case the driver is started and closed 2)To run other test case the driver should start again running but gecko is failed to initialize for the second test case, so remove driver.close after end of each test case. Here in your case:

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

can not be end of each test case, you can use at the end of last test case

Share:
16,927
Cassie H.
Author by

Cassie H.

I am a student at the University of Colorado Boulder, majoring in Computer Science. I primarily develop web applications.

Updated on June 14, 2022

Comments

  • Cassie H.
    Cassie H. almost 2 years

    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 protected]')
            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