From 0ca4d0842a08ff63e7c76a1cecf3f23deb75eb9a Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 16:19:57 +0100 Subject: [PATCH 01/11] 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 From b6adef38e5718eff62d7a256a4009a8187bcf77c Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 16:27:32 +0100 Subject: [PATCH 02/11] Remove unused auth helpers --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 937366a5d7..550d01db81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,7 +44,6 @@ WebMock.disable_net_connect!( # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } require 'spree/api/testing_support/setup' -require 'spree/testing_support/authorization_helpers' require 'spree/testing_support/preferences' require 'support/api_helper' From d0c07b931aed22c411e204b55f11e82303e3910b Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 16:33:38 +0100 Subject: [PATCH 03/11] Bring Spree::TestingSupport::Preferences from spree_core to ofn as PreferencesHelper --- spec/lib/spree/money_spec.rb | 2 ++ spec/spec_helper.rb | 3 +-- spec/support/preferences_helper.rb | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 spec/support/preferences_helper.rb diff --git a/spec/lib/spree/money_spec.rb b/spec/lib/spree/money_spec.rb index 139806137c..12b1d975b8 100644 --- a/spec/lib/spree/money_spec.rb +++ b/spec/lib/spree/money_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' describe Spree::Money do + include PreferencesHelper + before do configure_spree_preferences do |config| config.currency = "USD" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 550d01db81..f1111e7576 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,7 +44,6 @@ WebMock.disable_net_connect!( # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } require 'spree/api/testing_support/setup' -require 'spree/testing_support/preferences' require 'support/api_helper' # Capybara config @@ -157,7 +156,7 @@ RSpec.configure do |config| config.include Spree::UrlHelpers config.include Spree::CheckoutHelpers config.include Spree::MoneyHelper - config.include Spree::TestingSupport::Preferences + config.include PreferencesHelper config.include ControllerRequestsHelper, type: :controller config.include Devise::TestHelpers, type: :controller config.extend Spree::Api::TestingSupport::Setup, type: :controller diff --git a/spec/support/preferences_helper.rb b/spec/support/preferences_helper.rb new file mode 100644 index 0000000000..6563c6b27d --- /dev/null +++ b/spec/support/preferences_helper.rb @@ -0,0 +1,21 @@ +module PreferencesHelper + # Resets all preferences to default values, you can + # pass a block to override the defaults with a block + # + # reset_spree_preferences do |config| + # config.site_name = "my fancy pants store" + # end + # + def reset_spree_preferences(&config_block) + Spree::Preferences::Store.instance.persistence = false + Spree::Preferences::Store.instance.clear_cache + + config = Rails.application.config.spree.preferences + configure_spree_preferences &config_block if block_given? + end + + def configure_spree_preferences + config = Rails.application.config.spree.preferences + yield(config) if block_given? + end +end From 261269bd577b7bd85044fa53da53c135325974ba Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 17:08:57 +0100 Subject: [PATCH 04/11] Bring core/lib/spree/testing_support/i18n from spree_core to ofn as i18n_translations_checker --- spec/lib/spree/i18n_spec.rb | 2 +- spec/support/i18n_translations_checker.rb | 97 +++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 spec/support/i18n_translations_checker.rb diff --git a/spec/lib/spree/i18n_spec.rb b/spec/lib/spree/i18n_spec.rb index 386dd9d2ec..397dd36641 100644 --- a/spec/lib/spree/i18n_spec.rb +++ b/spec/lib/spree/i18n_spec.rb @@ -2,7 +2,7 @@ require 'rspec/expectations' require 'spree/i18n' -require 'spree/testing_support/i18n' +require 'support/i18n_translations_checker' describe "i18n" do before do diff --git a/spec/support/i18n_translations_checker.rb b/spec/support/i18n_translations_checker.rb new file mode 100644 index 0000000000..786e2f1cf9 --- /dev/null +++ b/spec/support/i18n_translations_checker.rb @@ -0,0 +1,97 @@ +# This file exists solely to test whether or not there are missing translations +# within the code that Spree's test suite covers. +# +# If there is a translation referenced which has no corresponding key within the +# .yml file, then there will be a message output at the end of the suite showing +# that. +# +# If there is a translation within the locale file which *isn't* used in the +# test, this will also be shown at the end of the suite run. +module Spree + class << self + attr_accessor :used_translations, :missing_translation_messages, + :unused_translations, :unused_translation_messages + alias_method :normal_t, :t + end + + def self.t(*args) + original_args = args.dup + options = args.extract_options! + self.used_translations ||= [] + [*args.first].each do |translation_key| + key = ([*options[:scope]] << translation_key).join('.') + self.used_translations << key + end + normal_t(*original_args) + end + + def self.check_missing_translations + self.missing_translation_messages = [] + self.used_translations ||= [] + used_translations.map { |a| a.split('.') }.each do |translation_keys| + root = translations + processed_keys = [] + translation_keys.each do |key| + begin + root = root.fetch(key.to_sym) + processed_keys << key.to_sym + rescue KeyError + error = "#{(processed_keys << key).join('.')} (#{I18n.locale})" + unless Spree.missing_translation_messages.include?(error) + Spree.missing_translation_messages << error + end + end + end + end + end + + def self.check_unused_translations + self.used_translations ||= [] + self.unused_translation_messages = [] + self.unused_translations = [] + self.load_translations(translations) + translation_diff = unused_translations - used_translations + translation_diff.each do |translation| + Spree.unused_translation_messages << "#{translation} (#{I18n.locale})" + end + end + + private + + def self.load_translations(hash, root=[]) + hash.each do |k,v| + if v.is_a?(Hash) + load_translations(v, root.dup << k) + else + key = (root + [k]).join('.') + self.unused_translations << key + end + end + end + + def self.translations + @translations ||= I18n.backend.send(:translations)[I18n.locale][:spree] + end +end + +RSpec.configure do |config| + # Need to check here again because this is used in i18n_spec too. + if ENV['CHECK_TRANSLATIONS'] + config.after :suite do + Spree.check_missing_translations + if Spree.missing_translation_messages.any? + puts "\nThere are missing translations within Spree:" + puts Spree.missing_translation_messages.sort + exit(1) + end + + Spree.check_unused_translations + if false && Spree.unused_translation_messages.any? + puts "\nThere are unused translations within Spree:" + puts Spree.unused_translation_messages.sort + exit(1) + end + end + end +end + From 907afe31316b0ed1a65019219932e67c541928df Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 17:12:27 +0100 Subject: [PATCH 05/11] Remove dependency to BarAbility from spree_core --- .../spree/admin/users_controller_spec.rb | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/spec/controllers/spree/admin/users_controller_spec.rb b/spec/controllers/spree/admin/users_controller_spec.rb index 22c0bc71ab..f67417e1fe 100644 --- a/spec/controllers/spree/admin/users_controller_spec.rb +++ b/spec/controllers/spree/admin/users_controller_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'spree/testing_support/bar_ability' describe Spree::Admin::UsersController do context '#authorize_admin' do @@ -34,18 +33,31 @@ describe Spree::Admin::UsersController do expect(response).to redirect_to(spree.edit_admin_user_path(test_user)) end - it 'should deny access to users with an bar role' do - user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') - Spree::Ability.register_ability(BarAbility) - spree_post :index - expect(response).to redirect_to('/unauthorized') - end + describe "with BarAbility" do + class BarAbility + include CanCan::Ability - it 'should deny access to users with an bar role' do - user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') - Spree::Ability.register_ability(BarAbility) - spree_post :update, id: '9' - expect(response).to redirect_to('/unauthorized') + def initialize(user) + user ||= Spree::User.new + return unless user.has_spree_role?('bar') + + can [:admin, :index, :show], Spree::Order + end + end + + it 'should deny access to users with an bar role' do + user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') + Spree::Ability.register_ability(BarAbility) + spree_post :index + expect(response).to redirect_to('/unauthorized') + end + + it 'should deny access to users with an bar role' do + user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') + Spree::Ability.register_ability(BarAbility) + spree_post :update, id: '9' + expect(response).to redirect_to('/unauthorized') + end end it 'should deny access to users without an admin role' do From 5cd40cb19fc5f21290927c4270e70b3757940a9c Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 17:18:46 +0100 Subject: [PATCH 06/11] Remove dependency to Spree::TestingSupport code --- lib/open_food_network/enterprise_issue_validator.rb | 9 ++++++--- .../open_food_network/enterprise_issue_validator_spec.rb | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/enterprise_issue_validator.rb b/lib/open_food_network/enterprise_issue_validator.rb index 885905f32b..b845046f2a 100644 --- a/lib/open_food_network/enterprise_issue_validator.rb +++ b/lib/open_food_network/enterprise_issue_validator.rb @@ -1,7 +1,6 @@ module OpenFoodNetwork class EnterpriseIssueValidator include Rails.application.routes.url_helpers - include Spree::TestingSupport::UrlHelpers def initialize(enterprise) @enterprise = enterprise @@ -13,14 +12,14 @@ module OpenFoodNetwork unless shipping_methods_ok? issues << { description: I18n.t('admin.enterprise_issues.has_no_shipping_methods', enterprise: @enterprise.name), - link: "#{I18n.t('admin.enterprise_issues.create_new')}" + link: "#{I18n.t('admin.enterprise_issues.create_new')}" } end unless payment_methods_ok? issues << { description: I18n.t('admin.enterprise_issues.has_no_payment_methods', enterprise: @enterprise.name), - link: "#{I18n.t('admin.enterprise_issues.create_new')}" + link: "#{I18n.t('admin.enterprise_issues.create_new')}" } end @@ -64,5 +63,9 @@ module OpenFoodNetwork @enterprise.payment_methods.available.any? end + + def spree_routes_helper + Spree::Core::Engine.routes.url_helpers + end end end diff --git a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb index 8503da23bb..c7ee1bfbe7 100644 --- a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb +++ b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'open_food_network/enterprise_issue_validator' module OpenFoodNetwork From 41a7e26fcd212525bc704df6e2d9d8df56298096 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 17:22:58 +0100 Subject: [PATCH 07/11] Remove unused helper --- spec/features/admin/overview_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/features/admin/overview_spec.rb b/spec/features/admin/overview_spec.rb index 3a4a3feb7d..d7e5ef7d6d 100644 --- a/spec/features/admin/overview_spec.rb +++ b/spec/features/admin/overview_spec.rb @@ -6,7 +6,6 @@ feature ' ', js: true do include WebHelper include AuthenticationHelper - include ::Spree::TestingSupport::AuthorizationHelpers context "as an enterprise user" do before do From a44d0f7af682cb31129e070b40e597e7b054ae7d Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 18:05:39 +0100 Subject: [PATCH 08/11] Add WebHelper where necessary --- spec/features/admin/configuration/states_spec.rb | 1 + spec/features/admin/configuration/tax_categories_spec.rb | 1 + spec/features/admin/configuration/taxonomies_spec.rb | 1 + spec/features/admin/configuration/zones_spec.rb | 1 + 4 files changed, 4 insertions(+) diff --git a/spec/features/admin/configuration/states_spec.rb b/spec/features/admin/configuration/states_spec.rb index e3a761088d..9caad93175 100755 --- a/spec/features/admin/configuration/states_spec.rb +++ b/spec/features/admin/configuration/states_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "States" do include AuthenticationHelper + include WebHelper let!(:country) { create(:country) } diff --git a/spec/features/admin/configuration/tax_categories_spec.rb b/spec/features/admin/configuration/tax_categories_spec.rb index 11cf5ea49b..4ab3ab8956 100644 --- a/spec/features/admin/configuration/tax_categories_spec.rb +++ b/spec/features/admin/configuration/tax_categories_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Tax Categories" do include AuthenticationHelper + include WebHelper before(:each) do login_as_admin_and_visit spree.edit_admin_general_settings_path diff --git a/spec/features/admin/configuration/taxonomies_spec.rb b/spec/features/admin/configuration/taxonomies_spec.rb index 3925e60039..e4c4d5da69 100644 --- a/spec/features/admin/configuration/taxonomies_spec.rb +++ b/spec/features/admin/configuration/taxonomies_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Taxonomies" do include AuthenticationHelper + include WebHelper before(:each) do login_as_admin_and_visit spree.edit_admin_general_settings_path diff --git a/spec/features/admin/configuration/zones_spec.rb b/spec/features/admin/configuration/zones_spec.rb index 1c03ff0941..f96229a1db 100644 --- a/spec/features/admin/configuration/zones_spec.rb +++ b/spec/features/admin/configuration/zones_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Zones" do include AuthenticationHelper + include WebHelper before do login_as_admin From b227c78aeb078d79a41d15247fcabaf8b93e5e11 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 18:13:21 +0100 Subject: [PATCH 09/11] Fix rubocop issues --- spec/support/i18n_translations_checker.rb | 19 ++++++++++--------- spec/support/preferences_helper.rb | 4 +++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/spec/support/i18n_translations_checker.rb b/spec/support/i18n_translations_checker.rb index 786e2f1cf9..a722f13302 100644 --- a/spec/support/i18n_translations_checker.rb +++ b/spec/support/i18n_translations_checker.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file exists solely to test whether or not there are missing translations # within the code that Spree's test suite covers. # @@ -49,29 +51,29 @@ module Spree self.used_translations ||= [] self.unused_translation_messages = [] self.unused_translations = [] - self.load_translations(translations) + load_translations(translations) translation_diff = unused_translations - used_translations translation_diff.each do |translation| Spree.unused_translation_messages << "#{translation} (#{I18n.locale})" end end - private - - def self.load_translations(hash, root=[]) - hash.each do |k,v| + def self.load_translations(hash, root = []) + hash.each do |k, v| if v.is_a?(Hash) load_translations(v, root.dup << k) else key = (root + [k]).join('.') - self.unused_translations << key + unused_translations << key end end end + private_class_method :load_translations def self.translations - @translations ||= I18n.backend.send(:translations)[I18n.locale][:spree] + @translations ||= I18n.backend.__send__(:translations)[I18n.locale][:spree] end + private_class_method :translations end RSpec.configure do |config| @@ -86,7 +88,7 @@ RSpec.configure do |config| end Spree.check_unused_translations - if false && Spree.unused_translation_messages.any? + if Spree.unused_translation_messages.any? puts "\nThere are unused translations within Spree:" puts Spree.unused_translation_messages.sort exit(1) @@ -94,4 +96,3 @@ RSpec.configure do |config| end end end - diff --git a/spec/support/preferences_helper.rb b/spec/support/preferences_helper.rb index 6563c6b27d..b69f5e8215 100644 --- a/spec/support/preferences_helper.rb +++ b/spec/support/preferences_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module PreferencesHelper # Resets all preferences to default values, you can # pass a block to override the defaults with a block @@ -11,7 +13,7 @@ module PreferencesHelper Spree::Preferences::Store.instance.clear_cache config = Rails.application.config.spree.preferences - configure_spree_preferences &config_block if block_given? + configure_spree_preferences(&config_block) if block_given? end def configure_spree_preferences From fb88cfedcf5ea357fe3629b52bcff67f82667385 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 26 Aug 2020 22:49:52 +0100 Subject: [PATCH 10/11] Remove unused helpers --- spec/support/request/web_helper.rb | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 37b2ce135f..436a73c33b 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -149,27 +149,6 @@ module WebHelper end end - def select2_search_async(value, options) - id = find_label_by_text(options[:from]) - options[:from] = "#s2id_#{id}" - targetted_select2_search_async(value, options) - end - - def targetted_select2_search_async(value, options) - page.execute_script %{$('#{options[:from]}').select2('open')} - page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');" - select_select2_result_async(value) - end - - def select_select2_result_async(value) - while page.has_selector? "div.select2-searching" - return if page.has_selector? "div.select2-no-results" - - sleep 0.2 - end - page.execute_script(%{$("div.select2-result-label:contains('#{value}')").mouseup()}) - end - def find_label_by_text(text) label = find_label(text) counter = 0 From 23c86a193721b263073a4189319acb85e58c5a04 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 26 Aug 2020 23:06:25 +0100 Subject: [PATCH 11/11] Replace select2_search usages with faster targetted_select2_search and remove select2_search and dependent code --- spec/features/admin/enterprise_groups_spec.rb | 4 +-- spec/features/admin/subscriptions_spec.rb | 2 +- spec/support/matchers/select2_matchers.rb | 2 -- spec/support/request/web_helper.rb | 30 ------------------- 4 files changed, 3 insertions(+), 35 deletions(-) diff --git a/spec/features/admin/enterprise_groups_spec.rb b/spec/features/admin/enterprise_groups_spec.rb index ff19b44f17..5793080b46 100644 --- a/spec/features/admin/enterprise_groups_spec.rb +++ b/spec/features/admin/enterprise_groups_spec.rb @@ -33,8 +33,8 @@ feature ' fill_in 'enterprise_group_name', with: 'EGEGEG' fill_in 'enterprise_group_description', with: 'This is a description' check 'enterprise_group_on_front_page' - select2_search e1.name, from: 'Enterprises' - select2_search e2.name, from: 'Enterprises' + targetted_select2_search e1.name, from: '#s2id_enterprise_group_enterprise_ids' + targetted_select2_search e2.name, from: '#s2id_enterprise_group_enterprise_ids' click_link 'Contact' fill_in 'enterprise_group_address_attributes_phone', with: '000' fill_in 'enterprise_group_address_attributes_address1', with: 'My Street' diff --git a/spec/features/admin/subscriptions_spec.rb b/spec/features/admin/subscriptions_spec.rb index 4278018aa2..30c27bb2d7 100644 --- a/spec/features/admin/subscriptions_spec.rb +++ b/spec/features/admin/subscriptions_spec.rb @@ -534,7 +534,7 @@ feature 'Subscriptions' do def add_variant_to_subscription(variant, quantity) row_count = all("#subscription-line-items .item").length variant_name = variant.full_name.present? ? "#{variant.name} - #{variant.full_name}" : variant.name - select2_search variant.name, from: I18n.t(:name_or_sku), dropdown_css: ".select2-drop", select_text: variant_name + targetted_select2_search variant.name, from: "#s2id_add_variant_id", dropdown_css: ".select2-drop", select_text: variant_name fill_in "add_quantity", with: quantity click_link "Add" expect(page).to have_selector("#subscription-line-items .item", count: row_count + 1) diff --git a/spec/support/matchers/select2_matchers.rb b/spec/support/matchers/select2_matchers.rb index ff7b6b8afe..d6acd7eddc 100644 --- a/spec/support/matchers/select2_matchers.rb +++ b/spec/support/matchers/select2_matchers.rb @@ -6,7 +6,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}| match do |node| @id, @options, @node = id, options, node - # id = find_label_by_text(locator) from = "#s2id_#{id}" results = [] @@ -35,7 +34,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}| match_when_negated do |node| @id, @options, @node = id, options, node - # id = find_label_by_text(locator) from = "#s2id_#{id}" results = [] diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 436a73c33b..a117b7742a 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -141,36 +141,6 @@ module WebHelper 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 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