2

I am automating test cases for a website using selenium-webdriver and cucumber in ruby. I need each feature to run in a particular order and using the same browser window. Atm each feature creates a new window to run test in. Though in some test cases this behavior is desired- in many cases it is not. From my research so far it seems there are mixed answers about whether or not it is possible to drive the same browser window with selenium throughout test cases. Most answers I have run into were for other languages and were work arounds specific to a browser (I am developing my test while testing IE but will be expected to run these test in other browsers). I am working in Ruby and from what I have read it seems as though I'd have to make a class for the page? I'm confused as to why I would have to do this or how that helps.

my env.rb file:

require 'selenium-webdriver'
require 'rubygems'
require 'nokogiri'
require 'rspec/expectations'

Before do

    @driver ||= Selenium::WebDriver.for :ie
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @driver.manage.timeouts.script_timeout = 30
    @verification_errors = []
  end

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

Some information I've gathered so far of people with similar problems: https://code.google.com/p/selenium/issues/detail?id=18 Is there any way to attach an already running browser to selenium webdriver in java?

Please ask me questions if anything about my question is not clear. I have many more test to create but I do not want to move on creating test if my foundation is sloppy and missing requested capabilities. (If you notice any other issues within my code please point them out in a comment)

11
  • Please post only the necessary part of the code. which part confused you and what make you confuse? Commented Jul 12, 2013 at 20:45
  • Every feature opens a new window in IE. I guess I will cut out support code. it may have something to do with the Before and After. Though it may not. Commented Jul 12, 2013 at 20:47
  • please re-edit your code. And put only the confusion part,what works we don't need.rather what is not working,point us to that part only. Commented Jul 12, 2013 at 20:49
  • I have edited it and cut out the extra code. Commented Jul 12, 2013 at 20:50
  • okay.. say your code opened the browser windows in the order A,B,C. Now in what order you want to close them ? How would you understand browser completed the tasks and then you will decide to close that. Commented Jul 12, 2013 at 20:53

3 Answers 3

9

The Before hook is run before each scenario. This is why a new browser is opened each time.

Do the following instead (in the env.rb):

require "selenium-webdriver"

driver = Selenium::WebDriver.for :ie
accept_next_alert = true
driver.manage.timeouts.implicit_wait = 30
driver.manage.timeouts.script_timeout = 30
verification_errors = []

Before do
  @driver = driver
end

at_exit do
  driver.close
end

In this case, a browser will be opened at the start (before any tests). Then each test will grab that browser and continue using it.

Note: While it is usually okay to re-use the browser across tests. You should be careful about tests that need to be run in a specific order (ie become dependent). Dependent tests can be hard to debug and maintain.

Sign up to request clarification or add additional context in comments.

3 Comments

I took out the driver.close in the After. it gave me this error: session 1c7e2090-08f6-4dc9-aa4d-d407a8b2dea2 does not exist (Selenium::WebDriver::Error::NoSuchDriverError). Any idea what this could be? though I think this may just be the answer. Without the driver.close the test ran within the same IE window.
I will update later when I find out what that error is from. I think at the moment it is due to the background of my first step is checking the current url of @driver. It seems as the the code in my first step is executing before the env.rb?
Sorry, my mistake. It should have been at_exit instead of After. After will run after each scenario - ie will try to close the browser after each scenario. at_exit will run at the very end after all scenarios. See the Hooks page.
2

I had a similar problem in creating a spec_helper file. I did the following (simplified for locally-run firefox) for my purposes and it works very, very reliably. RSpec will use the same browser window for all it blocks in your _spec.rb file.

Rspec.configure do |config|
  config.before(:all) do
    @driver = Selenium::WebDriver.for :firefox
  end

  config.after(:all) do
    @driver.quit
  end
end

If you switch to :each instead of :all, you can use a separate browser instance for each assertion block... again, with :each RSpec will give a new browser instance for each it. Both are useful depending on the circumstance.

Comments

0

As the answers solve the problem but do not answer the question "How to connect to an existing session".

I managed to do this with the following code since it is not officially supported.

# monkey-patch 2 methods
module Selenium
  module WebDriver
    class Driver
      # Be able to set the driver
      def set_bridge_to(b)
        @bridge = b
      end

      # bridge is a private method, simply create a public version
      def public_bridge
        @bridge
      end
    end
  end
end


caps = Selenium::WebDriver::Remote::Capabilities.send("chrome")

driver = Selenium::WebDriver.for(
  :remote,
  url: "http://chrome:4444/wd/hub",
  desired_capabilities: caps
)
used_bridge = driver.bridge
driver.get('https://www.google.com')

# opens a new unused chrome window
driver2 = Selenium::WebDriver.for(
  :remote,
  url: "http://chrome:4444/wd/hub",
  desired_capabilities: caps
)

driver2.close() # close unused chrome window
driver2.set_bridge_to(used_bridge)

driver2.title # => "Google"

Sadly this did not test work between 2 rescue jobs, will update this in the future when I made it work for my own use case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.