10000 Refactor: Use RPA.Windows · robocorp/example-windows-notepad@e50d198 · GitHub
[go: up one dir, main page]

Skip to content

Commit e50d198

Browse files
committed
Refactor: Use RPA.Windows
1 parent 33d5dc1 commit e50d198

File tree

6 files changed

+84
-99
lines changed

6 files changed

+84
-99
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# example-windows-notepad
1+
# Windows Notepad automation
22

3-
Contains Robocorp Robot which automates Windows Notepad.
3+
Contains a Robocorp robot which automates Windows Notepad.
44

5-
**explanation.** _"Robocorp Robot"_ means directory structure which is runnable with **rcc** tool (link at the bottom)
5+
**Explanation.** _"Robocorp Robot"_ means a directory structure which is runnable with [RCC](https://github.com/robocorp/rcc).
66

77
## Robot steps
88

9-
1. open test.txt from current directory and expect that it is opened by Notepad (as .txt associated application)
10-
2. change font settings
11-
3. clear existing text
12-
4. copy text from clipboard into editor
13-
5. save and exit
9+
1. Open test.txt from current directory in Notepad.
10+
2. Change font settings.
11+
3. Clear existing text.
12+
4. Write text into the editor.
13+
5. Save and exit.
1414

1515
## Configuring run with environment variables
1616

17-
- **FILE_TO_OPEN** (absolute filepath to file, defaults to current directory's _test.txt_)
17+
- **FILE_TO_OPEN** (an absolute filepath to file, defaults to current directory's _test.txt_)
1818
- **NOTEPAD_FONT_NAME** (default with Python is _Comic Sans MS_ and with RFW _Times New roman_)
1919
- **NOTEPAD_FONT_SIZE** (default is _18_)
2020
- **NOTEPAD_FONT_STYLE** (default is _Regular_)

conda.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dependencies:
66
- pip=20.1
77
- pip:
88
- rpaframework==12.9.0
9+
- rpaframework-windows==2.3.2

notepad.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
11
import datetime
22
import os
3-
from RPA.Desktop.Windows import Windows
3+
from RPA.Windows import Windows
44

55
windows = Windows()
66

77

8-
def format_text(message:str):
9-
message = message.replace(" ", "{VK_SPACE}")
10-
message = message.replace("\n", "{ENTER}")
11-
return message
12-
8+
def open_notepad():
9+
default_test_file = f'{os.getcwd()}/test.txt'
10+
test_file = os.getenv("FILE_TO_OPEN", default_test_file)
11+
windows.windows_run(f'notepad.exe {test_file}')
12+
windows.control_window("type:WindowControl class:Notepad")
1313

1414
def change_font_settings():
15-
NOTEPAD_FONT_NAME = os.getenv("NOTEPAD_FONT_NAME", "Comic Sans MS")
16-
NOTEPAD_FONT_SIZE = os.getenv("NOTEPAD_FONT_SIZE", "18")
17-
NOTEPAD_FONT_STYLE = os.getenv("NOTEPAD_FONT_STYLE", "Regular")
18-
19-
NOTEPAD_FONT_NAME = format_text(NOTEPAD_FONT_NAME.strip().replace('"', ""))
20-
NOTEPAD_FONT_STYLE = format_text(NOTEPAD_FONT_STYLE.strip().replace('"', ""))
15+
open_font_settings()
16+
set_font()
17+
set_font_style()
18+
set_font_size()
19+
windows.click("id:1") # OK
2120

22-
windows.menu_select("Format->Font")
23-
windows.refresh_window()
24-
windows.mouse_click("name:'Font style:' and type:Edit")
25-
windows.send_keys(NOTEPAD_FONT_STYLE)
26-
windows.mouse_click("name:Size: and class:Edit")
27-
windows.send_keys(NOTEPAD_FONT_SIZE)
28-
windows.send_keys("%f" + NOTEPAD_FONT_NAME)
29-
windows.mouse_click("name:OK and type:Button")
30-
windows.wait_for_element("class:Edit")
21+
def open_font_settings():
22+
windows.click("type:MenuItemControl name:Format")
23+
windows.click("type:MenuItemControl name:Font...")
3124

25+
def set_font():
26+
windows.select("id:1136", os.getenv("NOTEPAD_FONT_NAME", "Comic Sans MS"))
3227

33-
def write_notepad_message(message):
34-
windows.type_into("class:Edit", format_text(message))
28+
def set_font_style():
29+
windows.select("id:1137", os.getenv("NOTEPAD_FONT_STYLE", "Regular"))
3530

31+
def set_font_size():
32+
windows.select("id:1138", os.getenv("NOTEPAD_FONT_SIZE", "12"))
3633

37-
def save_and_exit():
38-
windows.menu_select("File->Save")
39-
windows.menu_select("File->Exit")
34+
def write_text_to_editor(text):
35+
windows.send_keys("id:15", text)
4036

37+
def save():
38+
windows.send_keys(keys="{CTRL}s")
4139

42-
def notepad_task():
43-
FILE_TO_OPEN = os.getenv("FILE_TO_OPEN", "test.txt")
44-
windows.open_file(FILE_TO_OPEN, "Notepad", wildcard=True)
40+
def automate_notepad():
41+
open_notepad()
4542
change_font_settings()
46-
write_notepad_message("^a{VK_CLEAR}")
47-
windows.send_keys("^v{ENTER}")
48-
write_notepad_message("\nTimestamp: %s\n" % datetime.datetime.now())
49-
save_and_exit()
50-
43+
write_text_to_editor("{CTRL}a{CLEAR}") # Clear text
44+
write_text_to_editor(f'Timestamp: {datetime.datetime.now()}')
45+
save()
46+
windows.close_current_window()
5147

5248
if __name__ == "__main__":
53-
notepad_task()
49+
automate_notepad()
50+

notepad.robot

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,49 @@
11
*** Settings ***
2-
Documentation Copy text from clipboard to Notepad and change font settings.
2+
Documentation Automate Notepad.
3+
... Expects Windows 10, English.
4+
Library RPA.Windows
35
Library String
4-
Library RPA.Desktop.Windows
5-
Library RPA.FileSystem
6+
Task Teardown Close Current Window
67

7-
*** Keywords ***
8-
Format text
9-
[Arguments] ${text}
10-
${text}= Replace String ${text} ${SPACE} {VK_SPACE}
11-
${text}= Replace String ${text} \n {ENTER}
12-
[Return] ${text}
8+
*** Variables ***
9+
${DEFAULT_TEST_FILE}= ${CURDIR}${/}test.txt
10+
11+
*** Tasks ***
12+
Automate Notepad
13+
Open Notepad
14+
Change font settings
15+
Write text to editor {CTRL}a{CLEAR} # Clear text
16+
Write text to editor Timestamp: ${{ datetime.datetime.now() }}
17+
Save
1318

1419
*** Keywords ***
20+
Open Notepad
21+
Windows Run notepad.exe %{FILE_TO_OPEN=${DEFAULT_TEST_FILE}}
22+
Control Window type:WindowControl class:Notepad
23+
1524
Change font settings
16-
Set Task Variable ${fontname} %{NOTEPAD_FONT_NAME=Times New Roman}
17-
Set Task Variable ${fontsize} %{NOTEPAD_FONT_SIZE=12}
18-
Set Task Variable ${fontstyle} %{NOTEPAD_FONT_STYLE=Regular}
19-
Menu Select Format->Font
20-
Refresh Window # because UI changed
21-
Wait For Element name:\'Font style:\' and type:Edit
22-
Mouse Click name:\'Font style:\' and type:Edit
23-
Send Keys ${fontstyle}
24-
Mouse Click name:Size: and class:Edit
25-
Send Keys ${fontsize}
26-
${fontname}= Format Text ${fontname}
27-
Send Keys %f${fontname}
28-
Mouse Click name:OK and type:Button
29-
Wait For Element class:Edit
25+
Open font settings
26+
Set font
27+
Set font style
28+
Set font size
29+
Click id:1 # OK
3030

31-
*** Keywords ***
32-
Write Notepad message
33-
[Arguments] ${message}
34-
${message}= Format Text ${message}
35-
Type Into class:Edit ${message}
31+
Open font settings
32+
Click type:MenuItemControl name:Format
33+
Click type:MenuItemControl name:Font...
3634

37-
*** Keywords ***
38-
Save and exit
39-
Menu Select File->Save
40-
Sleep 1s # For demo purpose
41-
Menu Select File->Exit
42-
43-
Run teardown
44-
Run Keyword If Test Failed
45-
... Screenshot
46-
... ${OUTPUT_DIR}${/}fail.png
47-
... desktop=True
48-
Close All Applications
35+
Set font
36+
Select id:1136 %{NOTEPAD_FONT_NAME=Times New Roman}
4937

50-
*** Tasks ***
51-
Notepad Font menu
52-
Set Task Variable ${workfile} %{FILE_TO_OPEN=test.txt}
53-
Open File ${workfile} Notepad wildcard=True
54-
Change font settings
55-
Write Notepad message ^a{VK_CLEAR} # Clear Notepad editor
56-
Send Keys ^v{ENTER}
57-
Write Notepad message \nTimestamp: ${{ datetime.datetime.now() }}\n
58-
Screenshot output/success.png desktop=True
59-
Save and exit
60-
[Teardown] Run Teardown
38+
Set font style
39+
Select id:1137 %{NOTEPAD_FONT_STYLE=Regular}
40+
41+
Set font size
42+
Select id:1138 %{NOTEPAD_FONT_SIZE=12}
43+
44+
Write text to editor
45+
[Arguments] ${text}
46+
Send Keys id:15 ${text} # Text Editor
47+
48+
Save
49+
Send Keys keys={CTRL}s

robot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tasks:
22
Notepad with Robot Framework:
3-
robotTaskName: Notepad Font menu
3+
robotTaskName: Automate Notepad
44
Notepad with Python:
55
shell: python notepad.py
66

test.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Screenshot output/success.png desktop=True
2-
3-
Timestamp: 2021-11-30 19:55:23.874960
1+
Timestamp: 2022-03-17 15:20:38.101145

0 commit comments

Comments
 (0)
0