1

I am testing links on a website using selenium and cucumber. Naturally these links would load in the current browser window. Though when I run my test they are opening in a new window each time! Even opening a webpage .get will open a brand new window. What am I missing here?

Also I am only doing testing in firefox so far

env.rb

require 'Rspec'

require 'selenium-webdriver'
include Rspec::Expectations

#=======================================================================================
  Before do
    @driver = Selenium::WebDriver.for :firefox
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end

  After do
    #@driver.quit
    @verification_errors.should == []
  end
#=======================================================================================

wait = Selenium::WebDriver::Wait.new(:timeout => 25)

def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end

  def alert_present?()
    @driver.switch_to.alert
    true
  rescue Selenium::WebDriver::Error::NoAlertPresentError
    false
  end

  def verify(&blk)
    yield
  rescue ExpectationNotMetError => ex
    @verification_errors << ex
  end

  def close_alert_and_get_its_text(how, what)
    alert = @driver.switch_to().alert()
    alert_text = alert.text
    if (@accept_next_alert) then
      alert.accept()
    else
      alert.dismiss()
    end
    alert_text
  ensure
    @accept_next_alert = true
  end

I realized that it is opening an odd firefox browser. I have changed the settings in firefox to open windows in new tabs (based on another question) and I realized selenium is opening an instance of firefox that has all old settings. I don't have multiple versions of firefox installed I don't understand where this could be coming from either.

step def:

When /^I am viewing Google$/ do
if @driver.current_url != "www.google.com"
 @driver.get "www.google.com"
 end
end

This code repeated will load many windows. Also any .click interactions with the website

I have recently asked a question that is similar to this one (didn't realize I was somewhat repeating myself) here is the link to it for anyone interested:

How to use same browser window for automated test using selenium-webdriver (ruby)?

Community
  • 1
  • 1
megan
  • 305
  • 1
  • 6
  • 18

2 Answers2

1

You can also just add this to your env.rb file:

at_exit do
  if ENV['KEEP_OPEN'] != 'false' || ENV['KEEP_OPEN'] != 'no'
    browser.close
  end
end

Really, all you need out of that is browser.close, the rest just sets the environment so that in the command line, if you want to keep it open, you can just say so.

Also, you might be opening/initializing too many browsers somewhere lost in your code. I'd have to see all of it to be able to debug...but, just thought i'd let you know that it might not be a problem with your env.rb file.

Whitney Imura
  • 810
  • 1
  • 6
  • 15
0

In order to avoid multiple window, you should edit the prefs.json of yout firefox webdriver

You should find the file here : /usr/lib64/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/extension/prefs.json

You need to edit the line with:

"browser.link.open_newwindow": 2 

replace by the value 1,2, or 3, depending on what you want : http://kb.mozillazine.org/Browser.link.open_newwindow

All profile options defined in this file cannot be overwritten, you must edit this file.

Fabrice31
  • 770
  • 11
  • 25