Pavel Romanishkin
Pavel Romanishkin

Reputation: 25

With Capybara, how do I switch to the new window when i have already switched to the window?

I need to call another helper inside my helper (which one opens in new window) and open and switch to another window, but when i'm trying to do this - i'm receiving this error:

Capybara::ScopeError:
   `switch_to_window` is not supposed to be invoked from `within`'s, `within_frame`'s' or `within_window`'s' block.

Is there a way to open and switch to another window?

[EDIT1]

def create_ticket(kind, products = [])
  visit('inventory/parcels/')
  products.each do |product|
    if product[:serialized]
      serialized = find_device(product[:status], product[:id])

So here is the find_device helper:

def find_device(status_id, product_id)
  within_window open_new_window do
  visit('inventory/items?per=25)

Upvotes: 0

Views: 1040

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

There is no way (currently) to switch to another window from inside within_window (without dropping to driver specific methods). You could switch to just using switch_to_window everywhere rather than within_window but then you'd have to very carefully manually manage the scope stack, and switching back to original windows. A better approach would probably be to restructure your test so you don't need to leap between multiple windows simultaneously.

That being said, the restriction is actually unnecessary in Capybara nowadays and will probably be relaxed in the next release - https://github.com/teamcapybara/capybara/pull/1882

Upvotes: 1

Related Questions