Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between different tabs using capybara or selenium

I wish to switch between browser tabs for a feature I am testing. However I have not been able to do this. Have tried using the following:

page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)

The idea been that when I am on the second tab the above code should bring it back to the first tab. However this is not working.

I also just tried to close the second tab using this:

page.execute_script "window.close();"

but this does do anything, the tab is not closed nor is the overall browser window so appears that it is not doing anything.

Had anybody else had problems like this and how did you figure out a solution? I am using FireFox.

like image 488
user1523236 Avatar asked Jul 14 '14 20:07

user1523236


2 Answers

Here's my method for closing new tabs.

def close_new_tabs
  window = page.driver.browser.window_handles
    
  if window.size > 1 
    page.driver.browser.switch_to.window(window.last)
    page.driver.browser.close
    page.driver.browser.switch_to.window(window.first)
  end
end

I call this method anytime a new tab may need to be closed.

like image 70
John W Avatar answered Jan 03 '23 21:01

John W


I was able to switch between tabs using the following

browser.switch_to.window browser.window_handles.last
like image 28
Kapidis Avatar answered Jan 03 '23 21:01

Kapidis