class Pincers::Chenso::Backend

Attributes

browser[R]
client[R]

Public Class Methods

new(_http_client) click to toggle source
Calls superclass method Pincers::Nokogiri::Backend::new
# File lib/pincers/chenso/backend.rb, line 13
def initialize(_http_client)
  super nil
  @client = _http_client
  @browser = BrowsingManager.new @client
end

Public Instance Methods

as_http_client() click to toggle source
# File lib/pincers/chenso/backend.rb, line 71
def as_http_client
  @client.fork(true)
end
click_on_element(_element, _modifiers) click to toggle source
# File lib/pincers/chenso/backend.rb, line 51
def click_on_element(_element, _modifiers)
  case classify _element
  when :a
    navigate_link _element
  when :option
    select_option _element
  when :input_checkbox
    toggle_checkbox _element
  when :input_radio
    set_radio_button _element
  when :input_submit, :button_submit, :button
    submit_parent_form _element
  end
end
document() click to toggle source
# File lib/pincers/chenso/backend.rb, line 19
def document
  browser.document
end
document_url() click to toggle source
# File lib/pincers/chenso/backend.rb, line 23
def document_url
  browser.current_url
end
merge_http_client(_client) click to toggle source
# File lib/pincers/chenso/backend.rb, line 75
def merge_http_client(_client)
  @client.join _client
  if _client.content and /text\/html/ === _client.content_type
    @browser.push HtmlPageCache.new(_client.uri, _client.content)
  end
end
navigate_back(_steps) click to toggle source
navigate_forward(_steps) click to toggle source
navigate_to(_url) click to toggle source
refresh_document() click to toggle source
# File lib/pincers/chenso/backend.rb, line 39
def refresh_document
  browser.refresh
end
set_element_text(_element, _value) click to toggle source
# File lib/pincers/chenso/backend.rb, line 43
def set_element_text(_element, _value)
  case classify _element
  when :input_text, :input_password, :input_email, :input_number, :textarea
    # TODO: validations?
    set_att _element, :value, _value
  end
end
submit_form(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 66
def submit_form(_element)
  form = new_form _element
  load_in_target form_as_request(form), form.target
end
switch_to_frame(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 82
def switch_to_frame(_element)
  if _element[:src] && !browser.switch_frame(_element[:src])
    browser.load_frame(_element[:src], new_page_request(_element[:src]))
  end
end
switch_to_top_frame() click to toggle source
# File lib/pincers/chenso/backend.rb, line 88
def switch_to_top_frame
  browser.switch_top_frame
end

Private Instance Methods

form_as_request(_form) click to toggle source
# File lib/pincers/chenso/backend.rb, line 162
def form_as_request(_form)
  HtmlFormRequest.new _form
end
load_in_target(_request, _target) click to toggle source
# File lib/pincers/chenso/backend.rb, line 138
def load_in_target(_request, _target)
  case _target
  when nil, '_self'
    browser.push _request
  when '_top'
    browser.switch_top_frame
    browser.push _request
  when '_parent'
    browser.switch_parent_frame
    browser.push _request
  when '_blank'
    # Should be: browser.load_window _request
    browser.switch_top_frame
    browser.push _request
  else
    frame = document.at_xpath("//iframe[@name='#{_target}']")
    switch_to_frame frame
  end
end
new_form(_element, _trigger=nil) click to toggle source
# File lib/pincers/chenso/backend.rb, line 166
def new_form(_element, _trigger=nil)
  Pincers::Core::Helpers::Form.new self, _element, _trigger
end
new_page_request(_url) click to toggle source
# File lib/pincers/chenso/backend.rb, line 158
def new_page_request(_url)
  HtmlPageRequest.new _url
end
select_option(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 107
def select_option(_element)
  select_element = _element.at_xpath('ancestor::select')
  unless get_att(select_element, :multiple)
    select_element.xpath('.//option[@selected]').each { |o| set_att(o, :selected, false) }
    set_att _element, :selected, true
  else
    toggle_att _element, :selected
  end
end
set_radio_button(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 121
def set_radio_button(_element)
  form = _element.at_xpath('ancestor::form')
  if form
    siblings = form.xpath(".//input[@type='radio' and @name='#{get_att(_element, :name)}']")
    siblings.each { |r| set_att(r, :checked, false) }
  end
  set_att _element, :checked, true
end
submit_parent_form(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 130
def submit_parent_form(_element)
  form_element = _element.at_xpath('ancestor::form')
  if form_element
    form = new_form form_element, _element
    load_in_target form_as_request(form), form.target
  end
end
toggle_att(_element, _name) click to toggle source
# File lib/pincers/chenso/backend.rb, line 97
def toggle_att(_element, _name)
  set_att(_element, _name, !get_att(_element, _name))
end
toggle_checkbox(_element) click to toggle source
# File lib/pincers/chenso/backend.rb, line 117
def toggle_checkbox(_element)
  toggle_att _element, :checked
end