Class: Kookaburra::UIDriver::UIComponent Abstract

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
HasUIComponents
Includes:
Assertion
Defined in:
lib/kookaburra/ui_driver/ui_component.rb,
lib/kookaburra/ui_driver/ui_component/address_bar.rb

Overview

This class is abstract.

Subclass and implement (at least) #component_locator. Unless you override the default implementation of #url, you must also override the #component_path method.

Note:

Even though a UIComponent should respond to all of the methods on the browser (i.e. all of the Capybara DSL methods), for some reason call to #select get routed to Kernel#select. You can get around this by calling it as self.select. See https://gist.github.com/3192103 for an example of this behavior.

UIComponent is intended to be subclassed to represent each component of your application-under-test's user interface. The purpose of the UIComponent object is to abstract away the implementation details of your interface when testing and allow you to concentrate on testing your business requirements. For instance, a UIComponent subclass for your sign-up form might have accessors for the individual fields as well as methods that allow you to perform distinct operations:

Note that the "browser operation" methods such as #fill_in and #click_button are delegated to a ScopedBrowser and are automatically scoped to the component's DOM element.

Examples:

SignUpForm component

class SignUpForm < Kookaburra::UIDriver::UIComponent
  def component_path
    '/signup'
  end

  def component_locator
    '#sign_up_form'
  end

  def email
    find('#user_email').value
  end

  def email=(new_email)
    fill_in 'user_email', :with => new_email
  end

  def password
    find('#user_password').value
  end

  def password=(new_password)
    fill_in 'user_password', :with => new_password
  end

  def password_confirmation
    find('#user_password_confirmation').value
  end

  def password_confirmation=(new_password_confirmation)
    fill_in 'user_password_confirmation', :with => new_password_confirmation
  end

  def submit
    click_button 'Sign Up'
  end

  def (data = {})
    self.email = data[:email]
    self.password = data[:password]
    self.password_confirmation = data[:password_confirmation]
    submit
  end
end

Direct Known Subclasses

AddressBar

Defined Under Namespace

Classes: AddressBar

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasUIComponents

ui_component

Methods included from Assertion

#assert

Constructor Details

#initialize(configuration, options = {}) ⇒ UIComponent

New UIComponent instances are typically created for you by your Kookaburra::UIDriver instance.

Parameters:

See Also:

  • Kookaburra::UIDriver.ui_component


96
97
98
99
100
101
102
103
104
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 96

def initialize(configuration, options = {})
  @configuration = configuration
  @options = options
  @browser = configuration.browser
  @app_host = configuration.app_host
  @server_error_detection = configuration.server_error_detection
  scoped_browser = ScopedBrowser.new(@browser, lambda { component_locator })
  super(scoped_browser)
end

Instance Attribute Details

#browserObject (readonly, protected)

The browser object from the initialized configuration



125
126
127
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 125

def browser
  @browser
end

#configurationObject (readonly)

The Configuration with which the component instance was instantiated.



82
83
84
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 82

def configuration
  @configuration
end

#optionsObject (readonly)

The options Hash with which the component instance was instantiated.



86
87
88
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 86

def options
  @options
end

Instance Method Details

#component_locatorString (protected)

This method is abstract.

Returns the CSS3 selector that will find the element in the DOM.

Returns:

  • (String)

    the CSS3 selector that will find the element in the DOM

Raises:

  • (Kookaburra::ConfigurationError)

    raised if you haven't provided an implementation



139
140
141
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 139

def component_locator
  raise ConfigurationError, "You must define #{self.class.name}#component_locator."
end

#component_pathString (protected)

This method is abstract.

Returns the URL path that should be loaded in order to reach this component.

Returns:

  • (String)

    the URL path that should be loaded in order to reach this component

Raises:

  • (Kookaburra::ConfigurationError)

    raised if you haven't provided an implementation



131
132
133
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 131

def component_path
  raise ConfigurationError, "You must define #{self.class.name}#component_path."
end

#detect_server_error!Object (protected)

Runs the server error detection function specified in Configuration#server_error_detection.

It's a noop if no server error detection was specified.

Raises:

  • (UnexpectedResponse)

    raised if the server error detection function returns true



150
151
152
153
154
155
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 150

def detect_server_error!
  return if @server_error_detection.nil?
  if @server_error_detection.call(browser)
    raise UnexpectedResponse, "Your server error detection function detected a server error. Looks like your applications is busted. :-("
  end
end

#url(*args) ⇒ Object

Returns the full URL by appending #component_path to the value of the Configuration#app_host from the initialized configuration.



118
119
120
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 118

def url(*args)
  "#{@app_host}#{component_path(*args)}"
end

#visible?Boolean

Is the component's element found on the page and is it considered "visible" by the browser driver.

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/kookaburra/ui_driver/ui_component.rb', line 108

def visible?
  visible = browser.has_css?(component_locator, :visible)
  unless visible
    detect_server_error!
  end
  visible
end