Running functions in a python script in order

Viewed 726

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 Answers
Related