Тест Django - глобальная переменная из другого скрипта не определена

Я пытался запустить второй скрипт из основного, вызываемого командой django manage.py test. Однако, я получаю эту ошибку:

File "<string>", line 254, in <module>
File "<string>", line 61, in login
NameError: name 'driver' is not defined

Сначала test.py, который запускается во время manage.py test:

import sys
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

#https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testcase
#LiveServerTestCase

class TestReunioes(StaticLiveServerTestCase):        
    fixtures = ['fix_core.json','users.json']

    def testRun(self):
        exec(open("seleniumtest.py").read())

И второй скрипт, seleniumtest.py:

import traceback
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys

url = "http://localhost:3000"

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
driver.set_window_size(1700, 850)
driver.implicitly_wait(20)

def login():
    user="teste"
    passwd="321"
    driver.find_element(By.NAME, "user").send_keys(user) #line 61 from the error
    driver.find_element(By.NAME, "passwd").send_keys(passwd)
    driver.find_element(By.NAME, "enter").click()
#
# Several other functions to test other parts
#
try:
    print("Starting test")
    login() #line 254 from the error
except:
    traceback.print_exc()
finally:
    driver.quit()

Это сбивает меня с толку, потому что если я запускаю seleniumtest.py из командной строки, он работает нормально. Он также работает нормально при вызове из файла python, который буквально состоит только из exec(open("seleniumtest.py").read()) . Ошибка происходит только при вызове из django test.

Я знаю, что это что-то связанное с областью видимости атрибута/переменной, но я не знаю, почему это происходит и как это решить.

Вернуться на верх