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.

main_concepts.rst 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. Main concepts
  2. ====================
  3. Flows
  4. --------------
  5. TagUI automates your actions by running *flows*, which are text files with ``.tag`` file extension.
  6. You can run a flow in the :ref:`Command Prompt/Terminal<how-to-use-command-prompt>` like this::
  7. tagui my_flow.tag
  8. .. raw:: html
  9. <video playsinline autoplay muted loop width="100%">
  10. <source src="./_static/run-a-flow.mp4" type="video/mp4">
  11. Your browser does not support the video tag.
  12. </video>
  13. |
  14. TagUI looks for ``my_flow.tag`` in your current working directory. You can also provide the full path::
  15. tagui c:\tagui\samples\1_google.tag
  16. You can also :ref:`run flows on a fixed schedule <run-on-schedule>`.
  17. Run by double-click
  18. **********************
  19. You can create a shortcut file with ``-deploy``::
  20. tagui my_flow.tag -deploy
  21. or using the shortcut ``-d``::
  22. tagui my_flow.tag -d
  23. This creates a shortcut (my_flow.cmd) to run your flow just by double clicking the shortcut. The shortcut is in the same folder as your flow, but you can move it to your desktop or anywhere else.
  24. .. raw:: html
  25. <video playsinline autoplay muted loop width="100%">
  26. <source src="./_static/deploy-a-flow.mp4" type="video/mp4">
  27. Your browser does not support the video tag.
  28. </video>
  29. |
  30. If you want to create the shortcut with options like ``-headless`` (``-h``), you can include them::
  31. tagui my_flow.tag -h -d
  32. .. note:: If you move your flow file to another folder, you will need to create a new shortcut file.
  33. Run from a URL
  34. **********************
  35. You can also run a flow directly from a URL::
  36. tagui https://raw.githubusercontent.com/kelaberetiv/TagUI/master/flows/samples/1_google.tag
  37. Hide the browser
  38. **********************
  39. You can run web flows without showing web browser by running TagUI with ``-headless`` option. ::
  40. tagui my_flow.tag -headless
  41. or using the shortcut ``-h``::
  42. tagui my_flow.tag -h
  43. This allows you to continue using your desktop normally while the flow is running, but it will not work if your flow uses visual automation, because it reads and clicks what is on your screen.
  44. .. raw:: html
  45. <video playsinline autoplay muted loop width="100%">
  46. <source src="./_static/run-headless-flow.mp4" type="video/mp4">
  47. Your browser does not support the video tag.
  48. </video>
  49. |
  50. Steps
  51. ---------
  52. Flows are made out of *steps*. Below are some common steps.
  53. You can see the full list of steps in the :ref:`steps reference <step-reference>`.
  54. click
  55. **********
  56. One of the most common steps is click. You can use it to click on a web element:
  57. .. code-block:: none
  58. click Getting started
  59. This command tells TagUI to try to click on any element which has “Getting started” as its “id”, “name”, “class” or “title” attributes (:ref:`How to find an element’s attributes <element_attributes>`), or as a last resort, has “Getting started” in its text.
  60. This method usually works for targeting what you want, but you can be more explicit by providing an XPath. XPath is a powerful way to choose which web element you want to target. For example:
  61. .. code-block:: none
  62. click //a[@class="icon icon-home"]
  63. You can also click on a certain point on your screen:
  64. .. code-block:: none
  65. click (500,300)
  66. Here, 500 and 300 are x-y coordinates. This command clicks on a point which is 500 pixels from the left of your screen and 300 pixels from the top of your screen. A good way to discover which coordinates to input is to use the ``mouse_xy()`` :ref:`helper function <helper-functions>` in live mode.
  67. Lastly, you can use visual automation to click where it matches a previously saved image. This command looks for button.png in the same folder as your flow, then looks for a similar image on your screen, and clicks it:
  68. .. code-block:: none
  69. click button.png
  70. It’s often a good idea to keep your flows and images organised. You can create a folder (eg. named images) for your images and use the image like this instead:
  71. .. code-block:: none
  72. click image/button.png
  73. visit
  74. **********
  75. You can visit a webpage simply by entering the url:
  76. .. code-block:: none
  77. https://somewebsite.com
  78. type
  79. **********
  80. You can type into web inputs. This command finds the element “some-input” in the same way as for the **click** step and types “some-text” into it:
  81. .. code-block:: none
  82. type some-input as some-text
  83. You can use [clear] to clear the input and [enter] to hit the Enter key:
  84. .. code-block:: none
  85. type some-input as [clear]some-text[enter]
  86. You can also use an image as the target, just like with the **click** step:
  87. .. code-block:: none
  88. type some-input.png as some-text
  89. read
  90. **********
  91. The **read** step allows you to save text from web elements or from the screen into a variable.
  92. This command finds the element “some-element” and saves its value into a variable called “some-variable”:
  93. .. code-block:: none
  94. read some-element to some-variable
  95. **read** can also use visual automation and OCR to read text from a region of your screen. The output from this may not be completely accurate as it relies on OCR.
  96. This command reads all the text in the rectangle formed between the points (300,400) and (500,550):
  97. .. code-block:: none
  98. read (300,400)-(500,550) to some-variable
  99. You can also use XPath to read some attribute values from web elements. This command reads the id attribute from the element:
  100. .. code-block:: none
  101. read //some-element/@some-attribute to some-variable
  102. assign
  103. **********
  104. You can assign values into variables. This makes them easier to reference and work with.
  105. This example uses the ``count()`` :ref:`helper function <helper-functions>`, counts the number of elements found with id/name/text with ‘row’ in them and assigns it to a variable ``row_count`` for later use:
  106. .. code-block:: none
  107. row_count = count('row')
  108. Identifiers
  109. ---------------
  110. You have probably noticed that different steps have different ways that they target elements, called **identifiers**. Let's look at the different types of identifiers.
  111. .. note:: The DOM and XPath identifiers only work for Chrome/Edge. To automate other browsers, use the Point/Region and Image identifiers.
  112. .. _dom:
  113. DOM
  114. **********
  115. .. code-block:: none
  116. click Getting started
  117. This matches an element in the DOM (Document Object Model) of the web page, matching either the :ref:`id, name, class attributes <element_attributes>` or the text of the element itself.
  118. .. _xpath:
  119. XPath
  120. **********
  121. .. code-block:: none
  122. click //body/div[1]/nav/div/div[1]/a
  123. This matches the :ref:`XPath <find-xpath>` of an element in the web page.
  124. It is a more explicit and powerful way of targeting web elements.
  125. .. note:: You can use CSS selectors too in place of XPath, but XPath is preferred.
  126. .. _point:
  127. Point
  128. **********
  129. .. code-block:: none
  130. click (200,500)
  131. This matches the point on the screen 200 pixels from the left of the screen and 500 pixels from the top of the screen. This uses *visual automation*.
  132. .. _region:
  133. Region
  134. **********
  135. .. code-block:: none
  136. read (300,400)-(500,550) to some-variable
  137. This matches the rectangle formed between the two points (300,400) and (500,550). See :ref:`Point <point>`. This uses *visual automation*.
  138. .. _image:
  139. Image
  140. **********
  141. .. code-block:: none
  142. click button.png
  143. This matches any area on the screen that looks similar to an image file ``button.png`` (in the folder of the flow). You first need to take a screenshot of ``button.png``. This uses *visual automation*.
  144. .. code-block:: none
  145. click image/button.png
  146. This allows you to look for ``button.png`` within the ``image`` folder.
  147. .. _live-mode:
  148. Live mode
  149. ---------------
  150. We recommend using live mode when you want to write your own flows or try out some step. In :ref:`Command Prompt/Terminal <how-to-use-command-prompt>`::
  151. tagui live
  152. This starts up a live session, where you can run steps one line at a time and immediately see the output. Within a TagUI flow, you can also use ``live`` step to pause execution and enter live mode.
  153. .. raw:: html
  154. <video playsinline autoplay muted loop width="100%">
  155. <source src="./_static/live-mode.mp4" type="video/mp4">
  156. Your browser does not support the video tag.
  157. </video>
  158. |
  159. .. _if-conditions:
  160. If conditions
  161. ---------------
  162. You may want your flow to do something different depending on some factors. You can use an if condition to do this.
  163. For example, if the URL contains the word “success”, then we want to click some buttons:
  164. .. code-block:: none
  165. if url() contains "success"
  166. click button1.png
  167. click button2.png
  168. ``url()`` is a :ref:`helper function <helper-functions>` that gets the url of the current webpage. Note that the other lines are *indented*, ie. there are spaces (or tabs) in front of them. This means that they are in the *if block*. The steps in the *if block* will only be run if the condition is met, ie. the url contains the word “success”.
  169. .. note::
  170. Before v6, you need to use ``{`` and ``}`` to surround your *if block*.
  171. From v6 onwards, the curly braces ``{}`` are optional.
  172. Another common case is to check if some element exists. Here, we say that “if some-element doesn’t appear after waiting for the timeout, then visit this webpage”.
  173. .. code-block:: none
  174. if !exist('some-element')
  175. https://tagui.readthedocs.io/
  176. The ! negates the condition and comes from JavaScript, which TagUI code eventually translates to.
  177. In below example, we check if a variable row_count, which we assigned a value earlier, is equal to 5:
  178. .. code-block:: none
  179. if row_count equals to 5
  180. some steps
  181. Here’s how we check if it is more than or less than 5:
  182. .. code-block:: none
  183. if row_count more than 5
  184. some steps
  185. .. code-block:: none
  186. if row_count less than 5
  187. some steps
  188. .. _for-loops:
  189. For loops
  190. -----------
  191. You can use loops to do the same thing many times within the same flow. In order to run one flow many times with different variables, the standard way is to use :ref:`datatables <datatables>`.
  192. In this example, we repeat the steps within the block for a total of 20 times:
  193. .. code-block:: none
  194. for n from 1 to 20
  195. some step to take
  196. some other step
  197. some more step
  198. .. _helper-functions:
  199. Helper functions
  200. ---------------------
  201. Helper functions are useful JavaScript functions which can get values to use in your steps.
  202. Each helper function is followed by brackets ``()``. Some helper functions take inputs within these brackets. You can see the full list of helper functions in the :ref:`helper functions reference <helper-functions-reference>`.
  203. csv_row()
  204. *********************
  205. Turns some variables into csv text for writing to a csv file. It takes variables as its input, surrounded by square brackets ``[]`` (which is actually a JavaScript array).
  206. .. code-block:: none
  207. read name_element to name
  208. read price_element to price
  209. read details_element to details
  210. write `csv_row([name, price, details])` to product_list.csv
  211. clipboard()
  212. *********************
  213. Gets text from the clipboard::
  214. dclick pdf_document.png
  215. wait 3 seconds
  216. keyboard [ctrl]a
  217. keyboard [ctrl]c
  218. text_contents = clipboard()
  219. You can also give it an input, which puts the input *onto* the clipboard instead. This can be useful for pasting large amounts of text directly, which is faster than using the **type** step::
  220. long_text = "This is a very long text which takes a long time to type"
  221. clipboard(long_text)
  222. click text_input
  223. keyboard [ctrl]v
  224. keyboard [enter]
  225. mouse_x(), mouse_y()
  226. *********************
  227. Gets the mouse's x or y coordinates.
  228. This is useful for modifying x or y coordinates with numbers for using in steps like ``read`` and ``click``. The example below clicks 200 pixels to the right of ``element.png``::
  229. hover element.png
  230. x = mouse_x() + 200
  231. y = mouse_y()
  232. click (`x`,`y`)
  233. mouse_xy()
  234. *********************
  235. In live mode, you can use find out the coordinates of your mouse using ``echo `mouse_xy()``` so that you can use the coordinates in your flows::
  236. echo `mouse_xy()`