tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

node.py (3627B)


      1 import pytest
      2 
      3 from webdriver.client import WebElement, ShadowRoot
      4 from tests.support.asserts import assert_error, assert_success
      5 from . import execute_script
      6 
      7 
      8 PAGE_DATA = """
      9    <div id="deep"><p><span></span></p><br/></div>
     10    <div id="text-node"><p></p>Lorem</div>
     11    <br/>
     12    <svg id="foo"></svg>
     13    <div id="comment"><!-- Comment --></div>
     14    <script>
     15        var svg = document.querySelector("svg");
     16        svg.setAttributeNS("http://www.w3.org/2000/svg", "svg:foo", "bar");
     17    </script>
     18 """
     19 
     20 # https://dom.spec.whatwg.org/#ref-for-dom-node-nodetype%E2%91%A0
     21 NODE_TYPE = {
     22    "element": 1,
     23    "attribute": 2,
     24    "text": 3,
     25    "cdata": 4,
     26    "processing_instruction": 7,
     27    "comment": 8,
     28    "document": 9,
     29    "doctype": 10,
     30 }
     31 
     32 @pytest.mark.parametrize("as_frame", [False, True], ids=["top_context", "child_context"])
     33 def test_detached_shadow_root(session, get_test_page, as_frame):
     34    session.url = get_test_page(as_frame)
     35 
     36    if as_frame:
     37        frame = session.find.css("iframe", all=False)
     38        session.switch_to_frame(frame)
     39 
     40    element = session.find.css("custom-element", all=False)
     41 
     42    # Retrieve shadow root to add it to the node cache
     43    shadow_root = element.shadow_root
     44 
     45    result = execute_script(session, """
     46        const [elem, shadowRoot] = arguments;
     47        elem.remove();
     48        return shadowRoot;
     49        """, args=[element, shadow_root])
     50    assert_error(result, "detached shadow root")
     51 
     52 
     53 @pytest.mark.parametrize("as_frame", [False, True], ids=["top_context", "child_context"])
     54 def test_stale_element(session, get_test_page, as_frame):
     55    session.url = get_test_page(as_frame)
     56 
     57    if as_frame:
     58        frame = session.find.css("iframe", all=False)
     59        session.switch_to_frame(frame)
     60 
     61    element = session.find.css("div", all=False)
     62 
     63    result = execute_script(session, """
     64        const elem = arguments[0];
     65        elem.remove();
     66        return elem;
     67        """, args=[element])
     68    assert_error(result, "stale element reference")
     69 
     70 
     71 @pytest.mark.parametrize("expression, expected_type", [
     72    (""" document.querySelector("svg").attributes[0] """, "attribute"),
     73    (""" document.querySelector("div#text-node").childNodes[1] """, "text"),
     74    (""" document.implementation.createDocument("", "root", null).createCDATASection("foo") """, "cdata"),
     75    (""" document.createProcessingInstruction("xml-stylesheet", "href='foo.css'") """, "processing_instruction"),
     76    (""" document.querySelector("div#comment").childNodes[0] """, "comment"),
     77    (""" document""", "document"),
     78    (""" document.doctype""", "doctype"),
     79 ], ids=["attribute", "text", "cdata", "processing_instruction", "comment", "document", "doctype"])
     80 def test_node_type(session, inline, expression, expected_type):
     81    session.url = inline(PAGE_DATA)
     82 
     83    response = execute_script(
     84        session,
     85        f"const result = {expression}; return {{'result': result, 'type': result.nodeType}}",
     86    )
     87    result = assert_success(response)
     88 
     89    assert result["type"] == NODE_TYPE[expected_type]
     90 
     91    if expected_type == "document":
     92        assert 'location' in result['result']
     93    else:
     94        assert result['result'] == {}
     95 
     96 
     97 
     98 @pytest.mark.parametrize("expression, expected_type", [
     99    ("document.querySelector('div')", WebElement),
    100    ("document.querySelector('custom-element').shadowRoot", ShadowRoot),
    101 ], ids=["element", "shadow-root"])
    102 def test_web_reference(session, get_test_page, expression, expected_type):
    103    session.url = get_test_page()
    104 
    105    result = execute_script(session, f"return {expression}")
    106    reference = assert_success(result)
    107    assert isinstance(reference, expected_type)