Skip to content

Commit 604151f

Browse files
committed
Convert go to link Action to async
1 parent a644c34 commit 604151f

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

Framework/Built_In_Automation/Web/Playwright/BuiltInFunctions.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import re
2828
from pathlib import Path
2929

30-
from playwright.sync_api import (
31-
sync_playwright,
30+
from playwright.async_api import (
31+
async_playwright,
3232
Page,
3333
Browser,
3434
BrowserContext,
@@ -75,7 +75,7 @@
7575
#########################
7676

7777
@logger
78-
def Open_Browser(step_data):
78+
async def Open_Browser(step_data):
7979
"""
8080
Launch a new browser instance with Playwright.
8181
@@ -104,7 +104,7 @@ def Open_Browser(step_data):
104104
# Parse parameters
105105
url = None
106106
browser_name = "chromium"
107-
headless = True
107+
headless = False
108108
viewport = default_viewport.copy()
109109
args = []
110110
timeout = default_timeout
@@ -168,7 +168,7 @@ def Open_Browser(step_data):
168168

169169
# Launch Playwright
170170
CommonUtil.ExecLog(sModuleInfo, f"Launching Playwright with {browser_name} browser", 1)
171-
playwright_instance = sync_playwright().start()
171+
playwright_instance = await async_playwright().start()
172172

173173
# Browser launch options
174174
launch_options = {
@@ -183,20 +183,20 @@ def Open_Browser(step_data):
183183

184184
# Select and launch browser
185185
if browser_name in ("chrome", "chromium"):
186-
browser = playwright_instance.chromium.launch(**launch_options)
186+
browser = await playwright_instance.chromium.launch(**launch_options)
187187
elif browser_name == "firefox":
188-
browser = playwright_instance.firefox.launch(**launch_options)
188+
browser = await playwright_instance.firefox.launch(**launch_options)
189189
elif browser_name in ("webkit", "safari"):
190-
browser = playwright_instance.webkit.launch(**launch_options)
190+
browser = await playwright_instance.webkit.launch(**launch_options)
191191
elif browser_name in ("edge", "msedge", "microsoft edge"):
192192
launch_options["channel"] = "msedge"
193-
browser = playwright_instance.chromium.launch(**launch_options)
193+
browser = await playwright_instance.chromium.launch(**launch_options)
194194
elif browser_name == "chrome-beta":
195195
launch_options["channel"] = "chrome-beta"
196-
browser = playwright_instance.chromium.launch(**launch_options)
196+
browser = await playwright_instance.chromium.launch(**launch_options)
197197
else:
198198
CommonUtil.ExecLog(sModuleInfo, f"Unknown browser '{browser_name}', using chromium", 2)
199-
browser = playwright_instance.chromium.launch(**launch_options)
199+
browser = await playwright_instance.chromium.launch(**launch_options)
200200

201201
# Context options
202202
context_options = {"viewport": viewport}
@@ -214,9 +214,9 @@ def Open_Browser(step_data):
214214
context_options["color_scheme"] = color_scheme
215215

216216
# Create context and page
217-
context = browser.new_context(**context_options)
217+
context = await browser.new_context(**context_options)
218218
context.set_default_timeout(timeout)
219-
current_page = context.new_page()
219+
current_page = await context.new_page()
220220
current_page_id = page_id
221221

222222
# Store in details
@@ -229,7 +229,7 @@ def Open_Browser(step_data):
229229

230230
# Navigate if URL provided
231231
if url:
232-
current_page.goto(url, wait_until="domcontentloaded")
232+
await current_page.goto(url, wait_until="domcontentloaded")
233233
CommonUtil.ExecLog(sModuleInfo, f"Navigated to: {url}", 1)
234234

235235
# Save to shared variables for compatibility
@@ -246,7 +246,7 @@ def Open_Browser(step_data):
246246

247247

248248
@logger
249-
def Go_To_Link(step_data):
249+
async def Go_To_Link(step_data):
250250
"""
251251
Navigate to a URL.
252252
@@ -263,8 +263,11 @@ def Go_To_Link(step_data):
263263

264264
try:
265265
if current_page is None:
266-
CommonUtil.ExecLog(sModuleInfo, "No browser open. Use 'open browser' first.", 3)
267-
return "zeuz_failed"
266+
CommonUtil.ExecLog(sModuleInfo, "No browser open. Opening browser with default settings.", 2)
267+
result = await Open_Browser(step_data)
268+
if result == "zeuz_failed":
269+
CommonUtil.ExecLog(sModuleInfo, "Failed to open browser automatically", 3)
270+
return "zeuz_failed"
268271

269272
url = None
270273
wait_until = "domcontentloaded"
@@ -275,11 +278,10 @@ def Go_To_Link(step_data):
275278
mid_l = mid.strip().lower()
276279
right_v = right.strip()
277280

278-
if mid_l == "input parameter":
279-
if left_l in ("go to link", "url", "link"):
281+
if left_l in ("go to link", "url", "link"):
280282
url = right_v
281283
elif mid_l == "optional parameter":
282-
if left_l in ("wait until", "wait_until", "waituntil"):
284+
if left_l in ("wait until", "wait_until", "waituntil", "wait time"):
283285
wait_until = right_v.lower()
284286
elif left_l == "timeout":
285287
timeout = int(float(right_v) * 1000)
@@ -292,7 +294,7 @@ def Go_To_Link(step_data):
292294
if timeout:
293295
goto_options["timeout"] = timeout
294296

295-
current_page.goto(url, **goto_options)
297+
await current_page.goto(url, **goto_options)
296298
CommonUtil.ExecLog(sModuleInfo, f"Navigated to: {url}", 1)
297299
return "passed"
298300

0 commit comments

Comments
 (0)