4

I am creating a test suite written in python using selenium webdriver. However, when I run my test, I get the error that: 'PythonOrgSearch' object has no attribute 'driver'

I am pretty sure this is because the tests are not running in order, so the driver is closed before the tests are completed. I had previously also gotten the error: "Tried to run command without establishing a connection", which I thought also indicated that the tests were not running in order so the driver hadn't started? I am not sure this is accurate though, just my best guess. My code looks like:

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
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser


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

#sets up driver to run tests
    def setUp(self):
        self.driver = driver
        self.driver.start()

    def test_opens(self):
        driver.get("url.com")
        driver.find_element_by_id('username').send_keys('user')
        driver.find_element_by_id('password').send_keys('pass')
        driver.find_elements_by_css_selector("button[type='submit']")[0].click()
        time.sleep(2);
        self.assertIn("title", driver.title)

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

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

EDIT: I added driver=self.driver at the start of each function

2
  • 2
    You don't want to use self.driver everywhere in the class ? Commented Jul 20, 2017 at 16:58
  • Tests can be run out of order if you run them in parallel somehow. Do you? Also, the driver is started in setUp before each test; no matter what order they run in, no test should be running when a driver is not started. With several tests running in parallel, open / close could interfere, because you likely only have one browser running. Commented Jul 20, 2017 at 16:58

2 Answers 2

1

It looks like you never initialized the self.driver variable. Do you have an __init__ method inside the PythonOrgSearch class declaring one?

Sign up to request clarification or add additional context in comments.

4 Comments

oh i just fixed that i think. I added driver=self.driver at the start of each function
What do you mean by an init method? I'm not super familiar with python.
I think what you're doing should theoretically work, but instance variables (self.whatever) are typically defined in an init method. This method is basically just what's ran everytime an instance of the class is created. When you added driver=self.driver did anything change?
Yes, the tests now run fine, except that the browser never closes, which I'm assuming means the driver didn't quit. This is a problem because I am integrating the tests as automated tests into a build and deploy system, so it doesn't know when the tests are complete until the driver finishes
1

Okay, I sort of solved the problem using a workaround. Although the functions didn't run in order, they did run in the same order every time, so I put driver.quit() at the end of the function that ran last. I also (as put in the edit) adding driver=self.driver to the top of each function. As other posters have responded, it would have been better practice to put an init method in the class.

This resolution is likely not the best practice, but it does work. For people with similar issues, other responses in this thread do give some insight into the issue, I just did not find one that fixed my issue.

1 Comment

You should be instantiating and closing the driver in the setUp and tearDowns methods.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.