field_actions.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FieldActions # rubocop:disable Metrics/ModuleLength
  3. delegate :app_host, to: Capybara
  4. # Check the field value of a form input field.
  5. #
  6. # @example
  7. # check_input_field_value('input_field_name', 'text', visible: :all)
  8. #
  9. def check_input_field_value(name, value, **find_options)
  10. input_field = find("input[name='#{name}']", **find_options)
  11. expect(input_field.value).to eq(value)
  12. end
  13. # Set the field value of a form input field.
  14. #
  15. # @example
  16. # set_input_field_value('input_field_name', 'text', visible: :all)
  17. #
  18. def set_input_field_value(name, value, **find_options)
  19. # input_field = find("input[name='#{name}']", **find_options)
  20. # expect(input_field.value).to eq(value)
  21. find("input[name='#{name}']", **find_options).fill_in with: value
  22. end
  23. # Set the field value of a form tree_select field.
  24. #
  25. # @example
  26. # set_tree_select_value('tree_select', 'Users') # via label
  27. # set_tree_select_value('tree_select', 1) # via value
  28. # set_tree_select_value('tree_select', 'Group 1 › Group 2 › Group 3') # via full path
  29. #
  30. def set_tree_select_value(name, value)
  31. tree_select_field = page.find(%( input[name='#{name}']+.js-input )) # search input
  32. .click # focus
  33. .ancestor('.controls', order: :reverse, match: :first) # find container
  34. # Try to find the option via its value.
  35. if tree_select_field.has_css?("[data-value='#{value}']", wait: false)
  36. tree_select_field.find("[data-value='#{value}']")
  37. .click
  38. return
  39. end
  40. # Try to find the option via its label.
  41. if tree_select_field.has_css?("[data-display-name='#{value}']", wait: false)
  42. tree_select_field.find("[data-display-name='#{value}']")
  43. .click
  44. return
  45. end
  46. path_delimiter = ' › '
  47. raise Capybara::ElementNotFound if !value.match(path_delimiter)
  48. components = value.split(path_delimiter)
  49. current_path = []
  50. # Try to drill down to a nested label.
  51. components.each_with_index do |component, index|
  52. current_path.push(component)
  53. display_name = current_path.join(path_delimiter)
  54. # Handle last item.
  55. if index == components.length - 1
  56. tree_select_field.find("[role='menu']:not(.velocity-animating) [data-display-name='#{display_name}']")
  57. .click
  58. # Handle parent items.
  59. else
  60. tree_select_field.find("[role='menu']:not(.velocity-animating) [data-display-name='#{display_name}'] .searchableSelect-option-arrow")
  61. .click
  62. end
  63. end
  64. end
  65. # Set the field value of a form external data source field.
  66. #
  67. # @example
  68. # set_external_data_source_value('external_data_source', '*', 'Users')
  69. #
  70. def set_external_data_source_value(name, search, value)
  71. input_elem = page.find(%( input[name*='#{name}']+.js-input ))
  72. input_elem.fill_in with: search, fill_options: { clear: :backspace }
  73. find('.js-optionsList span', text: value).click
  74. end
  75. # Check the field value of a form select field.
  76. #
  77. # @example
  78. # check_select_field_value('select_field_name', '1')
  79. #
  80. def check_select_field_value(name, value)
  81. select_field = find("select[name='#{name}']")
  82. expect(select_field.value).to eq(value)
  83. end
  84. # Set the field value of a form select field.
  85. #
  86. # @example
  87. # set_select_field_value('select_field_name', '1')
  88. #
  89. def set_select_field_value(name, value)
  90. find("select[name='#{name}'] option[value='#{value}']").select_option
  91. end
  92. # Set the value of a form select field via an option label.
  93. #
  94. # @example
  95. # set_select_field_label('select_field_name', 'A')
  96. #
  97. def set_select_field_label(name, label)
  98. find("select[name='#{name}']").select(label)
  99. end
  100. # Check the field value of a form tree select field.
  101. #
  102. # @example
  103. # check_tree_select_field_value('select_field_name', '1')
  104. #
  105. def check_tree_select_field_value(name, value)
  106. check_input_field_value(name, value, visible: :all)
  107. end
  108. # Check the field value of a form editor field.
  109. #
  110. # @example
  111. # check_editor_field_value('editor_field_name', 'plain text')
  112. #
  113. def check_editor_field_value(name, value)
  114. editor_field = find("[data-name='#{name}']")
  115. expect(editor_field.text).to have_text(value)
  116. end
  117. # Set the field value of a form editor field.
  118. #
  119. # @example
  120. # set_editor_field_value('editor_field_name', 'plain text')
  121. #
  122. def set_editor_field_value(name, value)
  123. find("[data-name='#{name}']").set(value)
  124. # Explicitly trigger the input event to mark the form field as "dirty".
  125. execute_script("$('[data-name=\"#{name}\"]').trigger('input')")
  126. end
  127. # Check the field value of a form date field.
  128. #
  129. # @example
  130. # check_date_field_value('date_field_name', '20/12/2020')
  131. #
  132. def check_date_field_value(name, value)
  133. date_attribute_field = find("div[data-name='#{name}'] input[data-item='date']")
  134. expect(date_attribute_field.value).to eq(value)
  135. end
  136. # Set the field value of a form date field.
  137. #
  138. # @example
  139. # set_date_field_value('date_field_name', '20/12/2020')
  140. #
  141. def set_date_field_value(name, value)
  142. # We need a special handling for a blank value, to trigger a correct update.
  143. if value.blank?
  144. find("div[data-name='#{name}'] input[data-item='date']").send_keys :backspace
  145. end
  146. find("div[data-name='#{name}'] .js-datepicker").fill_in with: value
  147. end
  148. # Check the field value of a form time field.
  149. #
  150. # @example
  151. # check_time_field_value('date_field_name', '08:00')
  152. #
  153. def check_time_field_value(name, value)
  154. date_attribute_field = find("div[data-name='#{name}'] input[data-item='time']")
  155. expect(date_attribute_field.value).to eq(value)
  156. end
  157. # Set the field value of a form time field.
  158. #
  159. # @example
  160. # set_time_field_value('date_field_name', '08:00')
  161. #
  162. def set_time_field_value(name, value)
  163. # We need a special handling for a blank value, to trigger a correct update.
  164. if value.blank?
  165. find("div[data-name='#{name}'] input[data-item='time']").send_keys :backspace
  166. end
  167. find("div[data-name='#{name}'] .js-timepicker").fill_in with: value
  168. end
  169. # Check the field value of a form tokens field.
  170. #
  171. # @example
  172. # check_tokens_field_value('tags', 'tag name')
  173. # check_tokens_field_value('tags', %w[tag1 tag2 tag3)
  174. #
  175. def check_tokens_field_value(name, value, **find_options)
  176. input_value = if value.is_a?(Array)
  177. value.join(', ')
  178. else
  179. value
  180. end
  181. expect(find("input[name='#{name}']", visible: :all, **find_options).value).to eq(input_value)
  182. end
  183. # Set the field value of a form tokens field.
  184. #
  185. # @example
  186. # set_tokens_field_value('tags', 'tag name')
  187. # set_tokens_field_value('tags', %w[tag1 tag2 tag3])
  188. #
  189. def set_tokens_field_value(name, value, **find_options)
  190. input_string = if value.is_a?(Array)
  191. value.join(', ')
  192. else
  193. value
  194. end
  195. find("input[name='#{name}'] ~ input.token-input", **find_options).send_keys input_string, :tab
  196. token_count = if value.is_a?(Array)
  197. value.length
  198. else
  199. 1
  200. end
  201. wait.until { find_all("input[name='#{name}'] ~ .token").length == token_count }
  202. end
  203. # Check the field value of a form switch field.
  204. #
  205. # @example
  206. # check_switch_field_value('switch_field_name', true)
  207. #
  208. def check_switch_field_value(name, value, **find_options)
  209. switch_field = find("input[name='#{name}']", visible: :all, **find_options)
  210. if value
  211. expect(switch_field).to be_checked
  212. else
  213. expect(switch_field).not_to be_checked
  214. end
  215. end
  216. # Set the field value of a form switch field.
  217. #
  218. # @example
  219. # set_switch_field_value('switch_field_name', false)
  220. #
  221. def set_switch_field_value(name, value, **find_options)
  222. find("input[name='#{name}']", visible: :all, **find_options).set(value)
  223. end
  224. # Check the field value of a form textarea field.
  225. #
  226. # @example
  227. # check_textarea_field_value('textarea_field_name', 'text', visible: :all)
  228. #
  229. def check_textarea_field_value(name, value, **find_options)
  230. textarea_field = find("textarea[name='#{name}']", **find_options)
  231. expect(textarea_field.value).to eq(value)
  232. end
  233. # Set the field value of a form textarea field.
  234. #
  235. # @example
  236. # set_textarea_field_value('textarea_field_name', 'text', visible: :all)
  237. #
  238. def set_textarea_field_value(name, value, **find_options)
  239. find("textarea[name='#{name}']", **find_options).fill_in with: value
  240. end
  241. end
  242. RSpec.configure do |config|
  243. config.include FieldActions, type: :system
  244. end