【问题标题】:Automating Selenium tests in Python在 Python 中自动化 Selenium 测试
【发布时间】:2026-01-24 06:35:01
【问题描述】:

我有一个 Django 项目,我正在尝试使用 Selenium 编写浏览器交互测试。我的目标是让 Hudson/Jenkins 的测试自动化。到目前为止,我能够让测试到达 Django 服务器,但是从服务器日志中我看到它正在访问 url /selenium-server/driver 而不是正确的路径。

这是我的代码(基于 Firefox 的 Selenium IDE 插件生成的代码:

from selenium import selenium


class AccountAdminPageTests(unittest.TestCase):
    def setUp(self):
        self.selenium = selenium("localhost", 
                                 8000, 
                                 "*chrome", 
                                 "http://localhost:8000/")
        self.selenium.start()
        self.selenium.open("/")

    def test_ok(self):
        self.assertTrue(self.selenium.is_text_present('OK'))

    def tearDown(self):
        self.selenium.stop()


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

有什么线索吗?

【问题讨论】:

  • 您需要在不同的端口上运行 selenium(默认为 4444)。这就是您点击错误网址的原因

标签: python django selenium


【解决方案1】:

从未见过确切的错误,但我认为 Selenium 正在尝试连接到您的应用程序而不是 selenium 服务器(一个 .jar 文件)。

selenium 服务器的端口应该是 selenium() 的第一个参数

这应该默认为端口 4444,您可能必须以

开头
$ java -jar selenium-server.jar

FWIW 这是我在 CI 服务器上运行 selenium 测试的方法...

from multiprocessing import Process
from django.test import TestCase
from selenium import selenium

class SeleniumFixtureCase(TestCase):
"""
Wrapper to multiprocess localhost server and selenium instance on one
test run.
"""

def setUp(self):
    "Make the selenium connection"
    TestCase.setUp(self)
    self.server = Process(target=serve)
    self.server.start()
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*firefox",
                             "http://localhost:8000/")
    self.selenium.start()

def tearDown(self):
    "Kill processes"
    TestCase.tearDown(self)
    self.server.terminate()
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)

def _login(self):
    "Login as Albert Camus"
    self.selenium.open("http://localhost:8000/admin/")
    self.selenium.wait_for_page_to_load("30000")
    self.selenium.type("id_username", "albert")
    self.selenium.type("id_password", "albert")
    self.selenium.click("//input[@value='Log in']")
    self.selenium.wait_for_page_to_load("30000")

【讨论】:

  • self.server = Process(target=serve) 代表什么?没有提到服务......
【解决方案2】:

我和一位同事使用 django 和 selenium 2 创建了一些自动化的 selenium 测试。它无需使用 jar 文件即可工作。这是一个link to the code,它显示了我们的测试用例。

【讨论】:

    【解决方案3】:

    我们目前使用 django-jenkins 从 Jenkins 成功运行 django 测试: https://github.com/kmmbvnr/django-jenkins

    FWIW 现在 django 以 LiveServerTestCase 的形式提供对 Selenium 的支持: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.LiveServerTestCase

    LiveServerTestCase 启动一个 django 服务器,允许 Selenium 等客户端连接到它。

    此外,您现在可以使用 PhantomJs 作为无头测试的 Web 驱动程序。这使得 CI 集成变得更加容易。

    【讨论】:

    • 使用 LiveServerTestCase,您不必让实时服务器并行运行,它会使用测试数据库(包括遵守 fixtures 变量!)。这绝对是持续集成的必经之路。 staticfiles 模块还有一个 LiveServerTestCase 的子类,可以在这种情况下正确处理静态文件。
    • @Risadinha 提到的子类是StaticLiveServerTestCase: django.readthedocs.org/en/latest/ref/contrib/…
    【解决方案4】:

    selenium() 调用的第二个参数应该是 Selenium 服务器端口号(如 David 的回答中所写),而不是测试应用程序的端口号。默认端口号为4444。我会将电话替换为:

      self.selenium = selenium("localhost", 4444, ....
    

    【讨论】:

      【解决方案5】:

      对于自动化 Selenium 测试,我肯定会使用像 Jenkins 这样的 CI 解决方案。您可以配置 Jenkins 以提取您的代码存储库并从您的服务器触发 Selenium 测试。我一直在 Jenkins 使用 Pytest。

      您可以在此处找到使用 Github 和 Selenium 配置 Jenkins 的分步教程:http://www.6020peaks.com/2015/12/how-to-build-a-test-automation-solution-for-your-web-projects/

      【讨论】:

        最近更新 更多