cypress-on-rails 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 307be16313426be599e503b2612ab3f33c8d4b54
4
- data.tar.gz: 81a10e2d6563f57cc3e2a5cb71d0547d2366b278
3
+ metadata.gz: 5043512be34402217f8e93420dafe69700bab374
4
+ data.tar.gz: 645216c886a702ea0713a890967965d8043fa480
5
5
  SHA512:
6
- metadata.gz: baa22d4846debaec324dc1641410a27e523665fea8bcf70624d165955809ec03054acd81564cb3075f6ff6c4ed1b29014fe23746f6bfa0a6f5ab8a0167d2979e
7
- data.tar.gz: 2fe3bb98ea96886a22d509a9881127cb10faa505b172734e41993063ebf5d6e9ee128a3bad2ee6884f3dc7e9585ef423ca0019ff5b3568f26805ed99801b0251
6
+ metadata.gz: 408182ff3eab4bb571cc2c7f969c564968d7f7e4bf96e7e179fd4bec4c6c27380d75586522c1dc59990b5dae0549481e67c1dcbff888148fa1d6fef1ca3c09eb
7
+ data.tar.gz: 39588bdf7da33febdd7184c290ecb062d9ee9402ab3a67f06f9ad4947cc236db3a63abaf9886514202cd36c05c8ad5971f0b0c1a83a5dbf712724ce63c961921
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.0 (2017-11-05)
2
+ ### Changed
3
+ * remove the need for a seperate port for the setup calls. Requires rerunning `cypress:install` generator
4
+
1
5
  ## 0.1.5 (2017-11-01)
2
6
 
3
7
  ### Added
