Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
New feature. window_handles returns the list of open windows names
Browse files Browse the repository at this point in the history
This helps exposing window names for #178
  • Loading branch information
juanger committed Jul 4, 2013
1 parent 037897f commit 4e2b6a0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ####

Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def within_frame(handle, &block)
command 'pop_frame'
end

def window_handles
command 'pages'
end

def within_window(name, &block)
command 'push_window', name
yield
Expand Down
3 changes: 3 additions & 0 deletions lib/capybara/poltergeist/client/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/client/compiled/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/client/compiled/web_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions lib/capybara/poltergeist/client/web_page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class Poltergeist.WebPage
else
false

pages: ->
@native.pagesWindowName

popFrame: ->
@frames.pop()
@native.switchToParentFrame()
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/poltergeist/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def within_window(name, &block)
browser.within_window(name, &block)
end

def window_handles
browser.window_handles
end

def reset!
browser.reset
@started = false
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,5 +439,28 @@ def session_url(path)
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

0 comments on commit 4e2b6a0

Please sign in to comment.