用Django编写干净的硒测试
django-selenium-test的Python项目详细描述
琼戈硒试验
在Django 2.x、django3.0、django3.1和python3.8上编写Selenium测试。 基于django-selenium-clean。在
此文档目前正在进行中。在
教程
安装
在你的虚拟环境中:
pip install django-selenium-test
设置
- 创建新的django项目和应用程序:
- 在
在
在foo/settings.py
中,将'bar'
添加到INSTALLED_APPS
- 在
在
在foo/urls.py
中,将from bar.views import SimpleView
添加到 顶部,并将url(r'^$', SimpleView.as_view())
添加到urlpatterns
。在 - 在
将SimpleView添加到
在bar/views.py
:
importtextwrapfromdjango.httpimportHttpResponsefromdjango.views.generic.baseimportViewclassSimpleView(View):defdispatch(request,*args,**kwargs):response_text=textwrap.dedent('''\ <html> <head> <title>Greetings to the world</title> </head> <body> <h1 id="earth">Greetings to earth</h1> <h1 id="world" style="display: none;">Hello, world!</h1> <p>We have some javascript here so that when you click the button the heading above toggles between "Greetings to earth" and "Hello, world!".</p> <button onclick="toggle()">Toggle</button> <script type="text/javascript"> toggle = function () { var heading_earth = document.getElementById("earth"); var heading_world = document.getElementById("world"); if (heading_earth.style.display == 'none') { heading_world.style.display = 'none'; heading_earth.style.display = 'block'; } else { heading_earth.style.display = 'none'; heading_world.style.display = 'block'; } } </script> </body> </html> ''')returnHttpResponse(response_text)
我们已经准备好了。如果现在运行python manage.py runserver
在您的浏览器中访问http://localhost:8000/在您的浏览器中,您
应该看到简单的页面。现在让我们继续为它编写一个测试。在
编写测试
修改bar/tests.py
,使其包含以下内容:
fromunittestimportskipUnlessfromdjango.confimportsettingsfromdjango_selenium_testimportselenium,SeleniumTestCase,PageElementfromselenium.webdriver.common.byimportBy@skipUnless(getattr(settings,'SELENIUM_WEBDRIVERS',False),"Selenium is unconfigured")classHelloTestCase(SeleniumTestCase):heading_earth=PageElement(By.ID,'earth')heading_world=PageElement(By.ID,'world')button=PageElement(By.CSS_SELECTOR,'button')deftest_toggle(self):# Visit the pageself.selenium.get(self.live_server_url)# Check that the earth heading is visibleself.assertTrue(self.heading_earth.is_displayed())self.assertFalse(self.heading_world.is_displayed())# Toggle and check the new conditionself.button.click()self.heading_world.wait_until_is_displayed()self.assertFalse(self.heading_earth.is_displayed())self.assertTrue(self.heading_world.is_displayed())# Toggle again and re-checkself.button.click()self.heading_earth.wait_until_is_displayed()self.assertTrue(self.heading_earth.is_displayed())self.assertFalse(self.heading_world.is_displayed())
执行测试
尝试python manage.py test
,它将跳过测试,因为
硒未配置。您需要通过指定
SELENIUM_WEBDRIVERS
在foo/settings.py
:
fromdjango_selenium_test.settingsimportmake_chrome_driverSELENIUM_WEBDRIVERS={'default':make_chrome_driver([],{}),}
现在再试一次,它应该执行测试。在
高级测试运行技巧
以多种宽度执行测试
将此添加到您的foo/settings.py
:
SELENIUM_WIDTHS=[1024,800,350]
这将导致执行所有SeleniumTestCase
的三次,
每个指定的浏览器宽度对应一个。适用于响应式设计。
默认值是只在一个宽度上运行,1024。在
使用许多硒驱动程序
您可以有许多SELENIUM_WEBDRIVERS
:
fromdjango_selenium_test.settingsimportmake_chrome_driver,make_firefox_driverSELENIUM_WEBDRIVERS={'default':make_chrome_driver([],{})'firefox':make_firefox_driver([],{})}
默认情况下,使用default
一个。您可以指定另一个使用
SELENIUM_WEBDRIVER
环境变量:
SELENIUM_WEBDRIVER=firefox python manage.py test无头浏览器
使用无头运行selenium测试非常有用 浏览器,即在不可见的浏览器窗口中。首先,它 速度快得多。在
为此,将headless=True传递给make_BRAND_driver()函数:
fromdjango_selenium_test.settingsimportmake_chrome_driver,make_firefox_driverSELENIUM_WEBDRIVERS={'default':make_chrome_driver([],{},headless=True)'firefox':make_firefox_driver([],{},headless=True)}
使用高级集成测试
(目前未备案)
参考文献
硒测试用例对象
。。代码::python
从django_selenium_测试导入硒测试案例
SeleniumTestCase
与Django的相同
StaticLiveServerTestCase
但是它添加了一点硒
功能。从这个类派生硒测试,而不是
StaticLiveServerTestCase
。在
SeleniumTestCase
最重要的特性是selenium
属性。从技术上讲,它是selenium驱动程序的包装器。在
实践一下,你可以把它当作浏览器,或者等效的
Django的测试客户端。它有所有的selenium driver attributes and methods
},但您将主要使用get()
。它还有
以下附加方法:
- 在
self.selenium.login(**credentials)
,self.selenium.force_login(user, base_url)
,self.selenium.logout()
类似于Django测试客户机
login()
,force_login()
和logout()
方法。login()
如果登录名为 可能;False
如果提供的凭据不正确,或者 用户处于非活动状态,或者会话框架不可用。在
在force_login()
代码改编自django-selenium-login, 这是根据麻省理工学院的许可证授权的。在 - 在
self.selenium.wait_until_n_windows(n, timeout=2)
当Javascript操作导致浏览器打开时非常有用 另一扇窗户。典型用法如下:
在
button_that_will_open_a_second_window.click()self.selenium.wait_until_n_windows(n=2,timeout=10)windows=self.selenium.window_handlesself.selenium.switch_to.window(windows[1])# continue testing
如果超时(以秒为单位)和浏览器数量
windows永远不会变成n
,会引发一个AssertionError
。在
PageElement对象
fromdjango_selenium_testimportPageElement
PageElement
是一个围绕WebElement的惰性包装器;它拥有
属性和方法。它是用一个定位器初始化的,但是
元素直到需要时才被实际定位。除了
WebElement的属性和方法有:
- 在
在PageElement.exists()
:如果可以找到元素,则返回True。在 - 在
PageElement.wait_until_exists(timeout=10)
PageElement.wait_until_not_exists(timeout=10)
PageElement.wait_until_is_displayed(timeout=10)
PageElement.wait_until_not_displayed(timeout=10)
PageElement.wait_until_contains(text, timeout=10)
PageElement.wait_until_not_contains(text, timeout=10)
这些方法的作用应该从它们的名字中不言而喻。这个 以
在contains
结尾的表示是否元素包含 指定文本。如果存在超时,这些方法将引发异常。在
集成测试对象
(目前未备案)
运行django selenium test自己的单元测试
默认情况下,单元测试将使用Chrome::
./setup.py test
使用SELENIUM_BROWSER
环境变量来使用另一个浏览器:
SELENIUM_BROWSER=firefox ./setup.py test
许可证
根据BSD 3条款许可证进行许可;有关详细信息,请参见LICENSE.txt
。在
- 项目
标签: