From 4e2b6a08279117a5b8800f78e0390ebb61f9e881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Germ=C3=A1n=20Casta=C3=B1eda=20Echevarr=C3=ADa?= Date: Tue, 25 Jun 2013 14:21:52 -0700 Subject: [PATCH] New feature. window_handles returns the list of open windows names This helps exposing window names for #178 --- README.md | 27 +++++++++++++++++++ lib/capybara/poltergeist/browser.rb | 4 +++ .../poltergeist/client/browser.coffee | 3 +++ .../poltergeist/client/compiled/browser.js | 4 +++ .../poltergeist/client/compiled/web_page.js | 4 +++ .../poltergeist/client/web_page.coffee | 3 +++ lib/capybara/poltergeist/driver.rb | 4 +++ spec/integration/driver_spec.rb | 23 ++++++++++++++++ 8 files changed, 72 insertions(+) diff --git a/README.md b/README.md index 6010ba1..7bfb7f5 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,32 @@ The following methods are used to inspect and manipulate cookies: object. * `page.driver.remove_cookie(name)` - remove a cookie +### Window switching ### + +The following methods can be used to execute commands inside different windows: + +* `page.driver.window_handles` - an array containing the names of all + the open windows. + +* `page.within_window(name) { # actions }` - executes + the passed block in the context of the named window. + +Example: + +``` ruby +find_link("Login with Facebook").trigger("click") + +sleep(0.1) + +fb_popup = page.driver.window_handles.last +page.within_window fb_popup do + fill_in "email", :with => "facebook_email@email.tst" + fill_in "pass", :with => "my_pass" + click_button "Log In" +end +``` + + ## Customization ## You can customize the way that Capybara sets up Poltegeist via the following code in your @@ -329,6 +355,7 @@ Include as much information as possible. For example: #### Features #### * Can set cookies for given domain +* Can get open window names with window_handles [Issue #178] #### Bug fixes #### diff --git a/lib/capybara/poltergeist/browser.rb b/lib/capybara/poltergeist/browser.rb index c97cddf..1fa833c 100644 --- a/lib/capybara/poltergeist/browser.rb +++ b/lib/capybara/poltergeist/browser.rb @@ -118,6 +118,10 @@ module Capybara::Poltergeist command 'pop_frame' end + def window_handles + command 'pages' + end + def within_window(name, &block) command 'push_window', name yield diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index 86307bb..9f9291d 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -171,6 +171,9 @@ class Poltergeist.Browser else @owner.sendError(new Poltergeist.FrameNotFound(name)) + pages: -> + this.sendResponse(@page.pages()) + pop_frame: -> this.sendResponse(@page.popFrame()) diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index 4e1916c..ae75a04 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -228,6 +228,10 @@ Poltergeist.Browser = (function() { } }; + Browser.prototype.pages = function() { + return this.sendResponse(this.page.pages()); + }; + Browser.prototype.pop_frame = function() { return this.sendResponse(this.page.popFrame()); }; diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index 5387453..f8bafa3 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -235,6 +235,10 @@ Poltergeist.WebPage = (function() { } }; + WebPage.prototype.pages = function() { + return this["native"].pagesWindowName; + }; + WebPage.prototype.popFrame = function() { this.frames.pop(); return this["native"].switchToParentFrame(); diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index af36fe5..5bf44bc 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -155,6 +155,9 @@ class Poltergeist.WebPage else false + pages: -> + @native.pagesWindowName + popFrame: -> @frames.pop() @native.switchToParentFrame() diff --git a/lib/capybara/poltergeist/driver.rb b/lib/capybara/poltergeist/driver.rb index 35c38bc..24c23f5 100644 --- a/lib/capybara/poltergeist/driver.rb +++ b/lib/capybara/poltergeist/driver.rb @@ -143,6 +143,10 @@ module Capybara::Poltergeist browser.within_window(name, &block) end + def window_handles + browser.window_handles + end + def reset! browser.reset @started = false diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index dea31e6..76350cd 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -439,5 +439,28 @@ module Capybara::Poltergeist driver.quit end end + + it 'lists the open windows' do + @session.visit '/' + + @session.evaluate_script <<-CODE + window.open('/poltergeist/simple', 'popup') + CODE + + @session.evaluate_script <<-CODE + window.open('/poltergeist/simple', 'popup2') + CODE + + @session.within_window 'popup2' do + @session.html.should include('Test') + @session.evaluate_script('window.close()') + end + + sleep 0.1; + + @driver.browser.window_handles.should == ["popup"] + @driver.window_handles.should == ["popup"] + end + end end