Sha256: 5ea59a6ad8ed0341b62b3ba701002b705c44e8d965223f4a6d7cafcca3ad7947

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

require 'mechanize'
require 'mechanical-cuke/web_steps'
require 'mechanical-cuke/save_and_open'

module MechanicalCuke
  class << self
    attr_accessor :save_and_open_page_path
    def configure
      yield self
    end
  end

  def mechanize
    @mechanize ||= Mechanize.new
  end

  def get(url)
    mechanize.get(url)
  end

  def current_page
    mechanize.current_page
  end

  def response_body
    current_page.body
  end

  def basic_auth(username,password)
    mechanize.basic_auth(username,password)
  end
end

World(MechanicalCuke)

private
 
def form
  current_page.forms.first
end

def find_by_id(id)
  node = current_page.search("##{id}")
  node.first
end

def find_by_label(field)
  label = current_page.labels.find { |l| l.text == field }
  !label.nil? ? label.for.first : nil
end
    
def find_field(field)
  # Search by id
  if (node = find_by_id(field))
    return form.field_with(:node => node)
  end
  
  # Then by name
  if (f = form.field_with(:name => field))
    return f
  end

  # And finally by label
  if (node = find_by_label(field))
    return form.field_with(:node => node)
  end

  return nil
end

def find_radiobutton(field)
  # Search by id
  if (node = find_by_id(field))
    return form.radiobutton_with(:node => node.first)
  end
  
  # And by label
  if (node = find_by_label(field))
    return form.radiobutton_with(:node => node)
  end

  return nil
end

def find_checkbox(field)
  # Search by id
  if (node = find_by_id(field))
    return form.checkbox_with(:node => node.first)
  end

  # Then by name
  if (f = form.checkbox_with(:name => field))
    return f
  end
  
  # And finally by label
  if (node = find_by_label(field))
    return form.checkbox_with(:node => node)
  end

  return nil
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mechanical-cuke-0.4.3 lib/mechanical-cuke.rb
mechanical-cuke-0.4.2 lib/mechanical-cuke.rb
mechanical-cuke-0.4.1 lib/mechanical-cuke.rb
mechanical-cuke-0.4.0 lib/mechanical-cuke.rb
mechanical-cuke-0.3.1 lib/mechanical-cuke.rb