Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CapybaraHelpers requireable #2445

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions lib/alchemy/test_support/capybara_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

module Alchemy
module TestSupport
module CapybaraHelpers
# Select2 capybara helper
def select2(value, options)
label = find_label_by_text(options[:from])

select2_anchor_selector = ".select2-container a"

if label.has_css?(select2_anchor_selector)
label.find(select2_anchor_selector).click
else
within label.find(:xpath, "..") do
find(select2_anchor_selector).click
end
end

within_entire_page do
page.find(
"div.select2-result-label",
text: /#{Regexp.escape(value)}/i, match: :prefer_exact,
).click
end
end

def select2_search(value, options)
if options[:from]
label = find_label_by_text(options[:from])
within label.first(:xpath, ".//..") do
options[:from] = "##{find(".select2-container")["id"]}"
end
elsif options[:element_id] && options[:ingredient_role]
container_id = find("#element_#{options[:element_id]} [data-ingredient-role='#{options[:ingredient_role]}'] .select2-container")["id"]
options[:from] = "##{container_id}"
end

find("#{options[:from]}:not(.select2-container-disabled):not(.select2-offscreen)").click

within_entire_page do
find("input.select2-input.select2-focused").set(value)
expect(page).to have_selector(".select2-result-label", visible: true)
find("div.select2-result-label", text: /#{Regexp.escape(value)}/i, match: :prefer_exact).click
expect(page).not_to have_selector(".select2-result-label")
end
end

def click_button_with_label(label)
label = find("label", text: label)
within label.first(:xpath, ".//..") do
first("button").click
end
end

private

def within_entire_page(&block)
within(:xpath, "//body", &block)
end

def find_label_by_text(text)
find "label:not(.select2-offscreen)",
text: /#{Regexp.escape(text)}/i,
match: :one
end
end
end
end
4 changes: 2 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require "alchemy/seeder"
require "alchemy/test_support"
require "alchemy/test_support/capybara_helpers"
require "alchemy/test_support/config_stubbing"
require "alchemy/test_support/having_crop_action_examples"
require "alchemy/test_support/having_picture_thumbnails_examples"
Expand All @@ -30,7 +31,6 @@

require_relative "support/calculation_examples"
require_relative "support/hint_examples"
require_relative "support/capybara_helpers"
require_relative "support/custom_news_elements_finder"

ActionMailer::Base.delivery_method = :test
Expand Down Expand Up @@ -78,7 +78,7 @@
config.include Alchemy::TestSupport::IntegrationHelpers, type: type
end
config.include FactoryBot::Syntax::Methods
config.include CapybaraSelect2, type: :system
config.include Alchemy::TestSupport::CapybaraHelpers, type: :system

config.use_transactional_fixtures = true

Expand Down
65 changes: 0 additions & 65 deletions spec/support/capybara_helpers.rb

This file was deleted.