Add sizzle support

Use the same `document` tracking fix/hack as in our CDP driver.
This commit is contained in:
Martin Pitt 2024-07-26 16:43:51 +02:00
parent 49317d7c19
commit 6c97825295
4 changed files with 17 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import logging
import os
import threading
import time
from pathlib import Path
from typing import Any
import bidi
@ -58,6 +59,18 @@ class Browser:
asyncio.run_coroutine_threadsafe(self.driver.start_session(), self.loop).result()
test_functions = Path("test-functions.js").read_text()
self.bidi("script.addPreloadScript", functionDeclaration=f"() => {{ {test_functions} }}")
try:
sizzle_js = (Path(__file__).parent / "node_modules/sizzle/dist/sizzle.js").read_text()
# HACK: sizzle tracks document and when we switch frames, it sees the old document
# although we execute it in different context.
sizzle_js = sizzle_js.replace('context = context || document;', 'context = context || window.document;')
self.bidi("script.addPreloadScript", functionDeclaration=f"() => {{ {sizzle_js} }}")
except FileNotFoundError:
pass
def close(self):
asyncio.run_coroutine_threadsafe(self.driver.close(), self.loop).result()
self.loop.call_soon_threadsafe(self.loop.stop)
@ -196,6 +209,7 @@ try:
b.wait_text("#super-user-indicator", "Limited access")
b.switch_to_frame("cockpit1:localhost/system")
b.wait_visible(".pf-v5-c-alert:contains('Web console is running in limited access mode.')")
b.wait_in_text(".system-configuration", "Join domain")
b.switch_to_top()

View file

@ -127,9 +127,6 @@ class WebdriverBidi:
"log.entryAdded", "browsingContext.domContentLoaded",
])
test_functions = Path("test-functions.js").read_text()
await self.bidi("script.addPreloadScript", functionDeclaration=f"() => {{ {test_functions} }}")
# wait for browser to initialize default context
for _ in range(10):
realms = (await self.bidi("script.getRealms"))["realms"]
@ -193,7 +190,8 @@ class WebdriverBidi:
"""Send a Webdriver BiDi command and return the JSON response"""
payload = json.dumps({"id": self.last_id, "method": method, "params": params})
log_proto.debug("ws ← %r", payload)
# avoid log spam for preload scripts
log_proto.debug("ws ← %r", method if method == "script.addPreloadScript" else payload)
await self.ws.send_str(payload)
future = asyncio.get_event_loop().create_future()
self.pending_commands[self.last_id] = future

View file

@ -42,6 +42,7 @@
"jed": "1.1.1",
"qunit": "2.21.1",
"sass": "1.77.8",
"sizzle": "2.3.10",
"stylelint": "16.7.0",
"stylelint-config-recommended-scss": "14.0.0",
"stylelint-config-standard": "36.0.1",

View file

@ -1,8 +1,4 @@
window.ph_select = function(sel) {
if (!window.Sizzle) {
return Array.from(document.querySelectorAll(sel));
}
if (sel.includes(":contains(")) {
if (!window.Sizzle) {
throw new Error("Using ':contains' when window.Sizzle is not available.");