Module: Avo::TestHelpers

Included in:
PrefixedTestHelpers
Defined in:
lib/avo/test_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_tag(field:, tag:) ⇒ Object

Example usage:

expect(add_tag(field: :tags, tag: "one")).to eq ["one"]
add_tag(field: :tags, tag: "two")


253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/avo/test_helpers.rb', line 253

def add_tag(field:, tag:)
  # Find the input field for the specified field
  input = find("[data-field-id='#{field}'] [data-slot='value'] [role='textbox']")

  # Enter the specified tag into the input field
  input.set(tag)
  sleep 0.1

  input.send_keys :return
  wait_for_tag_to_appear(tag)

  # Return an array of the current tags
  tags(field: field)
end

#check_select_allObject



242
243
244
# File 'lib/avo/test_helpers.rb', line 242

def check_select_all
  find("input[type='checkbox'][name='Select all'][data-action='input->item-select-all#toggle']").set(true)
end

#click_global_search_inputObject



114
115
116
117
# File 'lib/avo/test_helpers.rb', line 114

def click_global_search_input
  page.find(:xpath, "//*[contains(@class, 'global-search')]").click
  wait_for_search_loaded
end

#click_on_sidebar_item(label) ⇒ Object



179
180
181
182
183
# File 'lib/avo/test_helpers.rb', line 179

def click_on_sidebar_item(label)
  within main_sidebar do
    click_on label
  end
end

#click_resource_search_inputObject

Example usage:

click_resource_search_input # opens the first search box on the given page
opens the search box for the "users" resource
within(has_and_belongs_to_many_field_wrapper(id: :users)) {
  click_resource_search_input
  write_in_search("Bob")
}


109
110
111
112
# File 'lib/avo/test_helpers.rb', line 109

def click_resource_search_input
  first("[data-search-resource]:not([data-search-resource='global'])").click
  wait_for_search_loaded
end

#click_tab(tab_name = "", within_target: nil, **args) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/avo/test_helpers.rb', line 149

def click_tab(tab_name = "", within_target: nil, **args)
  if within_target.present?
    within within_target do
      within find('[data-controller="tabs"] turbo-frame:not(.hidden) [data-target="tab-switcher"]') do
        find_link(tab_name).click
      end
    end
  else
    within find('[data-controller="tabs"] turbo-frame:not(.hidden) [data-target="tab-switcher"]') do
      find_link(tab_name).click
    end
  end
end

#close_pickerObject



206
207
208
209
# File 'lib/avo/test_helpers.rb', line 206

def close_picker
  find('[data-target="title"]').click
  sleep 0.3
end

#confirm_alertObject



94
95
96
97
98
99
100
# File 'lib/avo/test_helpers.rb', line 94

def confirm_alert
  Capybara.using_wait_time(1) do
    page.driver.browser.switch_to.alert.accept
  rescue Selenium::WebDriver::Error::NoSuchAlertError
    retry
  end
end

#empty_dashObject



90
91
92
# File 'lib/avo/test_helpers.rb', line 90

def empty_dash
  ""
end

#first_tab_groupObject



167
168
169
# File 'lib/avo/test_helpers.rb', line 167

def first_tab_group
  tab_group 0
end

#grid_field_wrapper(record_id:) ⇒ Object

Finds the wrapper element on the index grid view type for the given record id Example usage:

grid_field_wrapper(record_id: 5)


20
21
22
# File 'lib/avo/test_helpers.rb', line 20

def grid_field_wrapper(record_id:)
  find("[data-component-name='avo/index/grid_item_component'][data-resource-id='#{record_id}']")
end

#has_and_belongs_to_many_field_wrapper(id:, view: :show) ⇒ Object

Finds the table on the show view for the given has_and_belongs_to_many field id and view Example usage:

has_and_belongs_to_many_field_wrapper(id: :users)
has_and_belongs_to_many_field_wrapper(id: :users, view: :edit)


50
51
52
# File 'lib/avo/test_helpers.rb', line 50

def has_and_belongs_to_many_field_wrapper(id:, view: :show)
  related_field_context(id: id, relation: :has_and_belongs_to_many, view: view)
end

#has_many_field_wrapper(id:, view: :show) ⇒ Object

Finds the table on the show view for the given has_many field id and view Example usage:

has_many_field_wrapper(id: :users)
has_many_field_wrapper(id: :users, view: :edit)


42
43
44
# File 'lib/avo/test_helpers.rb', line 42

def has_many_field_wrapper(id:, view: :show)
  related_field_context(id: id, relation: :has_many, view: view)
end

#has_one_field_wrapper(id:, view: :show) ⇒ Object

Finds the table on the show view for the given has_one field id and view Example usage:

has_one_field_wrapper(id: :users)
has_one_field_wrapper(id: :users, view: :edit)


