-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
executable file
·90 lines (77 loc) · 3.04 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
86
87
88
89
90
import os
from pathlib import Path
import requests
from robocorp import browser
from robocorp.tasks import task
from RPA.Excel.Files import Files as Excel
FILE_NAME = "challenge.xlsx"
EXCEL_URL = f"https://rpachallenge.com/assets/downloadFiles/{FILE_NAME}"
OUTPUT_DIR = Path(os.getenv("ROBOT_ARTIFACTS", "output"))
@task
def solve_challenge():
"""
Main task which solves the RPA challenge!
Downloads the source data Excel file and uses Playwright to fill the entries inside
rpachallenge.com.
"""
browser.configure(
browser_engine="chromium",
screenshot="only-on-failure",
headless=True,
)
try:
# Reads a table from an Excel file hosted online.
excel_file = download_file(
EXCEL_URL, target_dir=OUTPUT_DIR, target_filename=FILE_NAME
)
excel = Excel()
excel.open_workbook(excel_file)
rows = excel.read_worksheet_as_table("Sheet1", header=True)
# Surf the automation challenge website and fill in information from the table
# extracted above.
page = browser.goto("https://rpachallenge.com/")
page.click("button:text('Start')")
for row in rows:
fill_and_submit_form(row, page=page)
element = page.locator("css=div.congratulations")
browser.screenshot(element)
finally:
# A place for teardown and cleanups. (Playwright handles browser closing)
print("Automation finished!")
def download_file(url: str, *, target_dir: Path, target_filename: str) -> Path:
"""
Downloads a file from the given URL into a custom folder & name.
Args:
url: The target URL from which we'll download the file.
target_dir: The destination directory in which we'll place the file.
target_filename: The local file name inside which the content gets saved.
Returns:
Path: A Path object pointing to the downloaded file.
"""
# Obtain the content of the file hosted online.
response = requests.get(url)
response.raise_for_status() # this will raise an exception if the request fails
# Write the content of the request response to the target file.
target_dir.mkdir(exist_ok=True)
local_file = target_dir / target_filename
local_file.write_bytes(response.content)
return local_file
def fill_and_submit_form(row: dict, *, page: browser.Page):
"""
Fills a single form with the information of a single row from the table.
Args:
row: One row from the generated table out of the input Excel file.
page: The page object over which the browser interactions are done.
"""
field_data_map = {
"labelFirstName": "First Name",
"labelLastName": "Last Name",
"labelCompanyName": "Company Name",
"labelRole": "Role in Company",
"labelAddress": "Address",
"labelEmail": "Email",
"labelPhone": "Phone Number",
}
for field, key in field_data_map.items():
page.fill(f"//input[@ng-reflect-name='{field}']", str(row[key]))
page.click("input:text('Submit')")