18.5. Tests Templates

18.5.1. Install

  • python -m pip install selenium

Run command in the system terminal:

$ python -m pip install selenium

18.5.2. Setup

  1. Download the ChromeDriver from the link above

  2. Extract downloaded file

  3. Move the file to the project directory with selenium tests

  4. Set webdriver.Chrome('chromedriver') in the test file

18.5.3. Example

File myproject/shop/tests/test_templates.py:

from django.test import StaticLiveServerTestCase
from django.urls import reverse
from selenium import webdriver


class TestTemplates(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome('chromedriver')

    def tearDown(self):
        self.browser.close()

    def test_product_list(self):
        url = self.live_server_url + reverse('shop:product_list')
        self.browser.get(url)
        self.assertIn('Product List', self.browser.title)
        self.assertIn('Products', self.browser.find_element_by_tag_name('h1').text)

    def test_product_href(self):
        url_product_list = self.live_server_url + reverse('shop:product_list')
        self.browser.get(url)
        self.browser.find_element_by_link_text('Alpha').click()
        url_product_detail = self.live_server_url + reverse('shop:product_detail', kwargs={'pk':1})
        self.assertEqual(self.browser.current_url, url_product_detail)