Handle missing link elements

This commit is contained in:
Jonas Nicklas 2009-11-07 18:56:04 +01:00
parent 8706b7641c
commit c3eadc5304
3 changed files with 25 additions and 7 deletions

View File

@ -3,6 +3,7 @@ module Webcat
class WebcatError < StandardError; end class WebcatError < StandardError; end
class DriverNotFoundError < WebcatError; end class DriverNotFoundError < WebcatError; end
class ElementNotFound < WebcatError; end
class << self class << self
attr_accessor :debug attr_accessor :debug

View File

@ -22,13 +22,21 @@ class Webcat::Session
end end
def click_link(locator) def click_link(locator)
link = driver.find("//a[@id='#{locator}']").first find_element("//a[@id='#{locator}']", %{//a[text()="#{locator}"]}, %{//a[@title="#{locator}"]}).click
link ||= driver.find(%{//a[text()="#{locator}"]}).first
link ||= driver.find(%{//a[@title="#{locator}"]}).first
link.click
end end
def body def body
driver.body driver.body
end end
private
def find_element(*locators)
locators.each do |locator|
element = driver.find(locator).first
return element if element
end
raise Webcat::ElementNotFound, "element not found"
end
end end

View File

@ -18,9 +18,12 @@ describe Webcat::Session do
end end
describe '#click_link' do describe '#click_link' do
before do
@session.visit('/with_html')
end
context "with id given" do context "with id given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.visit('/with_html')
@session.click_link('foo') @session.click_link('foo')
@session.body.should == 'Another World' @session.body.should == 'Another World'
end end
@ -28,7 +31,6 @@ describe Webcat::Session do
context "with text given" do context "with text given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.visit('/with_html')
@session.click_link('labore') @session.click_link('labore')
@session.body.should == '<h1>Bar</h1>' @session.body.should == '<h1>Bar</h1>'
end end
@ -36,11 +38,18 @@ describe Webcat::Session do
context "with title given" do context "with title given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.visit('/with_html')
@session.click_link('awesome title') @session.click_link('awesome title')
@session.body.should == '<h1>Bar</h1>' @session.body.should == '<h1>Bar</h1>'
end end
end end
context "with a locator that doesn't exist" do
it "should raise an error" do
running do
@session.click_link('does not exist')
end.should raise_error(Webcat::ElementNotFound)
end
end
end end
end end