58
59
60
# File 'lib/avo/test_helpers.rb', line 58

def has_one_field_wrapper(id:, view: :show)
  related_field_context(id: id, relation: :has_one, view: view)
end

#index_field_label(id:) ⇒ Object

Finds the label element on the index view for the given field id Example usage:

index_field_label(id: "name")


65
66
67
# File 'lib/avo/test_helpers.rb', line 65

def index_field_label(id:)
  find("[data-component-name='avo/partials/table_header'] [data-table-header-field-id='#{id}']").text
end

#index_field_value(id:, record_id:, type: nil) ⇒ Object

Finds the value element on the index view for the given field id Example usage:

index_field_value(id: "name", record_id: 2)


79
80
81
# File 'lib/avo/test_helpers.rb', line 79

def index_field_value(id:, record_id:, type: nil)
  index_field_wrapper(id: id, record_id: record_id, type: type).text
end

#index_field_wrapper(id:, record_id:, type: nil) ⇒ Object

Finds the wrapper element on the index view for the given field id and type, and associated with the given record id Example usage:

index_field_wrapper(id: "name", type: "text", record_id: 2)
index_field_wrapper(id: "name", record_id: 2)


7
8
9
10
11
12
13
14
15
# File 'lib/avo/test_helpers.rb', line 7

def index_field_wrapper(id:, record_id:, type: nil)
  base_data = "#{row(record_id)} [data-field-id='#{id}']"

  if type.present?
    find("#{base_data}[data-resource-index-target='#{wrapper_name_for(id: id, type: type)}']")
  else
    find(base_data)
  end
end

#open_panel_action(action_name:, list: "Actions") ⇒ Object

Click on the action from the panel (index and show above the table) Pass list: nil to run an action outside of the list Pass list: “List name” if list is not the default “Actions” Example usage:

open_panel_action(action_name: "Dummy action")
open_panel_action(list: nil, action_name: "Release fish")
open_panel_action(list: "Runnables", action_name: "Release fish")


218
219
220
# File 'lib/avo/test_helpers.rb', line 218

def open_panel_action(action_name:, list: "Actions")
  open_action(action_name: action_name, list: list, context: first("[data-target='panel-tools']"))
end

#open_pickerObject



202
203
204
# File 'lib/avo/test_helpers.rb', line 202

def open_picker
  text_input.click
end

#open_row_action(record_id:, action_name:, list: "Actions") ⇒ Object

Open the action from the record_id row Pass list: nil to run an action outside of the list Pass list: “List name” if list is not the default “Actions” Example usage:

open_row_action(action_name: "Dummy action")
open_row_action(list: nil, action_name: "Release fish")
open_row_action(list: "Runnables", action_name: "Release fish")


229
230
231
# File 'lib/avo/test_helpers.rb', line 229

def open_row_action(record_id:, action_name:, list: "Actions")
  open_action(action_name: action_name, list: list, context: row(record_id))
end

#remove_tag(field:, tag:) ⇒ Object

Example usage:

expect(remove_tag(field: :tags, tag: "one")).to eq ["three"]
remove_tag(field: :tags, tag: "one")


271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/avo/test_helpers.rb', line 271

def remove_tag(field:, tag:)
  # Within the specified field
  within("[data-field-id='#{field}'] [data-slot='value']") do
    # Find the tag with the specified text and click the remove button for the tag
    page.find(".tagify__tag", text: tag).find(".tagify__tag__removeBtn").click

    wait_for_tag_to_disappear(tag)
  end

  # Return an array of the current tags
  tags(field: field)
end

#run_actionObject

Click on submit action button if dialog is open



234
235
236
237
238
239
240
# File 'lib/avo/test_helpers.rb', line 234

def run_action
  within(find("[role='dialog']")) do
    find("[data-target='submit_action']").click
  end

  wait_for_action_dialog_to_disappear
end

#saveObject

Save a record and wait for the page to load



144
145
146
147
# File 'lib/avo/test_helpers.rb', line 144

def save
  click_on "Save"
  wait_for_loaded
end

#second_tab_groupObject



171
172
173
# File 'lib/avo/test_helpers.rb', line 171

def second_tab_group
  tab_group 1
end

#select_first_result_in_searchObject

Should use the click_global_search_input or click_resource_search_input method to open the search box first and optionaly write_in_search. Example usage:

open_search_box(:users) # opens the search box for the "users" resource
write_in_search("John Doe")
select_first_result_in_search


137
138
139
140
141
# File 'lib/avo/test_helpers.rb', line 137

def select_first_result_in_search
  find(".aa-Input").send_keys :arrow_down
  find(".aa-Input").send_keys :enter
  wait_for_search_loaded
end

#set_picker_day(date) ⇒ Object



