dimanche 28 mars 2021

Django python problem finding h1 element and title

I am reading the book "TDD in practice" and I follow it, however, when I want to check functional tests, I get this error:

======================================================================
ERROR: test_title (__main__.NewvisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File 
"C:\Users\Piotr\AppData\Local\Programs\Python\Python39\pythonProject\superlists\functional_tests.py", 
line 19, in test_title
header_text = self.browser.find_element_by_tag_name('h1').text
File "C:\Users\Piotr\AppData\Local\Programs\Python\Python39\pythonProject\venv2\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 530, in find_e
lement_by_tag_name
return self.find_element(by=By.TAG_NAME, value=name)
File "C:\Users\Piotr\AppData\Local\Programs\Python\Python39\pythonProject\venv2\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 976, in find_e
lement
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Piotr\AppData\Local\Programs\Python\Python39\pythonProject\venv2\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 321, in execut
e
self.error_handler.check_response(response)
File "C:\Users\Piotr\AppData\Local\Programs\Python\Python39\pythonProject\venv2\lib\site- 
packages\selenium\webdriver\remote\errorhandler.py", line 242, in che
ck_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"css selector","selector":"h1"}
(Session info: chrome=89.0.4389.90)


----------------------------------------------------------------------
Ran 2 tests in 11.605s

FAILED (errors=1)

I understand that the h1 tag is a problem, but I don't understand why it can't find it.

functional_tests.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class NewvisitorTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(4)

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

    def test_can_start_a_list_retrieve_it_later(self):
        self.browser.get('http://localhost:8000')

    def test_title(self):
        self.assertIn('', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('Listy', header_text)

        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(inputbox.get_attribute('placeholder'),'Wpisz rzecz do zrobienia')

        inputbox.send_keys('Kupić pawie pióra')
        inputbox.send_keys(Keys.ENTER)
        table = self.browser.find_element_by_id('id_list_table')
        rows = table.find_elements_by_tag_name('tr')
        self.assertTrue(any(row.text == '1: Kupić pawie pióra' for row in rows))
        self.fail('Zakończenie testu!')

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

home.html

<html>
<head>
    <meta charset="UTF-8">
    <title>Listy rzeczy do zrobienia</title>
</head>
<body>
    <h1>Twoja lista rzeczy do zrobienia</h1>
    <input id="id_new_item" />
</body>
</html>
  1. While I will add an entry to the test:

    self.assertIn('Listy', self.browser.title)

Wyskakuję błąd:

self.assertIn('Listy', self.browser.title)
AssertionError: 'Listy' not found in ''

I understand that self.browser.title doesn't refer to

<title>Listy ...</title> 

Aucun commentaire:

Enregistrer un commentaire