Home Ask Login Register

Developers Planet

Your answer is one click away!

summer February 2016

Django pytest-selenium functional tests

I've created the tests folder, written my first test that should open a browser, point to a page and login, then go to home page.

Test run and fail, as expected, but I can't find out why.

browser should be available, pytest-selenium is installed by 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

what am I missing? Any advice to examples of this kind of test is really appreciated.

Answers


Ahmed Hosny February 2016

You should configure self.browser inside setUp function. You are also missing an import for Keys. Code should be like this.

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

Also please refer to the docs, here http://selenium-python.readthedocs.org/getting-started.html

Post Status

Asked in February 2016
Viewed 1,269 times
Voted 12
Answered 1 times

Search




Leave an answer


Quote of the day: live life