From 0ca4d0842a08ff63e7c76a1cecf3f23deb75eb9a Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 16:19:57 +0100 Subject: [PATCH] Remove capybara_ext from spree and bring its helpers that are required in ofn --- .../configuration/tax_categories_spec.rb | 8 +-- .../admin/configuration/taxonomies_spec.rb | 2 +- spec/features/admin/orders_spec.rb | 2 +- spec/spec_helper.rb | 9 ++-- spec/support/request/web_helper.rb | 51 ++++++++++++++++++- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/spec/features/admin/configuration/tax_categories_spec.rb b/spec/features/admin/configuration/tax_categories_spec.rb index 9e5d2f3823..11cf5ea49b 100644 --- a/spec/features/admin/configuration/tax_categories_spec.rb +++ b/spec/features/admin/configuration/tax_categories_spec.rb @@ -13,9 +13,9 @@ describe "Tax Categories" do click_link "Tax Categories" expect(page).to have_content("Listing Tax Categories") within_row(1) do - expect(column_text(1)).to eq("Clothing") - expect(column_text(2)).to eq("For Clothing") - expect(column_text(3)).to eq("False") + expect(find("td:nth-child(1)").text).to eq("Clothing") + expect(find("td:nth-child(2)").text).to eq("For Clothing") + expect(find("td:nth-child(3)").text).to eq("False") end end end @@ -44,7 +44,7 @@ describe "Tax Categories" do it "should be able to update an existing tax category" do create(:tax_category) click_link "Tax Categories" - within_row(1) { click_icon :edit } + within_row(1) { find(".icon-edit").click } fill_in "tax_category_description", with: "desc 99" click_button "Update" expect(page).to have_content("successfully updated!") diff --git a/spec/features/admin/configuration/taxonomies_spec.rb b/spec/features/admin/configuration/taxonomies_spec.rb index f26b320be3..3925e60039 100644 --- a/spec/features/admin/configuration/taxonomies_spec.rb +++ b/spec/features/admin/configuration/taxonomies_spec.rb @@ -43,7 +43,7 @@ describe "Taxonomies" do it "should allow an admin to update an existing taxonomy" do create(:taxonomy) click_link "Taxonomies" - within_row(1) { click_icon :edit } + within_row(1) { find(".icon-edit").click } fill_in "taxonomy_name", with: "sports 99" click_button "Update" expect(page).to have_content("successfully updated!") diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 8510ff5943..42d918db22 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -105,7 +105,7 @@ feature ' uncheck 'Only show complete orders' page.find('a.icon-search').click - click_icon :edit + find(".icon-edit").click expect(page).to have_current_path spree.edit_admin_order_path(incomplete_order) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d4130a9d2d..937366a5d7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,8 +32,7 @@ Shoulda::Matchers.configure do |config| end end -# Allow connections to phantomjs/selenium whilst raising errors -# when connecting to external sites +# Allow connections to selenium whilst raising errors when connecting to external sites require 'webmock/rspec' WebMock.enable! WebMock.disable_net_connect!( @@ -44,7 +43,6 @@ WebMock.disable_net_connect!( # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } -require 'spree/testing_support/capybara_ext' require 'spree/api/testing_support/setup' require 'spree/testing_support/authorization_helpers' require 'spree/testing_support/preferences' @@ -65,6 +63,11 @@ end Capybara.default_max_wait_time = 30 +Capybara.configure do |config| + config.match = :prefer_exact + config.ignore_hidden_elements = true +end + require "paperclip/matchers" # Override setting in Spree engine: Spree::Core::MailSettings diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 25ae07bbc1..37b2ce135f 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -95,16 +95,29 @@ module WebHelper end end + def within_row(num, &block) + within("table.index tbody tr:nth-child(#{num})", &block) + end + def select2_select(value, options) id = options[:from] options[:from] = "#s2id_#{id}" targetted_select2(value, options) end + def targetted_select2(value, options) + # find select2 element and click it + find(options[:from]).find('a').click + select_select2_result(value) + end + + def select_select2_result(value) + sleep(1) + page.execute_script(%Q{$("div.select2-result-label:contains('#{value}')").mouseup()}) + end + # Support having different texts to search for and to click in the select2 # field. - # - # This overrides the method in Spree. def targetted_select2_search(value, options) page.execute_script %{$('#{options[:from]}').select2('open')} page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');" @@ -124,6 +137,18 @@ module WebHelper page.execute_script "jQuery('#{selector}').select2('close');" end + def set_select2_field(field, value) + page.execute_script %Q{$('#{field}').select2('val', '#{value}')} + end + + def select2_search(value, options) + label = find_label_by_text(options[:from]) + within label.first(:xpath,".//..") do + options[:from] = "##{find(".select2-container")["id"]}" + targetted_select2_search(value, options) + end + end + def select2_search_async(value, options) id = find_label_by_text(options[:from]) options[:from] = "#s2id_#{id}" @@ -145,6 +170,28 @@ module WebHelper page.execute_script(%{$("div.select2-result-label:contains('#{value}')").mouseup()}) end + def find_label_by_text(text) + label = find_label(text) + counter = 0 + + # Because JavaScript testing is prone to errors... + while label.nil? && counter < 10 + sleep(1) + counter += 1 + label = find_label(text) + end + + if label.nil? + raise "Could not find label by text #{text}" + end + + label + end + + def find_label(text) + first(:xpath, "//label[text()[contains(.,'#{text}')]]") + end + def accept_js_alert page.driver.browser.switch_to.alert.accept end