Module: Avo::TestHelpers

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

Instance Method Summary collapse

Instance Method Details

#accept_custom_alert(&block) ⇒ Object



319
320
321
322
# File 'lib/avo/test_helpers.rb', line 319

def accept_custom_alert(&block)
  block.call
  find('#turbo-confirm button[value="confirm"]').click
end

#add_tag(field:, tag:) ⇒ Object

Example usage:

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


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

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

  # Enter the specified tag into the input field
  type tag, :return
  wait_for_tag_to_appear(tag)

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

#check_select_allObject



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

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



106
107
108
109
# File 'lib/avo/test_helpers.rb', line 106

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

#click_on_sidebar_item(label) ⇒ Object



182
183
184
185
186
# File 'lib/avo/test_helpers.rb', line 182

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")
}


101
102
103
104
# File 'lib/avo/test_helpers.rb', line 101

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



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

def click_tab(tab_name = "", within_target: nil, **args)
  if within_target.present?
    within within_target do
      within find('[data-controller="tabs"] [data-tabs-target="tabSwitcher"]') do
        find_link(tab_name).trigger("click")
      end
    end
  else
    within find('[data-controller="tabs"] [data-tabs-target="tabSwitcher"]') do
      find_link(tab_name).trigger("click")
    end
  end
  wait_for_loaded
end

#close_pickerObject



209
210
211
212
# File 'lib/avo/test_helpers.rb', line 209

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

#empty_dashObject



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

def empty_dash
  ""
end

#first_tab_groupObject



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

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")


221
222
223
# File 'lib/avo/test_helpers.rb', line 221

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



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

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")


232
233
234
# File 'lib/avo/test_helpers.rb', line 232

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



237
238
239
240
241
242
243
# File 'lib/avo/test_helpers.rb', line 237

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 For most cases ‘.click` works Sometimes other element may be overlapping the button so the `.trigger(“click”)` solves the issue Trigger can’t be used by default because it breaks on some feature specs



142
143
144
145
146
147
148
149
# File 'lib/avo/test_helpers.rb', line 142

def save
  button = find("button.button-component", text: "Save")
  button.click
rescue Capybara::Cuprite::MouseEventFailed
  button.trigger("click")
ensure
  wait_for_loaded
end

#second_tab_groupObject



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

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


129
130
131
132
133
134
135
136
# File 'lib/avo/test_helpers.rb', line 129

def select_first_result_in_search
  type :down, :enter
rescue
  find(".aa-Input").send_keys :arrow_down
  find(".aa-Input").send_keys :enter
ensure
  wait_for_search_loaded
end

#set_picker_day(date) ⇒ Object



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

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

#set_picker_hour(value) ⇒ Object



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

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

#set_picker_minute(value) ⇒ Object



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

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

#set_picker_second(value) ⇒ Object



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

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



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

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
313
# 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, set input to "open" than to " " to trigger dropdown
  if input.present?
    input_area.set(input)
  else
    input_area.set("open")
    input_area.set(" ")
  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



178
179
180
# File 'lib/avo/test_helpers.rb', line 178

def third_tab_group
  tab_group 3
end

#toggle_collapsable(section) ⇒ Object



249
250
251
# File 'lib/avo/test_helpers.rb', line 249

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

#typeObject



315
316
317
# File 'lib/avo/test_helpers.rb', line 315

def type(...)
  page.driver.browser.keyboard.type(...)
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")
}


118
119
120
121
122
# File 'lib/avo/test_helpers.rb', line 118

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