Executing functions in python script is ok

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

I'm pretty sure this is because the tests are not running fine, so the driver closes before the tests are complete. I also got a "Tried running command without connection" error, which I thought also indicated that the tests were not running so that the driver would not start? I'm not sure if this is accurate, just my best guess. My code 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
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 beginning of each function

+3


source to share


2 answers


Ok, I somehow solved the problem using a workaround. Although the functions were not executed in order, they were executed in the same order each time, so I put driver.quit () at the end of the last function to execute. I also (as added in edit) add driver = self.driver to the beginning of each function. As other posters have suggested, it would be better to apply the init method in the class.



This resolution is probably not the best practice, but it does work. For people with similar problems, the other answers in this thread give some insight into the problem, I just didn't find one that fixed my problem.

0


source


It looks like you never initialized the self.driver variable. Do you have a method __init__

inside the PythonOrgSearch class declaring one?



+1


source







All Articles