-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
85 lines (70 loc) · 2.93 KB
/
tasks.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
ORDER_WEBSITE_URL = "https://robotsparebinindustries.com/#/robot-order"
ORDERS_CSV_URL = "https://robotsparebinindustries.com/orders.csv"
OUTPUT_DIR = "output"
ZIP_FILE = "robot_orders.zip"
@task
def order_robots_from_RobotSpareBin():
"""Orders robots from RobotSpareBin Industries Inc"""
browser.configure(slowmo=100)
open_robot_order_website()
orders = get_orders()
for order in orders:
fill_the_form(order)
receipt_pdf = store_receipt_as_pdf(order["Order number"])
screenshot = screenshot_robot(order["Order number"])
embed_screenshot_to_receipt(screenshot, receipt_pdf)
archive_receipts()
def open_robot_order_website():
"""Navigates to the robot order website"""
browser.goto(ORDER_WEBSITE_URL)
def get_orders():
"""Downloads the orders CSV file and reads it as a table"""
http = HTTP()
http.download(url=ORDERS_CSV_URL, overwrite=True)
tables = Tables()
return tables.read_table_from_csv("orders.csv")
def fill_the_form(order_details):
"""Fills the order form with the given robot details"""
page = browser.page()
if page.locator("#order-another").is_visible():
page.click("#order-another")
close_annoying_modal()
page.select_option("#head", index=int(order_details["Head"]))
page.set_checked(f"#id-body-{order_details['Body']}", True)
page.fill("input[placeholder='Enter the part number for the legs']", str(order_details["Legs"]))
page.fill("#address", order_details["Address"])
page.click("button:text('Order')")
while page.locator(".alert-danger").is_visible():
page.click("button:text('Order')")
def store_receipt_as_pdf(order_number):
"""Saves the order HTML receipt as a PDF file."""
page = browser.page()
receipt_html = page.locator("#receipt").inner_html()
pdf = PDF()
pdf_path = f"{OUTPUT_DIR}/order_{order_number}.pdf"
pdf.html_to_pdf(receipt_html, pdf_path)
return pdf_path
def screenshot_robot(order_number):
"""Saves the screenshot of the ordered robot."""
page = browser.page()
screenshot_path = f"{OUTPUT_DIR}/order_{order_number}.png"
page.locator("#robot-preview-image").screenshot(path=screenshot_path)
return screenshot_path
def embed_screenshot_to_receipt(screenshot, pdf_file):
"""Embed the screenshot of the robot to the PDF receipt"""
pdf = PDF()
pdf.add_files_to_pdf([screenshot + ":orientation=P,align=center"], pdf_file, append=True)
def archive_receipts():
"""Creates ZIP archive of the receipts and the images"""
archive = Archive()
archive.archive_folder_with_zip(OUTPUT_DIR, ZIP_FILE, include="*.pdf")
def close_annoying_modal():
"""Closes the annoying modal that appears in robot order page"""
page = browser.page()
page.click("button:text('OK')")