Skip to content

Commit 1c942c0

Browse files
author
Shriyansh Agnihotri
committed
adding more checks to avoid abrupt reload and triggering browser tab failure and reload
1 parent 0bcee56 commit 1c942c0

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

testzeus_hercules/core/playwright_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ async def go_to_homepage(self) -> None:
975975

976976
async def set_navigation_handler(self) -> None:
977977
page: Page = await self.get_current_page()
978+
await page.wait_for_load_state("domcontentloaded")
978979
page.on("domcontentloaded", handle_navigation_for_mutation_observer)
979980

980981
async def set_iframe_navigation_handlers() -> None:
@@ -1856,6 +1857,13 @@ async def reuse_or_create_tab(self, force_new_tab: bool = False) -> Page:
18561857
# Try to reuse existing tab (use the most recent one)
18571858
page = pages[-1] # The most recently used page
18581859

1860+
try:
1861+
await page.wait_for_load_state("networkidle", timeout=5000)
1862+
except Exception as e:
1863+
logger.warning(f"Failed to wait for networkidle: {e}")
1864+
finally:
1865+
await page.wait_for_load_state("domcontentloaded")
1866+
18591867
# Check if the page is responsive, with a shorter timeout to avoid hanging
18601868
try:
18611869
# Simple check (the timeout is applied at a higher level)
@@ -1881,6 +1889,7 @@ async def reuse_or_create_tab(self, force_new_tab: bool = False) -> Page:
18811889
for i in range(len(pages) - 2, -1, -1):
18821890
try:
18831891
page = pages[i]
1892+
await page.wait_for_load_state("domcontentloaded")
18841893
await page.evaluate("1")
18851894
logger.info(f"Reusing alternative tab with URL: {page.url}")
18861895

testzeus_hercules/core/tools/open_url.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def openurl(
2424
timeout: Annotated[int, "Additional wait time in seconds after initial load."] = 3,
2525
force_new_tab: Annotated[bool, "Force opening in a new tab instead of reusing existing ones."] = False,
2626
) -> Annotated[str, "Returns the result of this request in text form"]:
27+
2728
logger.info(f"Opening URL: {url} (force_new_tab={force_new_tab})")
2829
browser_manager = PlaywrightManager()
2930
await browser_manager.get_browser_context()

0 commit comments

Comments
 (0)