Very early basics for form submission

This commit is contained in:
Jonas Nicklas 2009-11-08 01:13:16 +01:00
parent df870eab2a
commit 11973d2f13
5 changed files with 66 additions and 4 deletions

View File

@ -21,7 +21,16 @@ class Webcat::Driver::RackTest
end
def click
session.visit(self[:href])
if tag_name == 'a'
session.visit(self[:href])
elsif tag_name == 'input' and self[:type] == 'submit'
form = node.ancestors('form').first
attributes = form.xpath('//input').inject({}) do |agg, node|
agg[node['name'].to_s] = node['value'].to_s
agg
end
session.submit(form['action'].to_s, attributes)
end
end
def tag_name
@ -41,12 +50,23 @@ class Webcat::Driver::RackTest
def visit(path)
get(path)
@body = response.body
@html = Nokogiri::HTML(body)
cache_body
end
def submit(path, attributes)
post(path, attributes)
cache_body
end
def find(selector)
html.xpath(selector).map { |node| Node.new(self, node) }
end
private
def cache_body
@body = response.body
@html = Nokogiri::HTML(body)
end
end

View File

@ -25,6 +25,10 @@ class Webcat::Session
find_element("//a[@id='#{locator}']", %{//a[text()="#{locator}"]}, %{//a[@title="#{locator}"]}).click
end
def click_button(locator)
find_element("//input[@type='submit'][@id='#{locator}']", "//input[@type='submit'][@value='#{locator}']").click
end
def body
driver.body
end

View File

@ -50,6 +50,20 @@ shared_examples_for "session" do
end
end
end
describe '#click_button' do
before do
@session.visit('/form')
end
context "with value given" do
it "should submit the associated form" do
@session.click_button('awesome')
results = YAML.load(@session.body)
results['foo'].should == 'blah'
end
end
end
end

View File

@ -21,4 +21,12 @@ class TestApp < Sinatra::Base
get '/with_simple_html' do
erb :with_simple_html
end
end
get '/form' do
erb :form
end
post '/form' do
params[:form].to_yaml
end
end

16
spec/views/form.erb Normal file
View File

@ -0,0 +1,16 @@
<h1>Form</h1>
<form action="/form" method="post">
<p>
<input type="text" name="form[foo]" value="blah" id="foo"/>
</p>
<p>
<input type="text" name="form[bar]" value="fish" id="bar"/>
</p>
<p>
<input type="submit" name="form[awesome]" value="awesome"/>
<input type="submit" name="form[crappy]" value="crappy"/>
</p>
</form>