Skip to content

Instantly share code, notes, and snippets.

@velenux
Created December 30, 2010 09:25
Show Gist options
  • Save velenux/759623 to your computer and use it in GitHub Desktop.
Save velenux/759623 to your computer and use it in GitHub Desktop.
Watir begin/rescue encapsulation for teh lazy
class Element
attr_reader :description, :id, :xpath, :name, :value, :text
def initialize(opts={})
@description = opts[:description] || nil
@id = opts[:id] || nil
@xpath = opts[:xpath] || nil
@name = opts[:name] || nil
@value = opts[:value] || nil
@text = opts[:text] || nil
@target = opts[:target] || 'div'
@preferred = @xpath || @id || @name || @value || @text || nil
if @preferred.nil?
raise "Didn't find a valid access method, please specify at least one in :id, :xpath, :name, :value or :text"
end
end
def act_with(method)
begin
if @xpath
used = "xpath '#{@xpath}'"
$browser.send(@target, :xpath, @xpath).send(method)
elsif @id
used = "id '#{@id}'"
$browser.send(@target, :id, @id).send(method)
elsif @text
used = "text '#{@text}'"
$browser.send(@target, :text, @text).send(method)
elsif @name
used = "name '#{@name}'"
$browser.send(@target, :name, @name).send(method)
elsif @value
used = "value '#{@value}'"
$browser.send(@target, :value, @value).send(method)
else
used = 'nothing (fail!)'
raise
end
info "Successfull #{method} on #{@target} #{@description} using #{used}"
rescue => e
err "Can't #{method} on #{@target} #{@description} using #{used}: #{e}"
raise
end
end
end
class Link < Element
def initialize(opts={})
super(opts)
@target = 'link' # generates: $browser.link().click() and so on
end
def click
act_with('click')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment