You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced.rst 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. Advanced concepts
  2. ===================
  3. Saving run results of flow
  4. ----------------------------
  5. You can save an html log of the run and the flow run results to ``tagui/src/tagui_report.csv`` with the ``-report`` option (shortcut ``-r``). ::
  6. tagui my_flow.tag -report
  7. The CSV file will show one line for each run, when it started, how long it took to complete, any error message during run, the link to the log file for that run, and the user's workgroup\\userid.
  8. Handling exceptions and errors
  9. --------------------------------
  10. There are 3 ways to handle exceptions in TagUI when things do not go as planned.
  11. The first way is **local error handling**. This means using if conditions to check specifically for certain scenarios and handling the scenarios accordingly. For example, check if some UI element is missing, then do xyz steps. Using this way, a workflow can have multiple fine-grain exception handling.
  12. The second way is **workflow error handling**. A workflow can be chained as follows to handle error or success accordingly. The workflow error.tag will run only if flow.tag errors out. The workflow success.tag will run only if flow.tag runs successfully. TagUI will automatically throw error when it detects an expected UI element missing (and autosave screenshot) or some other unknown errors.
  13. Windows example from the command prompt::
  14. call tagui flow.tag || tagui error.tag
  15. call tagui flow.tag && tagui success.tag
  16. macOS / Linux example from the terminal::
  17. tagui flow.tag || tagui error.tag
  18. tagui flow.tag && tagui success.tag
  19. The third way is **global error handling**. Configuration can be done for TagUI such that after every run, special handling is done to send data or files generated from the report option to some target folder or API endpoint for error / success handling. For example, syncing all automation runs to central storage for auditing purpose. The special handling applies to all TagUI flows that are run.
  20. .. _datatables:
  21. Datatables for batch automation
  22. -------------------------------
  23. Datatables are :ref:`csv files <what-are-csv-files>` which can be used to run your flows multiple times with different inputs.
  24. A datatable (``trade_data.csv``) could look like this:
  25. = ============ ============= ======== ====== ====== =========
  26. # trade username password pair size direction
  27. - ------------ ------------- -------- ------ ------ ---------
  28. 1 Trade USDSGD test_account 12345678 USDSGD 10000 BUY
  29. 2 Trade USDSGD test_account 12345678 USDJPY 1000 SELL
  30. 3 Trade EURUSD test_account 12345678 EURUSD 100000 BUY
  31. = ============ ============= ======== ====== ====== =========
  32. To use it, you run your flow with ``tagui my_flow.tag trade_data.csv``. TagUI will run ``my_flow.tag`` once for each row in the datatable (except for the header).
  33. Within the flow, TagUI can use the variables ``trade``, ``username``, ``password``, etc as if they were in the :ref:`local object repository <object-repository>` and the values will be from that run's row.
  34. To know which iteration your flow is in you can use the ``iteration`` variable::
  35. echo current iteration: `iteration`
  36. if iteration equals to 1
  37. // go to login URL and do the login steps
  38. www.xero.com
  39. // do rest of the steps for every iteration
  40. .. _object-repository:
  41. Object repositories for reusability
  42. -----------------------------------
  43. Object repositories are optional :ref:`csv files <what-are-csv-files>` which can store variables for use in flows. They help to separate your flows from your personal data (like login information for web flows), and allow you to share common information between multiple flows for easy updating.
  44. Each flow has a **local object repository** and all flows share the **global object repository**. The local object repository is the ``tagui_local.csv`` in the same folder as the flow. The global object repository is the ``tagui_global.csv`` in the ``tagui/src/`` folder.
  45. An object repository could look like this:
  46. ============== =================================
  47. object definition
  48. -------------- ---------------------------------
  49. email user-email-textbox
  50. create account btn btn--green btn-xl signup-btn
  51. ============== =================================
  52. Within the flow, TagUI can use the objects ``email``, ``create account`` as variables and they will be replaced directly by the definitions before it is run. Local definitions take precedence over global definitions.
  53. If ``user-email-textbox`` was the identifier for some web text input, then you could use the following in your flow::
  54. type `email` as my_email@email.com
  55. Running flows within a flow
  56. -----------------------------
  57. You can modularise your RPA workflows by breaking a large workflow file into many subflow files. For more complex RPA scenarios, you can even let a subflow run other subflows.
  58. Some common reasons for doing that include the convenience of reusing the same subflow in other flows, doing something specific which is easier to organise by keeping the sequence of steps in a subflow, or storing your Python or JavaScript code and functions in separate subflows (using py begin and py finish code blocks for example).
  59. A flow can run another flow, like this::
  60. tagui login_crm.tag
  61. Flows can also be stored in subfolders::
  62. // Windows example
  63. tagui CRM\login.tag
  64. // Mac/Linux example
  65. tagui CRM/login.tag
  66. Variables in the parent flow are accessible in the child flow and vice versa::
  67. // in this case, username and password variables are available in login.tag
  68. username = 'jennifer'; password = '12345678';
  69. tagui login.tag
  70. // you can also define variables on separate lines instead of all in 1 line
  71. username = 'jennifer'
  72. password = '12345678'
  73. tagui login.tag
  74. // in login.tag you can define and return variables for its parent to use
  75. echo `login_result`
  76. You can even combine multiple sequences of steps into one subflow as follows. By designing a subflow this way, you can assign the variable ``action = 'login'`` in the parent flow to determine which sequence of steps gets executed when the subflow is called with ``tagui`` step::
  77. // crm_steps.tag
  78. if action equals to 'login'
  79. do some steps
  80. do some more steps
  81. else if action equals to 'report'
  82. do some steps
  83. do some more steps
  84. else if action equals to 'logout'
  85. do some steps
  86. do some more steps
  87. else
  88. echo ERROR - action undefined
  89. Turbo mode to run 10X faster
  90. -------------------------------
  91. To run TagUI with turbo option (use with caution)::
  92. tagui flow.tag -turbo
  93. or
  94. tagui flow.tag -t
  95. Most websites and desktop apps are not designed for the super-human speed user. If your RPA runs at a speed beyond what those websites are designed and tested for, you are surely going to run into problems with some apps. Problems could be fields and data not filling up properly, not triggering expected validations, form submissions with missing data, account being blocked etc.
  96. And the problems might happen randomly, including working on your PC but not working on another PC due to difference in CPU speed. Because of this, using turbo mode option is not recommended. You may save some cheap computer time, but if something is broken or does not work, you may end up spending expensive human time (your time) to troubleshoot or fix.
  97. However, this is very useful for some users for some specific scenarios. For eg, data collection from apps, data entry in web applications that can handle super-human speed reliably, as part of a chatbot doing backend RPA for user, for fast and rapid prototyping, perhaps taking part in RPA competitions and hackathons etc. Thoroughly test for your use case before using!
  98. Visual automation tricks
  99. ------------------------------------
  100. For many steps, you can end the step with ``using ocr`` or ``using OCR`` to tell TagUI to interact on some UI element on the screen using OCR (optical character recognition). See the examples below. Steps which this can be done: click, rclick, dclick, hover, type, select, read, snap, exist(), present().
  101. .. code-block:: none
  102. click Submit using ocr
  103. if exist('Special Offer using ocr')
  104. click Add To Cart using OCR
  105. // various usage combinations for select step
  106. select Dress Color using OCR as Dark Blue using OCR
  107. select dress_color.png as Bright Pink using ocr
  108. select Dress Color using OCR as dark_black.png
  109. select dress_color.png as bright_white.png
  110. If you make the background of a UI element in a ``.png`` file 100% transparent using an image editor, TagUI will be able to target the element regardless of its background.
  111. Conversely, you can also remove the foreground content near some anchor element like a frame, to allow you to OCR varying content in the empty area using the **read** step.
  112. .. _python:
  113. Writing Python within flows
  114. --------------------------------
  115. You can write Python code in TagUI flows. Python needs to be `installed separately <https://www.python.org/downloads/>`_.
  116. The ``py`` step can be used to run commands in Python (TagUI will call ``python`` on the command line). You can pass string values back to TagUI with `print()`. The ``stdout`` will be stored in the ``py_result`` variable in TagUI.
  117. .. code-block:: none
  118. py a=1
  119. py b=2
  120. py c=a+b
  121. py print(c)
  122. echo `py_result`
  123. You can also use ``py begin`` and ``py finish`` before and after a Python code block::
  124. py begin
  125. a=1
  126. b=2
  127. c=a+b
  128. print(c)
  129. py finish
  130. echo `py_result`
  131. You can pass a variable to Python like this::
  132. phone = 1234567
  133. py_step('phone = ' + phone)
  134. py print(phone)
  135. echo `py_result`
  136. name = 'Donald'
  137. py_step('name = "' + name + '"')
  138. py print(name)
  139. echo `py_result`
  140. To pass and return more complex data, for example multiple variables, you can use JavaScript and Python JSON libraries to send and receive back JSON strings. `See an example here <https://github.com/kelaberetiv/TagUI/issues/898#issuecomment-752833953>`_ of passing 2 variables, doing some processing, and returning 2 variables.
  141. Create log files for debugging
  142. ---------------------------------
  143. To do advanced debugging, you can create log files when running flows by creating an empty ``tagui_logging`` file in ``tagui/src/``.
  144. - ``my_flow.log`` stores step-by-step output of the execution.
  145. - ``my_flow.js`` is the generated JavaScript file that was run.
  146. - ``my_flow.raw`` is the expanded flow after parsing modules.
  147. Running TagUI on the cloud
  148. -----------------------------
  149. For cloud lovers, you can run TagUI on your web browser or phone using `free Google Cloud <https://github.com/kelaberetiv/TagUI/issues/913>`_. You can run up to 5 sessions concurrently on different tabs of your browser.
  150. For more control running on the cloud, you can run this `Docker image <https://hub.docker.com/r/openiap/nodered-tagui>`_ (use edge tag for the latest version) or `Docker file <https://github.com/open-rpa/openflow/blob/master/OpenFlowNodeRED/Dockerfiletagui>`_ on your preferred cloud vendor.
  151. Or run on `free Node-RED instance <https://app.openiap.io/>`_ on OpenFlow. The Docker image, Docker file and OpenFlow cloud are maintained and sponsored by `Allan Zimmermann <https://www.linkedin.com/in/skadefro/>`_.