Robotic automation promises to achieve the following goals.
- The repeated task with consistent accuracy and quality
- Perform multiple tasks parallelly
- Connect to legacy system via APIs.
- Scalable processing.
This post aims to provide understandings of good practices to build an automation pipeline, using the open-source library, PyAutoGUI. To learn, we will make automation to draw a diagram on MS-Paint. The diagram needs to be highly symmetrical which would be difficult to be drawn by a human.
Let’s understand the trade-off first. While designing a resilient system we should also consider the weakness of robots. Robot lacks the IQ as compared to humans. They lack the judgment to react to unseen scenarios. We need to assume that you are giving instructions to that no IQ bot sitting in front of the computer.
Bots don’t know how to deal with an unknown scenario. They don’t even know if they did something wrong.
Good practice to design an RPA (robotic process automation)
- Limit the task length and break it into series of smaller preemptable tasks.
- Smaller decoupled tasks will increase parallelism.
- Set expectations rigid. Teaching bots about right or wrong is a long term investment. Instead, assert the task.
- Keep frequent check-point, keep exporting and saving data after each data modification.
Deterministic UI interactions
- Prefer to use tab-sequence instead of click. Clicking buttons will require to have bookkeeping of coordinates which are more prone to Err.
- Use short-keys and function-keys, for the above-mentioned reasons.
Consume UI feedback
- Instead of waiting for a specific hard-coded time task post every action (click, key-stroke), wait till the feedback in UI like the opening/closing of window or dialog box.
Hence, Avoid the use time.sleep
- Use active window title as an assertion for flow. For example, in most software
ctrl+sshould open window with title Save As.
# Bad practise: since task can take more than 120 seconds.
time.sleep(120)# Good practise: time.sleep till active window gets "Save As"
Consider external factors
Have a comprehensive list of all factors which can affect automation. Make sure the structure has agility for the following factors.
- Change in UI
- Change in tab sequences
- Any window interruptions
Encapsulation class for PyAutoGUI library. Encapsulation makes the dependency on third-party libraries lose.
Context-aware implementation for the automation of the app. Store business logic to perform the app-related tasks.
The main caller of the app module could be either the consumer, worker-pool or as micro-service depending on the infra design.
Connecting with a scalable architecture
The automation tasks are demanding for time and resources. We need the pool of independent VMs self-distributing load to achieve maximum productivity. Each Window VM should be responsible to consume the next task from the queue.
I would recommend starting the RPA project on an open-source library, get a rigid understanding of the feasibility and expectation. Avoid getting dependent on 3rd party vendors like UI-path without having the metric goals.
Read about the resource and code as:
- Find the complete project on GitHub: https://github.com/arinkverma/rpa-draw
- Read about PyAutoGUI, automation kit for Windows: https://pyautogui.readthedocs.io/en/latest/mouse.html