The Issue

While running through a browser test, we may need to open a link or image in a separate tab or window. If manually testing, you may choose to right click, and then select the option under the right click menu. However, a much simpler (and faster way) is to send a specific keystroke through Watir-Webdriver to directly open the browser window/tab.

The Answer

We will be utilizing a click command which will allow us to send both a ctrl-click and a shift-click to our desired element. A ctrl-click will open what we click in a new tab, and a shift-click will open the link in a new window.

Lets utilize this command in a script.

The Code

We will navigate to the Cucumber homepage, and open a link in a new window with our shift-click command.

The Result

We will be validating our text on the Cucumber training page.

We will be validating our text on the Cucumber training page.

We can save and run our test (shiftclicktest.rb)

  • Browser opens
  • Browser navigates to Cucumber homepage
  • Browser opens new window
  • Validates text in new window

The Takeaway

Much like our send_keys command, using the control and shift click Watir-Webdriver commands will add versatility to your testing arsenal. Implement them into your automated tests and see how they can help optimize your execution.