From 9be1949dd31006a354739a13d8477bc4b0cf8da3 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Mon, 13 Dec 2021 20:51:36 +0200 Subject: [PATCH 1/5] Add VCR cassette wrapper --- lib/cypress_on_rails/middleware.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cypress_on_rails/middleware.rb b/lib/cypress_on_rails/middleware.rb index 5aad679..33541b9 100644 --- a/lib/cypress_on_rails/middleware.rb +++ b/lib/cypress_on_rails/middleware.rb @@ -17,7 +17,13 @@ def call(env) if request.path.start_with?('/__cypress__/command') configuration.tagged_logged { handle_command(request) } else - @app.call(env) + if defined?(VCR) + VCR.use_cassette('foobar', { :record => :new_episodes }) do + @app.call(env) + end + else + @app.call(env) + end end end From 074c3d00184e158cc152e394bb475a260ae74f40 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Tue, 14 Dec 2021 21:40:02 +0200 Subject: [PATCH 2/5] Add VCR wrapper --- lib/cypress_on_rails/configuration.rb | 4 ++ lib/cypress_on_rails/middleware.rb | 8 +--- lib/cypress_on_rails/vcr_wrapper.rb | 42 +++++++++++++++++++ .../initializers/cypress_on_rails.rb.erb | 14 +++++++ 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 lib/cypress_on_rails/vcr_wrapper.rb diff --git a/lib/cypress_on_rails/configuration.rb b/lib/cypress_on_rails/configuration.rb index a08571c..3479d40 100644 --- a/lib/cypress_on_rails/configuration.rb +++ b/lib/cypress_on_rails/configuration.rb @@ -5,6 +5,8 @@ class Configuration attr_accessor :cypress_folder attr_accessor :use_middleware attr_accessor :logger + attr_accessor :use_vcr + attr_accessor :vcr_record_mode def initialize reset @@ -16,6 +18,8 @@ def reset self.cypress_folder = 'spec/cypress' self.use_middleware = true self.logger = Logger.new(STDOUT) + self.use_vcr = false + self.vcr_record_mode = :new_episodes end def tagged_logged diff --git a/lib/cypress_on_rails/middleware.rb b/lib/cypress_on_rails/middleware.rb index 33541b9..455467f 100644 --- a/lib/cypress_on_rails/middleware.rb +++ b/lib/cypress_on_rails/middleware.rb @@ -17,13 +17,7 @@ def call(env) if request.path.start_with?('/__cypress__/command') configuration.tagged_logged { handle_command(request) } else - if defined?(VCR) - VCR.use_cassette('foobar', { :record => :new_episodes }) do - @app.call(env) - end - else - @app.call(env) - end + VCRWrapper.new(app: @app, env: @env).run_with_cassette end end diff --git a/lib/cypress_on_rails/vcr_wrapper.rb b/lib/cypress_on_rails/vcr_wrapper.rb new file mode 100644 index 0000000..154f2b3 --- /dev/null +++ b/lib/cypress_on_rails/vcr_wrapper.rb @@ -0,0 +1,42 @@ +require 'rack' +require 'cypress_on_rails/configuration' + +module CypressOnRails + class VCRWrapper + + def initialize(app:, env:) + @app = app + @env = env + @request = Rack::Request.new(env) + end + + def run_with_cassette + if defined?(VCR) && configuration.use_vcr + VCR.use_cassette(cassette_name, { :record => configuration.vcr_record_mode }) do + logger.info "Handle request with cassette name: #{cassette_name}" + @app.call(env) + end + else + @app.call(env) + end + end + + private + + def configuration + CypressOnRails.configuration + end + + def logger + configuration.logger + end + + def cassette_name + if request.path.start_with?('/graphql') && request.params.key?(:operation) + "#{request.path}/#{request.params[:operation]}" + else + request.path + end + end + end +end diff --git a/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb b/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb index be6ad63..3068b95 100644 --- a/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb +++ b/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb @@ -5,6 +5,20 @@ if defined?(CypressOnRails) # please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0 c.use_middleware = !Rails.env.production? c.logger = Rails.logger + c.use_vcr = ENV['WITH_VCR'].present? + + # # Setup VCR to mock external HTTP requests + # if ENV['WITH_VCR'].present? + # # c.vcr_record_mode = :once # Use to choose VCR record mode (:new_episodes by default) + # + # require 'vcr' + # VCR.configure do |config| + # config.cassette_library_dir = Rails.root.join('spec', 'cypress', 'fixtures', 'cassettes') # Update cassettes path as nedded + # config.hook_into :webmock + # config.ignore_localhost = true + # config.ignore_hosts('localhost', '127.0.0.1', '0.0.0.0') + # end + # end end # # if you compile your asssets on CI From d86a07ccb3281d899eb946ed87f1342b850e95d1 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Wed, 15 Dec 2021 18:32:33 +0200 Subject: [PATCH 3/5] Update VCR wrapper --- lib/cypress_on_rails/middleware.rb | 4 +++- lib/cypress_on_rails/vcr_wrapper.rb | 16 +++++--------- .../spec/cypress/support/commands.js | 22 +++++++++++++++++++ .../spec/cypress/support/on-rails.js | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/cypress_on_rails/middleware.rb b/lib/cypress_on_rails/middleware.rb index 455467f..1707447 100644 --- a/lib/cypress_on_rails/middleware.rb +++ b/lib/cypress_on_rails/middleware.rb @@ -16,8 +16,10 @@ def call(env) request = Rack::Request.new(env) if request.path.start_with?('/__cypress__/command') configuration.tagged_logged { handle_command(request) } + elsif defined?(VCR) && configuration.use_vcr + VCRWrapper.new(app: @app, env: env).run_with_cassette else - VCRWrapper.new(app: @app, env: @env).run_with_cassette + @app.call(env) end end diff --git a/lib/cypress_on_rails/vcr_wrapper.rb b/lib/cypress_on_rails/vcr_wrapper.rb index 154f2b3..6449d16 100644 --- a/lib/cypress_on_rails/vcr_wrapper.rb +++ b/lib/cypress_on_rails/vcr_wrapper.rb @@ -11,13 +11,9 @@ def initialize(app:, env:) end def run_with_cassette - if defined?(VCR) && configuration.use_vcr - VCR.use_cassette(cassette_name, { :record => configuration.vcr_record_mode }) do - logger.info "Handle request with cassette name: #{cassette_name}" - @app.call(env) - end - else - @app.call(env) + VCR.use_cassette(cassette_name, { :record => configuration.vcr_record_mode }) do + logger.info "Handle request with cassette name: #{cassette_name}" + @app.call(@env) end end @@ -32,10 +28,10 @@ def logger end def cassette_name - if request.path.start_with?('/graphql') && request.params.key?(:operation) - "#{request.path}/#{request.params[:operation]}" + if @request.path.start_with?('/graphql') && @request.params.key?('operation') + "#{@request.path}/#{@request.params['operation']}" else - request.path + @request.path end end end diff --git a/lib/generators/cypress_on_rails/templates/spec/cypress/support/commands.js b/lib/generators/cypress_on_rails/templates/spec/cypress/support/commands.js index c1f5a77..40caa52 100644 --- a/lib/generators/cypress_on_rails/templates/spec/cypress/support/commands.js +++ b/lib/generators/cypress_on_rails/templates/spec/cypress/support/commands.js @@ -23,3 +23,25 @@ // // -- This is will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) +// +// +// -- This is for Graphql usage. Add proxy-like mock to add operation name into query string -- +// Cypress.Commands.add('mockGraphQL', () => { +// cy.on('window:before:load', (win) => { +// const originalFetch = win.fetch; +// const fetch = (path, options, ...rest) => { +// if (options && options.body) { +// try { +// const body = JSON.parse(options.body); +// if (body.operationName) { +// return originalFetch(`${path}?operation=${body.operationName}`, options, ...rest); +// } +// } catch (e) { +// return originalFetch(path, options, ...rest); +// } +// } +// return originalFetch(path, options, ...rest); +// }; +// cy.stub(win, 'fetch', fetch); +// }); +// }); diff --git a/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js b/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js index f5b8f42..23723f4 100644 --- a/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js +++ b/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js @@ -44,6 +44,7 @@ Cypress.Commands.add('appFixtures', function (options) { // The next is optional // beforeEach(() => { // cy.app('clean') // have a look at cypress/app_commands/clean.rb +// cy.mockGraphQL(); // for GraphQL usage, see cypress/support/commands.rb // }); // comment this out if you do not want to attempt to log additional info on test fail From 8ea89ec55196a4118130b8bfbf0f65da8b339909 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Wed, 15 Dec 2021 18:40:12 +0200 Subject: [PATCH 4/5] Add VCR wrapper require call --- lib/cypress_on_rails/middleware.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cypress_on_rails/middleware.rb b/lib/cypress_on_rails/middleware.rb index 1707447..21b0726 100644 --- a/lib/cypress_on_rails/middleware.rb +++ b/lib/cypress_on_rails/middleware.rb @@ -2,6 +2,7 @@ require 'rack' require 'cypress_on_rails/configuration' require 'cypress_on_rails/command_executor' +require 'cypress_on_rails/vcr_wrapper' module CypressOnRails # Middleware to handle cypress commands and eval From 23cef9fc4f75d60abca908205b479f62d72f2f87 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Wed, 19 Apr 2023 18:18:41 +0200 Subject: [PATCH 5/5] Remove old exists? --- lib/cypress_on_rails/middleware.rb | 2 +- lib/generators/cypress_on_rails/install_generator.rb | 2 +- spec/integrations/rails_3_2/config/boot.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cypress_on_rails/middleware.rb b/lib/cypress_on_rails/middleware.rb index 21b0726..dffebfa 100644 --- a/lib/cypress_on_rails/middleware.rb +++ b/lib/cypress_on_rails/middleware.rb @@ -56,7 +56,7 @@ def handle_command(req) body = JSON.parse(req.body.read) logger.info "handle_command: #{body}" commands = Command.from_body(body, configuration) - missing_command = commands.find {|command| !@file.exists?(command.file_path) } + missing_command = commands.find {|command| !@file.exist?(command.file_path) } if missing_command.nil? begin diff --git a/lib/generators/cypress_on_rails/install_generator.rb b/lib/generators/cypress_on_rails/install_generator.rb index f5b6b08..83319e7 100644 --- a/lib/generators/cypress_on_rails/install_generator.rb +++ b/lib/generators/cypress_on_rails/install_generator.rb @@ -7,7 +7,7 @@ class InstallGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def install_cypress - if !Dir.exists?(options.cypress_folder) || Dir["#{options.cypress_folder}/*"].empty? + if !Dir.exist?(options.cypress_folder) || Dir["#{options.cypress_folder}/*"].empty? directories = options.cypress_folder.split('/') directories.pop install_dir = "#{Dir.pwd}/#{directories.join('/')}" diff --git a/spec/integrations/rails_3_2/config/boot.rb b/spec/integrations/rails_3_2/config/boot.rb index 4489e58..f2830ae 100644 --- a/spec/integrations/rails_3_2/config/boot.rb +++ b/spec/integrations/rails_3_2/config/boot.rb @@ -3,4 +3,4 @@ # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) -require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])