-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
executable file
·135 lines (114 loc) · 4.23 KB
/
Copy pathtasks.py
File metadata and controls
executable file
·135 lines (114 loc) · 4.23 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from robocorp.tasks import task
from robocorp import browser
from RPA.HTTP import HTTP
from RPA.Tables import Tables
from RPA.PDF import PDF
from RPA.Archive import Archive
@task
def order_robots_from_RobotSpareBin():
browser.configure( slowmo=230, )
"""
- Orders robots from RobotSpareBin Industries Inc.
- Saves the order HTML receipt as a PDF file.
- Saves the screenshot of the ordered robot.
- Embeds the screenshot of the robot to the PDF receipt.
- Creates ZIP archive of the receipts and the images.
"""
open_robot_order_website()
close_annoying_modal()
orders = get_orders()
i = 0
for row in orders:
fill_the_form_and_create_pdf(row)
i += 1
if i < (int(orders.size)) :
order_new_robot()
archive_receipts()
print("Finished : order_robots_from_RobotSpareBin()")
def open_robot_order_website():
"""
- Navigates to the given URL
"""
browser.goto("https://robotsparebinindustries.com/#/robot-order")
def get_orders():
"""
- Downloads the orders file, read it as a table, and return the result
"""
http = HTTP()
http.download(url="https://robotsparebinindustries.com/orders.csv", overwrite=True)
library = Tables()
orders = library.read_table_from_csv(
"orders.csv", columns=["Order number","Head","Body","Legs","Address"])
print("### Found Table Colums / Rows : " + str(orders.columns) + " / " + str(orders.size))
return orders
def fill_the_form_and_create_pdf(row):
"""
- Fills in the order data and click the 'ORDER' button
- Troubleshoots danger alerts
- Embeds screenshots to recipes and creates final PDF
"""
order_number = row['Order number']
page = browser.page()
page.select_option("#head", row['Head'])
page.set_checked("#id-body-" + row['Body'], True)
page.get_by_placeholder("Enter the part number for the legs").fill(row['Legs'])
page.fill("#address", row['Address'])
page.click("#order")
alert_danger = page.locator("[class='alert alert-danger']").is_visible()
if alert_danger :
print ("Starting reordering max 10 times for Order Nr : " + order_number)
i = 0
while (alert_danger and i<10):
i += 1
page = browser.page()
page.click("#order")
alert_danger = page.locator("[class='alert alert-danger']").is_visible()
if i==9 :
print ("Reordering for Order Nr " + order_number + " NOT POSSIBLE!")
else :
alert_danger = False
if not alert_danger :
embed_screenshot_to_receipt(screenshot_robot(order_number), store_receipt_as_pdf(order_number))
def store_receipt_as_pdf(order_number):
"""
- Takes an argument (the order number - to be used in the file name to ensure a unique name)
- Returns a result (the file system path to the PDF file)
"""
page = browser.page()
receipt_path = f"output/receipts/receipt_f_order_{order_number}.pdf"
PDF().html_to_pdf(page.locator("#receipt").inner_html(), receipt_path)
return receipt_path
def screenshot_robot(order_number):
"""
- Takes an argument (the order number - to be used in the file name to ensure a unique name)
- Returns a result (the file system path to the PNG file)
"""
page = browser.page()
png_path = f"output/screenshots/pic_f_order_{order_number}.png"
page.locator("#robot-preview-image").screenshot(path=png_path)
return png_path
def embed_screenshot_to_receipt(screenshot, pdf_file):
"""
- Embeds the robot screenshot to the receipt PDF file
"""
pdf = PDF()
pdf.add_watermark_image_to_pdf(image_path=screenshot, source_path=pdf_file, output_path=pdf_file)
def order_new_robot():
"""
- Preps the page for new robot ordering
"""
page = browser.page()
page.click("#order-another")
close_annoying_modal()
def archive_receipts():
"""
- Archives all order receipts
"""
ziparchive = Archive()
ziparchive.archive_folder_with_zip("./output/receipts", "./output/receipts.zip")
def close_annoying_modal():
"""
- Gets rid of that annoying modal that pops up when you open the robot order website
"""
page = browser.page()
page.click("button:text('Yep')")