mribeiro
mribeiro

Reputation: 307

Test Automation - New guide

I'm starting to study about automation, so I started over an application that I work on today. But I had a problem that I'm not sure how to solve and I'd like to know if anyone can help me.

On the system, when I click on a displayed option it opens a new tab in the browser, redirecting to the page of the clicked option. However, automation continues to run on the previous tab and does not focus on the new tab.

Exemplifying better: I enter the site X, there are 4 different login options (Y, P, T, O) I click on the Y option, it opens a new tab that is directed to the login screen of option Y But the automation keeps running on the X tab, it does not focus on the new tab so I can login to the Y screen

If I click on the other options, it also opens a new login guide for them.

From the way I wrote the test, it makes the full flow ... From accessing the site X, select the Y and then log in, but he is not performing for not focusing on this new guide.

Is it possible to do that? If so, would anyone have a hint of how to do it?

The automation I'm doing is with ruby, capybara and cucumber

Upvotes: 0

Views: 93

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49910

If you want to swap to a new tab/window you have to tell it to do that. The two methods that allow you to do that in Capybara are within_window - https://www.rubydoc.info/gems/capybara/Capybara/Session#within_window-instance_method - and switch_to_window - https://www.rubydoc.info/gems/capybara/Capybara/Session#switch_to_window-instance_method . Generally within_window would be preferred since normal flow is to move over to the new window, do a couple of things, and then return to the original window. However, when using Cucumber it may not be possible to set up your steps for nicely nesting a block, so you might need to use switch_to_window and handle switching back to the original window yourself

Whichever method you use first starts with getting the new window using window_opened_by - https://www.rubydoc.info/gems/capybara/Capybara/Session#window_opened_by-instance_method

new_window = page.window_opened_by do
  # perform whatever actions in the original window lead to creating a new tab/window
  page.click_on 'whatever'
end

Then, if using within_window it would be something like

page.within_window(new_window) do
  # perform actions in the new tab/window
  page.click_on 'something'
end
new_window.close # when done with the new tab/window

and if using switch_to_window it would be something like

orig_window = page.current_window # get the current window object
page.switch_to_window(new_window)
# perform actions in the new tab/window
page.click_on 'something'
# return to the original window
page.switch_to_window(orig_window)
new_window.close

Upvotes: 1

Related Questions