From 8ad161b0413ffe905b0c1cb99ed6d18d390f09b3 Mon Sep 17 00:00:00 2001 From: jgagner Date: Tue, 12 Jan 2010 11:40:10 -0800 Subject: [PATCH 1/8] #within_frame method --- lib/capybara/driver/base.rb | 4 ++++ lib/capybara/driver/selenium_driver.rb | 7 ++++++- spec/driver/selenium_driver_spec.rb | 2 +- spec/drivers_spec.rb | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/capybara/driver/base.rb b/lib/capybara/driver/base.rb index 2f94d106..dbdc064e 100644 --- a/lib/capybara/driver/base.rb +++ b/lib/capybara/driver/base.rb @@ -27,6 +27,10 @@ class Capybara::Driver::Base raise NotImplementedError end + def within_frame frame_id + raise Capybara::NotSupportedByDriverError + end + def source raise NotImplementedError end diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index 2f1deabb..076fcc15 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -103,7 +103,12 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base def browser self.class.driver end - + def with_frame(frame_id) + old_window = browser.window_handle + browser.switch_to.frame(frame_id) + yield + browser.switch_to.window old_window + end private def url(path) diff --git a/spec/driver/selenium_driver_spec.rb b/spec/driver/selenium_driver_spec.rb index a71047e0..0fdfa652 100644 --- a/spec/driver/selenium_driver_spec.rb +++ b/spec/driver/selenium_driver_spec.rb @@ -8,5 +8,5 @@ describe Capybara::Driver::Selenium do it_should_behave_like "driver" it_should_behave_like "driver with javascript support" it_should_behave_like "driver without node path support" - + it_should_behave_like "driver with frame support" end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 8643b4e6..523be045 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -1,5 +1,8 @@ require File.expand_path('spec_helper', File.dirname(__FILE__)) +shared_examples_for 'driver with frame support' do + it_should_behave_like 'within_frame' +end shared_examples_for 'driver' do describe '#visit' do From 28605a5d9c38481678a8bd8a41d7f9174026dfc9 Mon Sep 17 00:00:00 2001 From: jgagner Date: Tue, 12 Jan 2010 11:56:17 -0800 Subject: [PATCH 2/8] added specs for with_frame --- spec/drivers_spec.rb | 2 +- spec/dsl/with_frame_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/views/frame_one.erb | 8 ++++++++ spec/views/frame_two.erb | 8 ++++++++ spec/views/with_frames.erb | 10 ++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 spec/dsl/with_frame_spec.rb create mode 100644 spec/views/frame_one.erb create mode 100644 spec/views/frame_two.erb create mode 100644 spec/views/with_frames.erb diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 523be045..8d833609 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -1,7 +1,7 @@ require File.expand_path('spec_helper', File.dirname(__FILE__)) shared_examples_for 'driver with frame support' do - it_should_behave_like 'within_frame' + it_should_behave_like 'with_frame' end shared_examples_for 'driver' do diff --git a/spec/dsl/with_frame_spec.rb b/spec/dsl/with_frame_spec.rb new file mode 100644 index 00000000..6bf6ae53 --- /dev/null +++ b/spec/dsl/with_frame_spec.rb @@ -0,0 +1,32 @@ +module WithFrameSpec + shared_examples_for "with_frame" do + describe '#with_frame' do + before(:each) do + @driver.visit('/with_frames') + end + + it "should find the div in frameOne" do + @driver.with_frame("frameOne") do + @driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne' + end + end + it "should find the div in FrameTwo" do + @driver.with_frame("frameTwo") do + @driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo' + end + end + it "should find the text div in the main window after finding text in frameOne" do + @driver.with_frame("frameOne") do + @driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne' + end + @driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow' + end + it "should find the text div in the main window after finding text in frameTwo" do + @driver.with_frame("frameTwo") do + @driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo' + end + @driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow' + end + end + end +end \ No newline at end of file diff --git a/spec/views/frame_one.erb b/spec/views/frame_one.erb new file mode 100644 index 00000000..6b8f8a7b --- /dev/null +++ b/spec/views/frame_one.erb @@ -0,0 +1,8 @@ + + + This is the title of frame one + + +
This is the text of divInFrameOne
+ + \ No newline at end of file diff --git a/spec/views/frame_two.erb b/spec/views/frame_two.erb new file mode 100644 index 00000000..b344964d --- /dev/null +++ b/spec/views/frame_two.erb @@ -0,0 +1,8 @@ + + + This is the title of frame two + + +
This is the text of divInFrameTwo
+ + \ No newline at end of file diff --git a/spec/views/with_frames.erb b/spec/views/with_frames.erb new file mode 100644 index 00000000..871e5dc5 --- /dev/null +++ b/spec/views/with_frames.erb @@ -0,0 +1,10 @@ + + + With Frames + + +
This is the text for divInMainWindow
+ + + + \ No newline at end of file From 1e096ddcc02d09351ee61475e40ed6e8f5e4bca5 Mon Sep 17 00:00:00 2001 From: jgagner Date: Tue, 12 Jan 2010 12:27:39 -0800 Subject: [PATCH 3/8] Fixed naming --- lib/capybara/driver/selenium_driver.rb | 2 +- lib/capybara/session.rb | 6 +++++- spec/drivers_spec.rb | 2 +- spec/dsl/{with_frame_spec.rb => within_frame_spec.rb} | 0 spec/views/{with_frames.erb => within_frames.erb} | 0 5 files changed, 7 insertions(+), 3 deletions(-) rename spec/dsl/{with_frame_spec.rb => within_frame_spec.rb} (100%) rename spec/views/{with_frames.erb => within_frames.erb} (100%) diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index 076fcc15..d450c629 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -103,7 +103,7 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base def browser self.class.driver end - def with_frame(frame_id) + def within_frame(frame_id) old_window = browser.window_handle browser.switch_to.frame(frame_id) yield diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index a82d4347..ba0e6a67 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -8,7 +8,7 @@ module Capybara :all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script, :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?, :has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck, - :visit, :wait_until, :within, :within_fieldset, :within_table + :visit, :wait_until, :within, :within_fieldset, :within_table,:within_frame ] attr_reader :mode, :app @@ -33,6 +33,10 @@ module Capybara end end + def within_frame frame_id + driver.within_frame frame_id + end + def current_url driver.current_url end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 8d833609..523be045 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -1,7 +1,7 @@ require File.expand_path('spec_helper', File.dirname(__FILE__)) shared_examples_for 'driver with frame support' do - it_should_behave_like 'with_frame' + it_should_behave_like 'within_frame' end shared_examples_for 'driver' do diff --git a/spec/dsl/with_frame_spec.rb b/spec/dsl/within_frame_spec.rb similarity index 100% rename from spec/dsl/with_frame_spec.rb rename to spec/dsl/within_frame_spec.rb diff --git a/spec/views/with_frames.erb b/spec/views/within_frames.erb similarity index 100% rename from spec/views/with_frames.erb rename to spec/views/within_frames.erb From 35c70890376e8d8bbb76785c5f7fdc3394ad6e28 Mon Sep 17 00:00:00 2001 From: jgagner Date: Tue, 12 Jan 2010 12:33:06 -0800 Subject: [PATCH 4/8] Fixed bug with within_frame session method and changed naming in spec --- lib/capybara/session.rb | 4 +++- spec/dsl/within_frame_spec.rb | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index ba0e6a67..a66b86f8 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -34,7 +34,9 @@ module Capybara end def within_frame frame_id - driver.within_frame frame_id + driver.within_frame(frame_id) do + yield + end end def current_url diff --git a/spec/dsl/within_frame_spec.rb b/spec/dsl/within_frame_spec.rb index 6bf6ae53..be46f7d5 100644 --- a/spec/dsl/within_frame_spec.rb +++ b/spec/dsl/within_frame_spec.rb @@ -1,28 +1,28 @@ -module WithFrameSpec - shared_examples_for "with_frame" do - describe '#with_frame' do +module WithinFrameSpec + shared_examples_for "within_frame" do + describe '#within_frame' do before(:each) do - @driver.visit('/with_frames') + @driver.visit('/within_frames') end it "should find the div in frameOne" do - @driver.with_frame("frameOne") do + @driver.within_frame("frameOne") do @driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne' end end it "should find the div in FrameTwo" do - @driver.with_frame("frameTwo") do + @driver.within_frame("frameTwo") do @driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo' end end it "should find the text div in the main window after finding text in frameOne" do - @driver.with_frame("frameOne") do + @driver.within_frame("frameOne") do @driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne' end @driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow' end it "should find the text div in the main window after finding text in frameTwo" do - @driver.with_frame("frameTwo") do + @driver.within_frame("frameTwo") do @driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo' end @driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow' From 2b813f09b921e4ec2ec06c9d602f3eb972650361 Mon Sep 17 00:00:00 2001 From: jgagner Date: Thu, 14 Jan 2010 17:37:19 -0800 Subject: [PATCH 5/8] made it so has_xpath?/has_no_xpath? checks to make sure the element is visible by default. Can be overriden with options. --- lib/capybara/session.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index a66b86f8..2a6e5d17 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -134,7 +134,7 @@ module Capybara def has_xpath?(path, options={}) wait_conditionally_until do results = all(path, options) - + options = {:visible => true}.merge options if options[:count] results.size == options[:count] else @@ -147,6 +147,7 @@ module Capybara def has_no_xpath?(path, options={}) wait_conditionally_until do + options = {:visible => true}.merge options results = all(path, options) if options[:count] From b207370441144e76c587517a5f7138e59026f710 Mon Sep 17 00:00:00 2001 From: jgagner Date: Thu, 14 Jan 2010 17:37:48 -0800 Subject: [PATCH 6/8] rescues the inevitable "Selenium::WebDriver::Error::WebDriverError: element is obsolete" if you check to see if an element that has been removed from the DOM is visible --- lib/capybara/driver/selenium_driver.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index d450c629..5e0226ed 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -44,7 +44,12 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base end def visible? - node.displayed? and node.displayed? != "false" + begin + node.displayed? and node.displayed? != "false" + rescue Selenium::WebDriver::Error::WebDriverError + # rescues the inevitable "Selenium::WebDriver::Error::WebDriverError: element is obsolete" if you check to see if an element that has been removed from the DOM is visible + return false + end end private From a19c902a65c1389922e73afc3453d1ca4d7c0b6d Mon Sep 17 00:00:00 2001 From: jgagner Date: Thu, 14 Jan 2010 17:37:19 -0800 Subject: [PATCH 7/8] made it so has_xpath?/has_no_xpath? checks to make sure the element is visible by default. Can be overriden with options. rescues the inevitable "Selenium::WebDriver::Error::WebDriverError: element is obsolete" if you check to see if an element that has been removed from the DOM is visible --- lib/capybara/driver/selenium_driver.rb | 7 ++++++- lib/capybara/session.rb | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index d450c629..5e0226ed 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -44,7 +44,12 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base end def visible? - node.displayed? and node.displayed? != "false" + begin + node.displayed? and node.displayed? != "false" + rescue Selenium::WebDriver::Error::WebDriverError + # rescues the inevitable "Selenium::WebDriver::Error::WebDriverError: element is obsolete" if you check to see if an element that has been removed from the DOM is visible + return false + end end private diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index a66b86f8..90d11b6e 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -133,8 +133,8 @@ module Capybara def has_xpath?(path, options={}) wait_conditionally_until do + options = {:visible => true}.merge options results = all(path, options) - if options[:count] results.size == options[:count] else @@ -147,6 +147,7 @@ module Capybara def has_no_xpath?(path, options={}) wait_conditionally_until do + options = {:visible => true}.merge options results = all(path, options) if options[:count] From 4b7609ac1522c8e7c1e9462e74fd53f2bcc7460b Mon Sep 17 00:00:00 2001 From: jgagner Date: Fri, 15 Jan 2010 08:15:38 -0800 Subject: [PATCH 8/8] Ignoring .idea directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7f61f0d3..92836684 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ .DS_Store pkg *~