Added 'within' with nested scopes.
This commit is contained in:
parent
2c86723ea2
commit
4885af6393
|
@ -36,7 +36,7 @@ module Webcat
|
||||||
|
|
||||||
SESSION_METHODS = [
|
SESSION_METHODS = [
|
||||||
:visit, :body, :click_link, :click_button, :fill_in, :choose,
|
:visit, :body, :click_link, :click_button, :fill_in, :choose,
|
||||||
:check, :uncheck, :attach_file, :select, :has_content?
|
:check, :uncheck, :attach_file, :select, :has_content?, :within
|
||||||
]
|
]
|
||||||
SESSION_METHODS.each do |method|
|
SESSION_METHODS.each do |method|
|
||||||
class_eval <<-RUBY, __FILE__, __LINE__+1
|
class_eval <<-RUBY, __FILE__, __LINE__+1
|
||||||
|
|
|
@ -60,11 +60,25 @@ class Webcat::Session
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_content?(content)
|
def has_content?(content)
|
||||||
driver.find("//*[contains(child::text(),'#{content}')]").size > 0
|
find("//*[contains(child::text(),'#{content}')]").size > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def within(scope)
|
||||||
|
scopes.push(scope)
|
||||||
|
yield
|
||||||
|
scopes.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def current_scope
|
||||||
|
scopes.join('')
|
||||||
|
end
|
||||||
|
|
||||||
|
def scopes
|
||||||
|
@scopes ||= []
|
||||||
|
end
|
||||||
|
|
||||||
def find_link(locator)
|
def find_link(locator)
|
||||||
find_element("//a[@id='#{locator}']", %{//a[text()="#{locator}"]}, %{//a[@title="#{locator}"]})
|
find_element("//a[@id='#{locator}']", %{//a[text()="#{locator}"]}, %{//a[@title="#{locator}"]})
|
||||||
end
|
end
|
||||||
|
@ -96,7 +110,7 @@ private
|
||||||
def find_field_by_id(locator, *kinds)
|
def find_field_by_id(locator, *kinds)
|
||||||
kinds.each do |kind|
|
kinds.each do |kind|
|
||||||
path = FIELDS_PATHS[kind]
|
path = FIELDS_PATHS[kind]
|
||||||
element = driver.find(path.call(locator)).first
|
element = find(path.call(locator)).first
|
||||||
return element if element
|
return element if element
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
|
@ -104,7 +118,7 @@ private
|
||||||
|
|
||||||
def find_field_by_label(locator, *kinds)
|
def find_field_by_label(locator, *kinds)
|
||||||
kinds.each do |kind|
|
kinds.each do |kind|
|
||||||
label = driver.find("//label[text()='#{locator}']").first
|
label = find("//label[text()='#{locator}']").first
|
||||||
if label
|
if label
|
||||||
element = find_field_by_id(label[:for], kind)
|
element = find_field_by_id(label[:for], kind)
|
||||||
return element if element
|
return element if element
|
||||||
|
@ -115,10 +129,15 @@ private
|
||||||
|
|
||||||
def find_element(*locators)
|
def find_element(*locators)
|
||||||
locators.each do |locator|
|
locators.each do |locator|
|
||||||
element = driver.find(locator).first
|
element = find(locator).first
|
||||||
return element if element
|
return element if element
|
||||||
end
|
end
|
||||||
raise Webcat::ElementNotFound, "element not found"
|
raise Webcat::ElementNotFound, "element not found"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find(locator)
|
||||||
|
locator = current_scope.to_s + locator
|
||||||
|
driver.find(locator)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -324,6 +324,56 @@ shared_examples_for "session" do
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#within' do
|
||||||
|
before do
|
||||||
|
@session.visit('/with_scope')
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with click_link" do
|
||||||
|
it "should click links in the given scope" do
|
||||||
|
@session.within("//li[contains(.,'With Simple HTML')]") do
|
||||||
|
@session.click_link('Go')
|
||||||
|
end
|
||||||
|
@session.body.should include('<h1>Bar</h1>')
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with nested scopes" do
|
||||||
|
it "should respect the inner scope" do
|
||||||
|
@session.within("//div[@id='for_bar']") do
|
||||||
|
@session.within("//li[contains(.,'Bar')]") do
|
||||||
|
@session.click_link('Go')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@session.body.should include('Another World')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should respect the outer scope" do
|
||||||
|
@session.within("//div[@id='another_foo']") do
|
||||||
|
@session.within("//li[contains(.,'With Simple HTML')]") do
|
||||||
|
@session.click_link('Go')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@session.body.should include('Hello world')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with forms" do
|
||||||
|
it "should fill in a field and click a button" do
|
||||||
|
@session.within("//li[contains(.,'Bar')]") do
|
||||||
|
@session.click_button('Go')
|
||||||
|
end
|
||||||
|
extract_results(@session)['first_name'].should == 'Peter'
|
||||||
|
@session.visit('/with_scope')
|
||||||
|
@session.within("//li[contains(.,'Bar')]") do
|
||||||
|
@session.fill_in('First Name', :with => 'Dagobert')
|
||||||
|
@session.click_button('Go')
|
||||||
|
end
|
||||||
|
extract_results(@session)['first_name'].should == 'Dagobert'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Webcat::Session do
|
describe Webcat::Session do
|
||||||
|
|
|
@ -25,6 +25,10 @@ class TestApp < Sinatra::Base
|
||||||
erb :with_simple_html
|
erb :with_simple_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/with_scope' do
|
||||||
|
erb :with_scope
|
||||||
|
end
|
||||||
|
|
||||||
get '/form' do
|
get '/form' do
|
||||||
erb :form
|
erb :form
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
<h1>This page is used for testing various scopes</h1>
|
||||||
|
|
||||||
|
<p id="for_foo">
|
||||||
|
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
|
||||||
|
incididunt ut labore et dolore magna aliqua. Ut enim ad minim venia.
|
||||||
|
<a href="/redirect">Go</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div id="for_bar">
|
||||||
|
<ul>
|
||||||
|
<li>With Simple HTML: <a href="/with_simple_html">Go</a>
|
||||||
|
<form action="/redirect" method="post" accept-charset="utf-8">
|
||||||
|
<p>
|
||||||
|
<label for="simple_first_name">First Name</label>
|
||||||
|
<input type="text" name="first_name" value="John" id="simple_first_name"/>
|
||||||
|
</p>
|
||||||
|
<p><input type="submit" value="Go"/></p>
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
<li>Bar: <a href="/foo">Go</a>
|
||||||
|
<form action="/form" method="post" accept-charset="utf-8">
|
||||||
|
<p>
|
||||||
|
<label for="bar_first_name">First Name</label>
|
||||||
|
<input type="text" name="form[first_name]" value="Peter" id="bar_first_name"/>
|
||||||
|
</p>
|
||||||
|
<p><input type="submit" value="Go"/></p>
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="another_foo">
|
||||||
|
<ul>
|
||||||
|
<li>With Simple HTML: <a href="/">Go</a>
|
||||||
|
</ul>
|
||||||
|
</div>
|
Loading…
Reference in New Issue