185
186
187
188
# File 'lib/avo/test_helpers.rb', line 185

def set_picker_day(date)
  find(".flatpickr-day[aria-label=\"#{date}\"]").click
  sleep 0.2
end

#set_picker_hour(value) ⇒ Object



190
191
192
# File 'lib/avo/test_helpers.rb', line 190

def set_picker_hour(value)
  find(".flatpickr-hour").set(value)
end

#set_picker_minute(value) ⇒ Object



194
195
196
# File 'lib/avo/test_helpers.rb', line 194

def set_picker_minute(value)
  find(".flatpickr-minute").set(value)
end

#set_picker_second(value) ⇒ Object



198
199
200
# File 'lib/avo/test_helpers.rb', line 198

def set_picker_second(value)
  find(".flatpickr-second").set(value)
end

#show_field_label(id:) ⇒ Object

Finds the label element on the show view for the given field id Example usage:

show_field_label(id: "name")


72
73
74
# File 'lib/avo/test_helpers.rb', line 72

def show_field_label(id:)
  within(show_field_wrapper(id: id)) { find("[data-slot='label']").text }
end

#show_field_value(id:, type: nil) ⇒ Object

Finds the value element on the show view for the given field id Example usage:

show_field_value(id: "name")


86
87
88
# File 'lib/avo/test_helpers.rb', line 86

def show_field_value(id:, type: nil)
  within(show_field_wrapper(id: id, type: type)) { find("[data-slot='value']").text }
end

#show_field_wrapper(id:, type: nil) ⇒ Object

Finds the wrapper element on the show view for the given field id and type Example usage:

show_field_wrapper(id: "name", type: "text")
show_field_wrapper(id: "name")


28
29
30
31
32
33
34
35
36
# File 'lib/avo/test_helpers.rb', line 28

def show_field_wrapper(id:, type: nil)
  base_data = "[data-panel-id='main'] [data-field-id='#{id}']"

  if type.present?
    find("#{base_data}[data-resource-show-target='#{wrapper_name_for(id: id, type: type)}']")
  else
    find(base_data)
  end
end

#tab_group(index = 0) ⇒ Object



163
164
165
# File 'lib/avo/test_helpers.rb', line 163

def tab_group(index = 0)
  find_all('[data-controller="tabs"]')[index]
end

#tag_suggestions(field:, input:) ⇒ Object

Example usage:

expect(tag_suggestions(field: :tags, input: "")).to eq ["one", "two", "three"]
expect(tag_suggestions(field: :tags, input: "t")).to eq ["two", "three"]


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/avo/test_helpers.rb', line 296

def tag_suggestions(field:, input:)
  # Find the input field for the specified tag field
  input_area = find("[data-field-id='#{field}'] [data-slot='value'] [role='textbox']")

  # If the input argument is present, enter it into the input field
  # Else, just click on the middle of the input field to display the dropdown
  if input.present?
    input_area.set(input)
  else
    input_area.click(x: input_area.native.size.width / 2, y: input_area.native.size.height / 2)
  end
  wait_for_tag_suggestions_to_appear

  # Find all elements with class 'tagify_dropdown_item' within the dropdown
  # Map the elements to their 'label' attribute values and return the array of labels
  page.all(".tagify__dropdown__item").map { |element| element[:label] }
end

#tags(field:) ⇒ Object

Example usage:

expect(tags(field: :tags)).to eq ["one", "two", "three"]
expect(tags(field: :tags)).to eq []


287
288
289
290
291
# File 'lib/avo/test_helpers.rb', line 287

def tags(field:)
  # Find all elements with class 'tagify__tag'
  # Map the elements to their 'label' attribute values and return the array of labels
  page.all(".tagify__tag").map { |element| element[:label] }
end

#third_tab_groupObject



175
176
177
# File 'lib/avo/test_helpers.rb', line 175

def third_tab_group
  tab_group 3
end

#toggle_collapsable(section) ⇒ Object



246
247
248
# File 'lib/avo/test_helpers.rb', line 246

def toggle_collapsable(section)
  find("[data-action='click->menu#triggerCollapse'][data-menu-key-param*='main_menu.#{section.underscore}'] svg").click
end

#write_in_search(input) ⇒ Object

Should use the click_global_search_input or click_resource_search_input method to open the search box first. Example usage:

open_search_box(:users)
within(has_and_belongs_to_many_field_wrapper(id: :users)) {
  click_resource_search_input # opens the search box for the "users" resource
  write_in_search("Bob")
}


126
127
128
129
130
# File 'lib/avo/test_helpers.rb', line 126

def write_in_search(input)
  # Use xpath to find outside of within context if any
  find(:xpath, "//input[@class='aa-Input']").set(input)
  wait_for_search_loaded
end