Permalink
Browse files

Rewrite API for ActionSystemTest

This is a major rewrite of what existed previously. After discussing
this feature with DHH I realized that I was looking at the setup all
wrong.

I had originally mentally broken it into "what Rails wants" and "what
Capybara already has".

What happened after looking at it from DHH's angle was that I saw there
was no reason to group settings by Driver but instead the following
groups:

- There will always be a `Driver`
  - This can selenium, poltergeist, or capybara webkit. Capybara already
    provides all of these and there's no reason to break them into a
    category of Rails' usese Selenium like this and Capybara uses it
    like that.
- Only Selenium drivers care about `Browser`
  - Because of this it was weird to set it only in the Rails end.
  - Therefore only `Browser`, and not `Driver` cares about
    `screen_size`.
- Puma is the default `Server` in Rails
  - Therefore there's no reason to explictly support Webkit

Once I looked at it from this angle I was able to abstract all the
settings away from grouping the drivers with their options.

Now all the driver, server, and browser settings are abstracted away and
not part of the public facing API.

This means there's no requirement to initialize new classes to change
the default settings and the public options API is much smaller.

All of Rails preferred defaults are still there (selenium with port
21800 using the chrome browser with a screen size of 1400x1400) but
changing these no longer requires initializing a new class or
understanding which driver you're using underneath (rails defaults or
capybaras defaults respectively). Rails opinions are now simple defaults
instead of doing a them versus us setup with Drivers and explicit
options.

Changing the defaults is simple. Call `driven_by` with different
settings to change the defaults which will on their own initialize new
classes and change the default settings.

Use poltergeist with port 3000 for Puma

```
driven_by :poltergeist, on: 3000
```

Use selenium with the Chrome browser and a screen size of 800x800
```
driven_by :selenium, using: :firefox, screen_size: [ 800, 800 ]
```

The entire setup of how browser and drivers interact with each other are
abstracted away and the only required argument is the driver name.
  • Loading branch information...
eileencodes committed Jan 22, 2017
1 parent 62c7d98 commit 3dfbe7e4e5057d56c636b8231e8d00a06a15796b
@@ -50,18 +50,39 @@
# driver defaults whereas the <tt>RailsSeleniumDriver</tt> has pre-set
# configuration for browser, server, port, etc.
require "action_system_test/test_helper"
require "action_system_test/driver_adapter"
require "capybara/dsl"
require "action_controller"
require "action_system_test/driver"
require "action_system_test/browser"
require "action_system_test/server"
require "action_system_test/test_helpers/screenshot_helper"
module ActionSystemTest
include ActionSystemTest::TestHelper
include ActionSystemTest::DriverAdapter
DEFAULT_DRIVER = :rails_selenium_driver
include Capybara::DSL
include ActionSystemTest::TestHelpers::ScreenshotHelper
class Base < ActionDispatch::IntegrationTest
include ActionSystemTest
ActionSystemTest.driver = DEFAULT_DRIVER
def self.start_application # :nodoc:
Capybara.app = Rack::Builder.new do
map "/" do
run Rails.application
end
end
end
def self.driven_by(driver, using: :chrome, on: 21800, screen_size: [1400, 1400])
Driver.new(driver).run
Server.new(on).run
Browser.new(using, screen_size).run if selenium?(driver)
end
def self.selenium?(driver) # :nodoc:
driver == :selenium
end
end
Base.start_application
Base.driven_by :selenium
end
@@ -0,0 +1,26 @@
module ActionSystemTest
class Browser
def initialize(name, screen_size)
@name = name
@screen_size = screen_size
end
def run
register
setup
end
private
def register
Capybara.register_driver @name do |app|
Capybara::Selenium::Driver.new(app, browser: @name).tap do |driver|
driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size)
end
end
end
def setup
Capybara.default_driver = @name.to_sym
end
end
end
@@ -0,0 +1,16 @@
module ActionSystemTest
class Driver # :nodoc:
def initialize(name)
@name = name
end
def run
register
end
private
def register
Capybara.default_driver = @name
end
end
end

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 3dfbe7e

Please sign in to comment.