python - 按顺序在python脚本中运行函数

标签 python unit-testing selenium selenium-webdriver python-unittest

我正在使用 selenium webdriver 创建一个用 python 编写的测试套件。但是,当我运行测试时,出现以下错误:“PythonOrgSearch”对象没有属性“驱动程序”

我很确定这是因为测试没有按顺序运行,所以驱动程序在测试完成之前就关闭了。我之前也遇到过错误:“试图在没有建立连接的情况下运行命令”,我认为这也表明测试没有按顺序运行,所以驱动程序没有启动?我不确定这是否准确,只是我最好的猜测。我的代码如下所示:

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

编辑:我在每个函数的开头添加了 driver=self.driver

最佳答案

看起来您从未初始化 self.driver 变量。您是否在 PythonOrgSearch 类中声明了一个 __init__ 方法?

关于python - 按顺序在python脚本中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220824/

相关文章:

python - 为 sklearn 管道获取 "valueError: could not convert string to float: ..."

python - 解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义

python - 设置 URL gif 背景但不使用 Python 2 上的新框架

python - 如何告诉 tox 不要从依赖项收集测试?

python - 使用 Python 进行依赖测试

python - Django 数据库未迁移

java - Katalon Debian headless 浏览器

python Selenium : Firefox set_preference to overwrite files on download?

python - 使用 selenium 和 phantomJS 切换框架

javascript - 如何使用 Jest toHaveBeenCalled 与不同的实例一起工作