Berg Dodson
Berg Dodson

Reputation: 63

How do I open tabs using a Watir in ruby

I'm trying to create additional tabs in Firefox using Watir webdriver. However, I'm not having much luck

After opening the browser:

require 'watir-webdriver' b =Watir::Browser.new

I want to open up tabs but have been unable to figure out how to access the Open a New Tab button on the browser. How could I create a new tab then?

Upvotes: 6

Views: 6364

Answers (3)

Astrit Shuli
Astrit Shuli

Reputation: 657

You can use javascript :

require 'watir' # Crawler
browser = Watir::Browser.new :chrome   #or firefox in your case
browser.goto 'http://example.com'

browser.execute_script('window.open("http://example1.com")')

Upvotes: 4

XYZ
XYZ

Reputation: 27397

Watir does not care if a new page opens in a new window or in a new tab, so use window switching API to switch to the new tab after open it.

http://watir.com/guides/windows/

Upvotes: 0

Carldmitch
Carldmitch

Reputation: 409

As already mentioned Selenium doesn't explicitly support opening of tabs, however there are 'workarounds' such as...

require 'watir-webdriver'
browser = Watir::Browser.new :ff
browser.goto 'http://the-internet.herokuapp.com'
browser.link(:text, 'A/B Testing').click(:command, :shift)
browser.windows.last.use

This will open the link in a new tab (on a mac) and focus it

Upvotes: 6

Related Questions