Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string argument to #within_window is deprecated

Tags:

capybara

I am trying to update my code because I am currently getting the following deprecation warning when running it:

"DEPRECATION WARNING: Passing string argument to #within_window is deprecated. Pass window object or lambda."

Here is the code:

new_window=page.driver.browser.window_handles.last 
    page.within_window new_window do
        expect(current_url).to eq("url")
    end
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)

How should I edit the above so I no longer get the deprecation warning? Thanks!

like image 269
Chris Vannithone Avatar asked Aug 21 '14 22:08

Chris Vannithone


People also ask

How do you pass string as an argument?

To pass a one dimensional string to a function as an argument we just write the name of the string array variable. In the following example we have a string array variable message and it is passed to the displayString function.

How do you pass a string as a reference?

First, we have the function definition “DisplayString,” where a constant string reference is passed. The constant strings are defined and initialized in the main function as “str1” and “str2”. After that, pass these constant strings to the function “InputString”.

Can we pass string in function?

Passing Strings to Function As strings are character arrays, so we can pass strings to function in the same way we pass an array to a function. Below is a sample program to do this: C.


1 Answers

The within_window method was changed to expect a Capybara::Window or a proc/lamda. Locating the window by a string, which is what window_handles.last returns, is what was deprecated.

To get the last Capybara::Window, use the windows method. It works similar to what was done with window_handles:

new_window = windows.last
page.within_window new_window do
    expect(current_url).to eq("url")
end

Note that the documentation states that "The order of windows in returned array is not defined. The driver may sort windows by their creation time but it's not required.". I think the same was true when using window_handles so it is probably safe to assume that the last window is the new window.

However, where possible, it would probably be better to locate the window by something specific such as the title:

within_window(->{ page.title == 'New window title' }) do
  expect(current_url).to eq("url")
end
like image 89
Justin Ko Avatar answered Oct 11 '22 17:10

Justin Ko