python - 如何从 django View 运行unitest和selenium?

标签 python django selenium

我有一个功能测试“y1.py”,已从 selenium IDE 导出。它看起来像:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re    

class Y1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.yahoo.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_y1(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_link_text("Weather").click()
        driver.save_screenshot('out11.png')    

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)    

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

我试图直接从 python/django 函数中调用它。在调查这个问题时我发现:AttributeError 'module' object has no attribute 'runserver' in django ,其中 Udi 指出:

Are you trying to run unitest and selenium from a view? You should consider launching a second process for that.

我该怎么做?

最佳答案

您可以使用 subprocess module 作为新进程启动 django 服务器.

关于python - 如何从 django View 运行unitest和selenium?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472238/

相关文章:

ruby-on-rails - 如何使用Webkit或Selenium驱动程序填充来自 capybara 的ckeditor

python - 创建 JSON 对象时如何使用列名,Python

python - 在django中接收发送邮件的发送列表

python - Django 测试 : Faking User Creation

java - 无法使用selenium下载任何文件

java - 在表中搜索给定文本,如果存在则应显示通过

python - 向后切片元组 - 如何处理获取第一个元素?

python - 使用 BeautifulSoup 查找 href 链接

python - 如何在 QMainWindow MVC PyQt 的 closeEvent 中调用非 QObject 的方法

python - Django Boto 和上传到 S3 是一个 400 错误请求