forked from RedHatQE/widgetastic.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_script.py
More file actions
86 lines (65 loc) · 3 KB
/
Copy pathfirst_script.py
File metadata and controls
86 lines (65 loc) · 3 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Complete First Script Example
This is a complete, working example that demonstrates core widgetastic concepts.
"""
# first_script.py
import json
import os
from pathlib import Path
from playwright.sync_api import sync_playwright
from widgetastic.browser import Browser
from widgetastic.widget import View, Text, TextInput, Checkbox
# Define your widgets and views i.e. Modeling of the testing page.
class DemoFormView(View):
# Define the form elements as widgets
custname = TextInput(locator='.//input[@name="custname"]')
telephone = TextInput(locator='.//input[@name="custtel"]')
email = TextInput(locator='.//input[@name="custemail"]')
@View.nested
class pizza_size(View): # noqa
small = Checkbox(locator=".//input[@value='small']")
medium = Checkbox(locator=".//input[@value='medium']")
large = Checkbox(locator=".//input[@value='large']")
@View.nested
class pizza_toppings(View): # noqa
bacon = Checkbox(locator=".//input[@value='bacon']")
extra_cheese = Checkbox(locator=".//input[@value='cheese']")
onion = Checkbox(locator=".//input[@value='onion']")
mushroom = Checkbox(locator=".//input[@value='mushroom']")
delivery_instructions = TextInput(locator='.//textarea[@name="comments"]')
submit_order = Text(".//button[text()='Submit order']")
response = Text(
".//body"
) # After submitting the form, we will get the response in the body of the page.
# Step: Main automation logic where actualy we are interacting with the page.
def main():
# Get headless mode from environment (set by conftest or CI)
headless = os.getenv("PLAYWRIGHT_HEADLESS", "false").lower() == "true"
# Use a local HTML form to avoid any external network dependency.
form_path = Path(__file__).parent / "pizza_form.html"
form_url = form_path.resolve().as_uri()
with sync_playwright() as playwright:
# Launch browser using Playwright
browser = playwright.chromium.launch(headless=headless)
# Tip: Use slow_mo for debugging: browser = playwright.chromium.launch(headless=False, slow_mo=500)
page = browser.new_page()
# Create widgetastic browser instance
wt_browser = Browser(page)
# Navigate to the local pizza order form.
wt_browser.url = form_url
# Initialize the view i.e. Model of the testing page.
form_view = DemoFormView(wt_browser)
# Fill individual fields
form_view.custname.fill("John Doe")
form_view.telephone.fill("1234567890")
form_view.email.fill("john.doe@example.com")
form_view.pizza_size.small.fill(True)
form_view.pizza_toppings.bacon.fill(True)
form_view.delivery_instructions.fill("Hello from Widgetastic!")
form_view.submit_order.click()
response_data = json.loads(form_view.response.text)
print("Response data:")
print(json.dumps(response_data, indent=4))
# Close the browser
browser.close()
if __name__ == "__main__":
main()