data/README.md CHANGED
@@ -9,7 +9,7 @@ Proof-of-Concept gem for using [cypress.io](http://github.com/cypress-io/) in Ra
9
9
 
10
10
  Add this to your Gemfile:
11
11
  ```
12
- group :test do
12
+ group :test, :development do
13
13
  gem 'cypress-on-rails'
14
14
  end
15
15
  ```
@@ -41,6 +41,7 @@ When writing End-to-End tests, you will probably want to prepare your database t
41
41
  ### Using embedded ruby
42
42
  You can embed ruby code in your test file. This code will then be executed in the context of your application. For example:
43
43
 
44
+ ```
44
45
  // spec/cypress/integrations/simple_spec.js
45
46
  describe('My First Test', function() {
46
47
  it('visit root', function() {
@@ -55,6 +56,7 @@ describe('My First Test', function() {
55
56
  cy.contains("Cypress Hill")
56
57
  })
57
58
  })
59
+ ```
58
60
 
59
61
  Use the (`) backtick string syntax to allow multiline strings.
60
62
 
data/bin/cypress CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'cypress'
3
- Cypress::Server.new(ARGV).run
3
+ Cypress::Runner.new(ARGV).run
data/lib/cypress.rb CHANGED
@@ -3,12 +3,11 @@ require 'active_support/dependencies/autoload'
3
3
  module Cypress
4
4
  extend ActiveSupport::Autoload
5
5
 
6
- autoload :Server, 'cypress/server'
7
- autoload :CallbackServer, 'cypress/callback_server'
8
6
  autoload :Runner, 'cypress/runner'
9
7
  autoload :ScenarioBank, 'cypress/scenario_bank'
10
8
  autoload :ScenarioContext, 'cypress/scenario_context'
11
9
  autoload :Configuration, 'cypress/configuration'
10
+ autoload :Middleware, 'cypress/middleware'
12
11
 
13
12
  def self.configuration
14
13
  @configuration ||= Configuration.new
@@ -17,6 +16,10 @@ module Cypress
17
16
  def self.configure(&block)
18
17
  yield configuration if block_given?
19
18
  end
19
+
20
+ def self.run_middleware?
21
+ Rails.env.test?
22
+ end
20
23
  end
21
24
 
22
25
  if defined?(Rails)
@@ -1,11 +1,17 @@
1
1
  module Cypress
2
2
  class Configuration
3
- attr_accessor :test_framework, :db_resetter, :cache_classes
3
+ attr_accessor :test_framework, :db_resetter, :cache_classes, :server_port
4
4
 
5
- def initialize
5
+ def initialize(args=[])
6
+ setup(args)
7
+ end
8
+
9
+ def setup(args)
10
+ @args = args
6
11
  @test_framework = :rspec
7
12
  @db_resetter = :database_cleaner
8
13
  @before = proc {}
14
+ @cache_classes = run_mode == 'run'
9
15
  end
10
16
 
11
17
  def include(mod)
@@ -20,14 +26,16 @@ module Cypress
20
26
  end
21
27
  end
22
28
 
23
- def cache_classes
24
- !! @cache_classes
29
+ def run_mode
30
+ if @args.first == 'run'
31
+ 'run'
32
+ else
33
+ 'open'
34
+ end
25
35
  end
26
36
 
27
- def disable_class_caching
28
- if @cache_classes.nil?
29
- @cache_classes = false
30
- end
37
+ def load_support
38
+ require "./spec/cypress/cypress_helper"
31
39
  end
32
40
  end
33
41
  end
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+
3
+ module Cypress
4
+ class Middleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ if env['REQUEST_PATH'].starts_with?('/__cypress__/')
11
+ path = env['REQUEST_PATH'].sub('/__cypress__/', '')
12
+ cmd = path.split('/').first
13
+ if respond_to?("handle_#{cmd}", true)
14
+ send "handle_#{cmd}", Rack::Request.new(env)
15
+ [201, {}, ["success"]]
16
+ else
17
+ [404, {}, ["unknown command: #{cmd}"]]
18
+ end
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+
24
+ private
25
+ def configuration
26
+ Cypress.configuration
27
+ end
28
+
29
+ def new_context
30
+ ScenarioContext.new(configuration)
31
+ end
32
+
33
+ def handle_setup(req)
34
+ reset_rspec if configuration.test_framework == :rspec
35
+ call_database_cleaner if configuration.db_resetter == :database_cleaner
36
+ new_context.execute configuration.before
37
+ end
38
+
39
+ def reset_rspec
40
+ require 'rspec/rails'
41
+ RSpec::Mocks.teardown
42
+ RSpec::Mocks.setup
43
+ end
44
+
45
+ def call_database_cleaner
46
+ require 'database_cleaner'
47
+ DatabaseCleaner.strategy = :truncation
48
+ DatabaseCleaner.clean
49
+ end
50
+
51
+ def json_from_body(req)
52
+ JSON.parse(req.body.read)
53
+ end
54
+
55
+ def handle_scenario(req)
56
+ handle_setup(req)
57
+
58
+ @scenario_bank.load
59
+ if block = @scenario_bank[json_from_body(req)['scenario']]
60
+ new_context.execute block
61
+ end
62
+ end
63
+
64
+ def handle_eval(req)
65
+ new_context.execute json_from_body(req)['code']
66
+ end
67
+ end
68
+ end
@@ -1,5 +1,13 @@
1
+ require 'rails/railtie'
1
2
  module Cypress
2
3
  class Railtie < Rails::Railtie
4
+ initializer :setup_cypress_middleware do |app|
5
+ if Cypress.run_middleware?
6
+ Cypress.configuration.load_support
7
+ app.middleware.use Middleware
8
+ end
9
+ end
10
+
3
11
  generators do
4
12
  require_relative '../generators/install_generator'
5
13
  end
@@ -1,12 +1,14 @@
1
1
  module Cypress
2
2
  class Runner
3
- def initialize(owner, callback_url)
4
- @owner = owner
5
- @callback_url = callback_url
3
+ def initialize(args)
4
+ @args = args
5
+ @scenario_bank = ScenarioBank.new
6
6
  end
7
7
 
8
- def run(server_port)
9
- Open3.popen2(*cypress_cli(server_port)) do |sin, sout, status|
8
+ def run
9
+ configuration.setup(@args)
10
+ boot_rails
11
+ Open3.popen2(*cypress_cli) do |sin, sout, status|
10
12
  sout.each_line do |line|
11
13
  puts "CYPRESS: #{line}"
12
14
  end
@@ -14,10 +16,24 @@ module Cypress
14
16
  end
15
17
 
16
18
  private
17
- def cypress_cli(server_port)
19
+ def configuration
20
+ Cypress.configuration
21
+ end
22
+
23
+ def boot_rails
24
+ require 'cypress/railtie'
25
+ configuration.load_support
26
+ require 'capybara/rails'
27
+
28
+ Capybara.current_driver = :selenium # oh, the irony....
29
+ Capybara.server = :puma
30
+ configuration.server_port = Capybara.current_session.server.port
31
+ end
32
+
33
+ def cypress_cli
18
34
  result = ['yarn', 'run']
19
- result += ['cypress', @owner.mode]
20
- result += ['--env', "SERVER_PORT=#{server_port},CALLBACK=#{@callback_url}"]
35
+ result += ['cypress', configuration.run_mode]
36
+ result += ['--env', "SERVER_PORT=#{configuration.server_port}"]
21
37
  result += ['-c', 'videosFolder=spec/cypress/videos,fixturesFolder=spec/cypress/fixtures,integrationFolder=spec/cypress/integrations/,supportFile=spec/cypress/support/setup.js']
22
38
  result
23
39
  end
@@ -1,3 +1,3 @@
1
1
  module Cypress
2
- VERSION = "0.1.5".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -12,7 +12,7 @@ module Cypress
12
12
  ]
13
13
  gsub_file 'config/environments/test.rb', 'config.cache_classes = true', replace.join("\n")
14
14
 
15
- create_file "spec/cypress/cypress_helper.rb", <<-FILE
15
+ create_file "spec/cypress/cypress_helper.rb", <<-EOF
16
16
  ENV['RAILS_ENV'] ||= 'test'
17
17
  require File.expand_path('../../../config/environment', __FILE__)
18
18
 
@@ -31,7 +31,7 @@ Cypress.configure do |c|
31
31
  # add a module to your run context
32
32
  # c.include MyModule
33
33
  end
34
- FILE
34
+ EOF
35
35
 
36
36
  create_file "spec/cypress/integrations/simple_spec.js", <<-FILE
37
37
  describe('My First Test', function() {
@@ -55,17 +55,17 @@ FILE
55
55
 
56
56
  create_file "spec/cypress/support/setup.js", <<-FILE
57
57
  // cypress-on-rails: dont remove these command
58
- Cypress.Commands.add('setupRails', function () {
59
- cy.request('POST', Cypress.env("CALLBACK") + "/setup")
60
- });
61
-
62
58
  Cypress.Commands.add('setupScenario', function(name) {
63
59
  Cypress.log({ message: name })
64
- cy.request('POST', Cypress.env("CALLBACK")+"/scenario", JSON.stringify({ scenario: name }))
60
+ cy.request('POST', 'http://localhost:' + Cypress.env("SERVER_PORT") + "/__cypress__/scenario", JSON.stringify({ scenario: name }))
61
+ });
62
+
63
+ Cypress.Commands.add('setupRails', function () {
64
+ cy.request('POST', 'http://localhost:' + Cypress.env("SERVER_PORT") + "/__cypress__/setup")
65
65
  });
66
66
 
67
67
  Cypress.Commands.add('rails', function(code) {
68
- cy.request('POST', Cypress.env("CALLBACK") + "/eval", JSON.stringify({ code: code }))
68
+ cy.request('POST', 'http://localhost:' + Cypress.env("SERVER_PORT") + '/__cypress__/eval', JSON.stringify({ code: code }))
69
69
  })
70
70
  // cypress-on-rails: end
71
71
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cypress-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - miceportal team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-01 00:00:00.000000000 Z
11
+ date: 2017-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -69,13 +69,12 @@ files:
69
69
  - cypress-on-rails.gemspec
70
70
  - lib/cypress-on-rails.rb
71
71
  - lib/cypress.rb
72
- - lib/cypress/callback_server.rb
73
72
  - lib/cypress/configuration.rb
73
+ - lib/cypress/middleware.rb
74
74
  - lib/cypress/railtie.rb
75
75
  - lib/cypress/runner.rb
76
76
  - lib/cypress/scenario_bank.rb
77
77
  - lib/cypress/scenario_context.rb
78
- - lib/cypress/server.rb
79
78
  - lib/cypress/version.rb
80
79
  - lib/generators/install_generator.rb
81
80
  homepage: http://github.com/konvenit/cypress-on-rails
@@ -1,36 +0,0 @@
1
- require 'webrick'
2
- require 'json'
3
-
4
- module Cypress
5
- class CallbackServer
6
- attr_reader :port
7
- def initialize(owner)
8
- @port = 9293
9
- @webrick = WEBrick::HTTPServer.new(:Port => port)
10
- @webrick.mount_proc '/setup' do |req, res|
11
- owner.run_command :setup
12
- res.body = ''
13
- end
14
- @webrick.mount_proc '/eval' do |req, res|
15
- owner.run_command :eval, JSON.parse(req.body)
16
- res.body = ''
17
- end
18
- @webrick.mount_proc '/scenario' do |req, res|
19
- owner.run_command :scenario, JSON.parse(req.body)
20
- res.body = ''
21
- end
22
- end
23
-
24
- def start
25
- @webrick.start
26
- end
27
-
28
- def shutdown
29
- @webrick.shutdown
30
- end
31
-
32
- def callback_url
33
- "http://localhost:9293"
34
- end
35
- end
36
- end
@@ -1,82 +0,0 @@
1
- module Cypress
2
- class Server
3
- def initialize(args)
4
- @args = args
5
- @callback_server = CallbackServer.new(self)
6
- @runner = Runner.new self, @callback_server.callback_url
7
- @scenario_bank = ScenarioBank.new
8
- end
9
-
10
- def mode
11
- if @args.first == 'run'
12
- 'run'
13
- else
14
- 'open'
15
- end
16
- end
17
-
18
- def run
19
- server_port = boot_rails
20
-
21
- @callback_thread = Thread.new { @callback_server.start }
22
- @runner_thread = Thread.new { @runner.run server_port }
23
- @runner_thread.join
24
-
25
- @callback_server.shutdown
26
- end
27
-
28
- def run_command(command, options={})
29
- if respond_to?("run_command_#{command}", true)
30
- send "run_command_#{command}", options
31
- end
32
- end
33
-
34
- private
35
- def configuration
36
- Cypress.configuration
37
- end
38
-
39
- def boot_rails
40
- configuration.disable_class_caching if mode == 'open'
41
- require "./spec/cypress/cypress_helper"
42
- require 'capybara/rails'
43
-
44
- Capybara.current_driver = :selenium # oh, the irony....
45
- Capybara.current_session.server.port
46
- end
47
-
48
- def run_command_setup(options={})
49
- reset_rspec if configuration.test_framework == :rspec
50
- call_database_cleaner if configuration.db_resetter == :database_cleaner
51
- new_context.execute configuration.before
52
- end
53
-
54
- def reset_rspec
55
- RSpec::Mocks.teardown
56
- RSpec::Mocks.setup
57
- end
58
-
59
- def call_database_cleaner
60
- require 'database_cleaner'
61
- DatabaseCleaner.strategy = :truncation
62
- DatabaseCleaner.clean
63
- end
64
-
65
- def run_command_scenario(options={})
66
- run_command_setup
67
-
68
- @scenario_bank.load
69
- if block = @scenario_bank[options['scenario']]
70
- new_context.execute block
71
- end
72
- end
73
-
74
- def run_command_eval(options={})
75
- new_context.execute options['code']
76
- end
77
-
78
- def new_context
79
- ScenarioContext.new(configuration)
80
- end
81
- end
82
- end