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!
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.
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”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With