我已经创建了tests文件夹,编写了我的第一个应该打开浏览器的测试,指向一个页面并登录,然后转到主页。
按预期测试运行并失败,但我找不到原因。
浏览器应该可用,pytest-selenium是由pip安装的。
import pytest
from django.contrib.auth.models import Group, Permission, User
from django.test import TestCase, RequestFactory
class CreaPageTest(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_homepage(self):
request = self.client.get('/new')
request.user = self.user
self.assertEqual(request.status_code, 200)
def test_login(self):
request = self.client.get('/per/login')
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('peppa')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('pig')
password_field.send_keys(Keys.RETURN)
test_homepage()
> username_field = self.browser.find_element_by_name('username')
E AttributeError: 'CreaPageTest' object has no attribute 'browser'
tests/test_ore_app_views.py:27: AttributeError
我错过了什么?
我们非常感谢您对此类测试示例的任何建议。
答案 0 :(得分:1)
您应该在setUp函数中配置self.browser。您还缺少Keys
的导入。代码应该是这样的。
import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import Group, Permission, User
from django.test import TestCase, RequestFactory
class CreaPageTest(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.browser = webdriver.Firefox()
另请参阅文档,http://selenium-python.readthedocs.org/getting-started.html