Class: Capybara::Driver::Akephalos

Inherits:
Base
  • Object
show all
Defined in:
lib/akephalos/capybara.rb

Overview

Driver class exposed to Capybara. It implements Capybara’s full driver API, and is the entry point for interaction between the test suites and HtmlUnit.

This class and Capybara::Driver::Akephalos::Node are written to run on both MRI and JRuby, and is agnostic whether the Akephalos::Client instance is used directly or over DRb.

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Akephalos

Creates a new instance of the Akephalos Driver for Capybara. The driver is registered with Capybara by a name, so that it can be chosen when Capybara’s javascript_driver is changed. By default, Akephalos is registered like this:

Capybara.register_driver :akephalos do |app|
  Capybara::Akephalos::Driver.new(
    app,
    :browser => :firefox_3_6,
    :validate_scripts => true
  )
end

Parameters:

  • app

    the Rack application to run

  • options (Hash) (defaults to: {})

    the Akephalos configuration options

Options Hash (options):

  • :browser (Symbol) — default: :firefox_3_6

    the browser compatibility mode to run in. Available options:

    :firefox_3_6
    :firefox_3
    :ie6
    :ie7
    :ie8
    
  • :validate_scripts (true, false) — default: true

    whether to raise exceptions on script errors



191
192
193
194
195
196
# File 'lib/akephalos/capybara.rb', line 191

def initialize(app, options = {})
  @app = app
  @options = options
  @rack_server = Capybara::Server.new(@app)
  @rack_server.boot if Capybara.run_server
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



163
164
165
# File 'lib/akephalos/capybara.rb', line 163

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



163
164
165
# File 'lib/akephalos/capybara.rb', line 163

def options
  @options
end

#rack_serverObject (readonly)

Returns the value of attribute rack_server.



163
164
165
# File 'lib/akephalos/capybara.rb', line 163

def rack_server
  @rack_server
end

Instance Method Details

#bodyString

page.modified_source will return a string with html entities converted into the unicode equivalent but the string will be marked as ASCII-8BIT which causes conversion issues so we force the encoding to UTF-8 (ruby 1.9 only)

Returns:

  • (String)

    the page’s modified source



216
217
218
219
220
221
222
223
224
# File 'lib/akephalos/capybara.rb', line 216

def body
  body_source = page.modified_source

  if body_source.respond_to?(:force_encoding)
    body_source.force_encoding("UTF-8")
  else
    body_source
  end
end

#browserObject

Returns the browser.

Returns:

  • the browser



295
296
297
# File 'lib/akephalos/capybara.rb', line 295

def browser
  @browser ||= Akephalos::Client.new(@options)
end

#cleanup!Object

Deprecated.

This method is deprecated in Capybara’s master branch. Use #reset! instead.

Clear all cookie session data.



249
250
251
# File 'lib/akephalos/capybara.rb', line 249

def cleanup!
  reset!
end

#cookiesObject

Returns the session cookies.

Returns:

  • the session cookies



300
301
302
# File 'lib/akephalos/capybara.rb', line 300

def cookies
  browser.cookies
end

#current_urlString

Returns the page’s current URL.

Returns:

  • (String)

    the page’s current URL



259
260
261
# File 'lib/akephalos/capybara.rb', line 259

def current_url
  page.current_url
end

#evaluate_script(script) ⇒ Object

Execute JavaScript against the current page and return the results.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • the result of the JavaScript



285
286
287
# File 'lib/akephalos/capybara.rb', line 285

def evaluate_script(script)
  page.evaluate_script script
end

#execute_script(script) ⇒ nil

Execute JavaScript against the current page, discarding any return value.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • (nil)


277
278
279
# File 'lib/akephalos/capybara.rb', line 277

def execute_script(script)
  page.execute_script script
end

#find(selector) ⇒ Array<Node>

Search for nodes which match the given XPath selector.

Parameters:

  • selector (String)

    XPath query

Returns:

  • (Array<Node>)

    the matched nodes



267
268
269
270
271
# File 'lib/akephalos/capybara.rb', line 267

def find(selector)
  nodes = []
  page.find(selector).each { |node| nodes << Node.new(self, node) }
  nodes
end

#pageObject

Returns the current page.

Returns:

  • the current page



290
291
292
# File 'lib/akephalos/capybara.rb', line 290

def page
  browser.page
end

#reset!Object

Clear all cookie session data.



254
255
256
# File 'lib/akephalos/capybara.rb', line 254

def reset!
  cookies.clear
end

#response_headersHash{String => String}

Returns the page’s response headers.

Returns:

  • (Hash{String => String})

    the page’s response headers



227
228
229
# File 'lib/akephalos/capybara.rb', line 227

def response_headers
  page.response_headers
end

#sourceString

Returns the page’s original source.

Returns:

  • (String)

    the page’s original source



206
207
208
# File 'lib/akephalos/capybara.rb', line 206

def source
  page.source
end

#status_codeInteger

Returns the response’s status code.

Returns:

  • (Integer)

    the response’s status code



232
233
234
# File 'lib/akephalos/capybara.rb', line 232

def status_code
  page.status_code
end

#user_agentString

Returns the current user agent string.

Returns:

  • (String)

    the current user agent string



305
306
307
# File 'lib/akephalos/capybara.rb', line 305

def user_agent
  browser.user_agent
end

#user_agent=(user_agent) ⇒ Object

Set the User-Agent header for this session. If :default is given, the User-Agent header will be reset to the default browser’s user agent.

Parameters:

  • user_agent (:default)

    the default user agent

  • user_agent (String)

    the user agent string to use



314
315
316
# File 'lib/akephalos/capybara.rb', line 314

def user_agent=(user_agent)
  browser.user_agent = user_agent
end

#visit(path) ⇒ Object

Visit the given path in the browser.

Parameters:

  • path (String)

    relative path to visit



201
202
203
# File 'lib/akephalos/capybara.rb', line 201

def visit(path)
  browser.visit(url(path))
end

#waitfalse

Disable waiting in Capybara, since waiting is handled directly by Akephalos.

Returns:

  • (false)


322
323
324
# File 'lib/akephalos/capybara.rb', line 322

def wait
  false
end

#within_frame(frame_id, &block) ⇒ Object

Execute the given block within the context of a specified frame.

Parameters:

  • frame_id (String)

    the frame’s id

Raises:

  • (Capybara::ElementNotFound)

    if the frame is not found



240
241
242
243
244
# File 'lib/akephalos/capybara.rb', line 240

def within_frame(frame_id, &block)
  unless page.within_frame(frame_id, &block)
    raise Capybara::ElementNotFound, "Unable to find frame with id '#{frame_id}'"
  end
end