Skip to content

Instantly share code, notes, and snippets.

class HaveXpathContaining < Struct.new( :xpath, :contents )
def matches?( page )
if self.contents.blank?
page.has_xpath?( self.xpath + "[not( text() )]" ) || page.has_xpath?( self.xpath + "[string-length( normalize-space( . ) ) = 0]" )
else
page.has_xpath?( self.xpath + "[contains( ., '#{ self.contents }' )]" )
end
end
describe 'application flow'
before_each
$page = $( fixture( "jqt.html" ) )
end
it 'should start on the login page'
$page.find( "#login" ).should.have_class "current"
end
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Penny</title>
</head>
<body>
<form accept-charset='utf-8' action='/session.json' class='current' id='login' method='post'>
<div class='toolbar'>
<h1>Login</h1>
describe 'application flow'
before_each
$page = $( fixture( "jqt" ) )
end
it 'should start on the login page'
$page.find( "#login" ).should.have_class( "current" )
end
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8' />
<title>Penny</title>
</head>
<body id="body">
<form accept-charset='utf-8' action='/session.json' class='current' id='login' method='post'>
<div class='toolbar' id='login-c'>
When /^I enter "([^\"]*)"$/ do |value|
find( "//*[@class = 'current']//input" ).set( value )
find( "//*[@class = 'current']//input" ).value.should == value
end
# features/support/my_env.rb
def enter_text( input, text )
input.node.clear
input.node.send_keys( text.to_s )
end
# features/steps/feature_steps.rb
When /^I enter "([^\"]*)"$/ do |value|
enter_text( "//*[@class = 'current']//input", value )
# My page wrote alert p elements to the body (due to a hack to allow alerts in my app that wouldn't upset cuke flow),
# but iterating through them with all() didn't work, as when I called the text() method, it returned nil, even though
# there was text there -- so I used this instead.
#
# Get all alert p's off the page and dump their contents.
#
def _dump_alerts
doc = Nokogiri.HTML( page.body )
doc.xpath( "//p[@class = 'alert']" ).reverse.each do |p|
@biot023
biot023 / gist:294802
Created February 4, 2010 16:09
my_env.rb
# Again, the text() method wouldn't give me what I wanted, so I came up with this to get at it (and to remove any
# whitespace either side of it).
# Used like:
# find( "//a[@id = 'messages']" ).should contain_text( "Review Your Messages" )
class ContainText
def initialize( text )
@text = text
# Rob's fix to dump in my_env.rb
class Capybara::Driver::Selenium::Node
def set( value )
if tag_name == 'input' and type == 'radio'
node.select
elsif tag_name == 'input' and type == 'checkbox'
node.toggle
else
node.clear