From c295c5e40dce6b58517ba44c34ebb0fbecf96356 Mon Sep 17 00:00:00 2001 From: Constantin Hong Date: Wed, 8 May 2024 02:50:21 +0900 Subject: [PATCH] tests/test_xpath_selector_unit/test: Add same behavior for xpath 1 --- .../tests/test_xpath_selector_unit.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/changedetectionio/tests/test_xpath_selector_unit.py b/changedetectionio/tests/test_xpath_selector_unit.py index 958e2e29..cfee3080 100644 --- a/changedetectionio/tests/test_xpath_selector_unit.py +++ b/changedetectionio/tests/test_xpath_selector_unit.py @@ -259,3 +259,21 @@ def test_Broken_DOM_02(html_content, xpath, answer): assert type(html_content) == str # Check the answer is *not in* the html_content assert answer not in html_content + +@pytest.mark.parametrize("html_content", [DOM_violation_two_html_root_element]) +@pytest.mark.parametrize("xpath, answer", [ + ("/html/body/p[1]", 2), + ("/html", 2), + ("//html", 2), + ("//body", 2), + ("/html/body", 2), + ]) +def test_Broken_DOM_03(html_content, xpath, answer): + # In normal situation, DOM's root element node is only one. So when DOM violation happens, Exception occurs. + + from lxml import etree, html + parser = etree.HTMLParser() + tree = html.fromstring(bytes(html_content, encoding='utf-8'), parser=parser) + + # test xpath 1 + assert len(tree.xpath(xpath)) == 2