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)

Community
  • 1
  • 1
megan
  • 305
  • 1
  • 6
  • 18
  • Please post only the necessary part of the code. which part confused you and what make you confuse? – Arup Rakshit Jul 12 '13 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. – megan Jul 12 '13 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. – Arup Rakshit Jul 12 '13 at 20:49
  • I have edited it and cut out the extra code. – megan Jul 12 '13 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. – Arup Rakshit Jul 12 '13 at 20:53
  • Each feature tests a functionality of a webpage. Features for a particular page need to be done in a particular order in the same browser instance. – megan Jul 12 '13 at 20:54
  • I do not want to close them I want to use the same browser window A with all test cases. I do not need a B or C. – megan Jul 12 '13 at 20:56
  • Oh I see, no, right now I am only in IE 8. When I run my features it is opening a new IE window for every feature. – megan Jul 12 '13 at 20:57
  • Still don't know what gives you trouble? – Arup Rakshit Jul 12 '13 at 20:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33390/discussion-between-priti-and-megaxelize) – Arup Rakshit Jul 12 '13 at 21:03
  • It's okay. Okay I have 3 features that test buttons on a webpage, the buttons highlight things, change colors on the page, display information. So there is not navigating away form this one particular page. Each of these features creates a new instance of IE to runs its test in so. Test 1: Opens IE -navigates to webpage- performs test; Test 2: Opens IE -navigates to webpage- performs test; Test 3: Opens IE -navigates to webpage- performs test. SO in the end I have 3 browser windows open and 2 times selenium navigated to a webpage that was already open and available. – megan Jul 12 '13 at 21:03

3 Answers3

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.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • 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. – megan Jul 12 '13 at 21:28
  • 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? – megan Jul 12 '13 at 21:32
  • 1
    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](https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks) page. – Justin Ko Jul 12 '13 at 21:32
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.

hardknocks
  • 155
  • 1
  • 7
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.