Skip to content

Commit

Permalink
Add window support
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Sep 9, 2014
1 parent 5c5da86 commit 2d23867
Show file tree
Hide file tree
Showing 14 changed files with 609 additions and 520 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added ability to set zoom_factor (Dmytro Budnyk)
* Write JSON to the logger, rather than Ruby [Issue #430]
* Added ability to access all of a nodes attributes (Jon Rowe)
* Capybara 2.3 window support (Dmitry Vorotilin)

#### Bug fixes ####

Expand Down
32 changes: 1 addition & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ and the following optional features:

* `page.evaluate_script` and `page.execute_script`
* `page.within_frame`
* `page.within_window`
* `page.status_code`
* `page.response_headers`
* `page.save_screenshot`
* `page.driver.render_base64(format, options)`
* `page.driver.scroll_to(left, top)`
* `page.driver.basic_authorize(user, password)`
* `element.native.send_keys(*keys)`
* window API
* cookie handling
* drag-and-drop

Expand All @@ -131,11 +131,6 @@ If you need for some reasons base64 encoded screenshot you can simply call
same as for `save_screenshot` except the first argument which is format (:png by
default, acceptable :png, :gif, :jpeg).

### Resizing the window ###

Sometimes the window size is important to how things are rendered. Poltergeist sets the window
size to 1024x768 by default, but you can set this yourself with `page.driver.resize(width, height)`.

### Clicking precise coordinates ###

Sometimes its desirable to click a very specific area of the screen. You can accomplish this with
Expand Down Expand Up @@ -217,31 +212,6 @@ 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 => "[email protected]"
fill_in "pass", :with => "my_pass"
click_button "Log In"
end
```

### Sending keys ###

There's an ability to send arbitrary keys to the element:
Expand Down
35 changes: 28 additions & 7 deletions lib/capybara/poltergeist/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
module Capybara::Poltergeist
class Browser
ERROR_MAPPINGS = {
"Poltergeist.JavascriptError" => JavascriptError,
"Poltergeist.FrameNotFound" => FrameNotFound,
"Poltergeist.InvalidSelector" => InvalidSelector,
"Poltergeist.StatusFailError" => StatusFailError
'Poltergeist.JavascriptError' => JavascriptError,
'Poltergeist.FrameNotFound' => FrameNotFound,
'Poltergeist.InvalidSelector' => InvalidSelector,
'Poltergeist.StatusFailError' => StatusFailError,
'Poltergeist.NoSuchWindowError' => NoSuchWindowError
}

attr_reader :server, :client, :logger
Expand Down Expand Up @@ -131,15 +132,35 @@ def within_frame(handle, &block)
command 'pop_frame'
end

def window_handle
command 'window_handle'
end

def window_handles
command 'pages'
command 'window_handles'
end

def switch_to_window(handle)
command 'switch_to_window', handle
end

def open_new_window
command 'open_new_window'
end

def close_window(handle)
command 'close_window', handle
end

def within_window(name, &block)
command 'push_window', name
original = window_handle
handle = command 'window_handle', name
handle = name if handle.nil? && window_handles.include?(name)
raise NoSuchWindowError unless handle
switch_to_window(handle)
yield
ensure
command 'pop_window'
switch_to_window(original)
end

def click(page_id, id)
Expand Down
Loading

0 comments on commit 2d23867

Please sign in to comment.