Skip to content

Commit

Permalink
Added node class and made driver's nodes inherit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Nov 17, 2009
1 parent a8789c6 commit c389a0c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def log(message)

autoload :Server, 'capybara/server'
autoload :Session, 'capybara/session'
autoload :Node, 'capybara/node'

module Driver
autoload :RackTest, 'capybara/driver/rack_test_driver'
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/driver/culerity_driver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'culerity'

class Capybara::Driver::Culerity
class Node < Struct.new(:node)
class Node < Capybara::Node
def text
node.text
end
Expand Down Expand Up @@ -61,7 +61,7 @@ def body
end

def find(selector)
browser.elements_by_xpath(selector).map { |node| Node.new(node) }
browser.elements_by_xpath(selector).map { |node| Node.new(self, node) }
end

private
Expand Down
12 changes: 6 additions & 6 deletions lib/capybara/driver/rack_test_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'nokogiri'

class Capybara::Driver::RackTest
class Node < Struct.new(:session, :node)
class Node < Capybara::Node
def text
node.text
end
Expand All @@ -16,7 +16,7 @@ def set(value)
if tag_name == 'input' and %w(text password hidden file).include?(type)
node['value'] = value.to_s
elsif tag_name == 'input' and type == 'radio'
session.html.xpath("//input[@name='#{self[:name]}']").each { |node| node.remove_attribute("checked") }
driver.html.xpath("//input[@name='#{self[:name]}']").each { |node| node.remove_attribute("checked") }
node['checked'] = 'checked'
elsif tag_name == 'input' and type == 'checkbox'
if value
Expand All @@ -36,9 +36,9 @@ def select(option)

def click
if tag_name == 'a'
session.visit(self[:href])
driver.visit(self[:href])
elsif tag_name == 'input' and %w(submit image).include?(type)
Form.new(session, form).submit(self)
Form.new(driver, form).submit(self)
end
end

Expand Down Expand Up @@ -100,9 +100,9 @@ def params(button)

def submit(button)
if post?
session.submit(node['action'].to_s, params(button))
driver.submit(node['action'].to_s, params(button))
else
session.visit(node['action'].to_s + '?' + params(button))
driver.visit(node['action'].to_s + '?' + params(button))
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/driver/selenium_driver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'selenium-webdriver'

class Capybara::Driver::Selenium
class Node < Struct.new(:node)
class Node < Capybara::Node
def text
node.text
end
Expand Down Expand Up @@ -77,7 +77,7 @@ def body
end

def find(selector)
driver.find_elements(:xpath, selector).map { |node| Node.new(node) }
driver.find_elements(:xpath, selector).map { |node| Node.new(self, node) }
end

private
Expand Down
36 changes: 36 additions & 0 deletions lib/capybara/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Capybara::Node
attr_reader :driver, :node

def initialize(driver, node)
@driver = driver
@node = node
end

def text
raise "Not implemented"
end

def [](name)
raise "Not implemented"
end

def value
self[:value]
end

def set(value)
raise "Not implemented"
end

def select(option)
raise "Not implemented"
end

def click
raise "Not implemented"
end

def tag_name
raise "Not implemented"
end
end
4 changes: 2 additions & 2 deletions spec/drivers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
end

it "should allow assignment of field value" do
@driver.find('//input').first[:value].should == 'monkey'
@driver.find('//input').first.value.should == 'monkey'
@driver.find('//input').first.set('gorilla')
@driver.find('//input').first[:value].should == 'gorilla'
@driver.find('//input').first.value.should == 'gorilla'
end

it "should extract node tag name" do
Expand Down

0 comments on commit c389a0c

Please sign in to comment.