-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaywright_automation.py
71 lines (62 loc) · 2.32 KB
/
playwright_automation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import Callable
from playwright.sync_api import Page, sync_playwright
CONSTANT_LOCATOR_TYPES = {
"text": "get_by_text",
"label": "get_by_label",
"placeholder": "get_by_placeholder",
"alt text": "get_by_alt_text",
"title": "get_by_title"
}
def playwright_goto(page: Page, url: str) -> None:
page.goto(url)
def playwright_click(page: Page, type: str, text: str, input: str) -> None:
if type:
locator_type = _get_locator_type(type)
element = getattr(page, locator_type)(text)
else:
element = page.locator(text)
element.click()
def playwright_iframe_click(page: Page, type: str, text: str, input: str) -> None:
iframe = page.frame_locator(input)
if type:
locator_type = _get_locator_type(type)
element = getattr(iframe, locator_type)(text)
else:
element = iframe.locator(text)
element.click()
def playwright_click_download(page: Page, type: str, text: str, input: str) -> str:
with page.expect_download() as download_info:
# Perform the action that initiates download
if type:
locator_type = _get_locator_type(type)
element = getattr(page, locator_type)(text)
else:
element = page.locator(text)
element.click()
# Save the downloaded file
download = download_info.value
download.save_as("static/downloads/" + download.suggested_filename)
return f"static/downloads/{download.suggested_filename}"
def playwright_fill_input(page: Page, type: str, text: str, input: str) -> None:
if type:
locator_type = _get_locator_type(type)
element = getattr(page, locator_type)(text)
else:
element = page.locator(text)
element.fill(input)
def playwright_tick_checkbox(page: Page, type: str, text: str, input: str) -> None:
if type:
locator_type = _get_locator_type(type)
element = getattr(page, locator_type)(text)
else:
element = page.locator(text)
element.check()
def playwright_select_dropdown(page: Page, type: str, text: str, input: str) -> None:
if type:
locator_type = _get_locator_type(type)
element = getattr(page, locator_type)(text)
else:
element = page.locator(text)
element.select_option(input)
def _get_locator_type(type) -> str:
return CONSTANT_LOCATOR_TYPES[type]