You can do what you want by creating a custom test runner type that has a before and after all tests method. You could then create a selenium-webdriver instance before all tests and close it after all tests.
Here is an example of starting a browser and going to Google before all tests. Each test then re-uses the same browser.
require 'minitest/autorun'
require 'selenium-webdriver'
#Add before and after suite hooks using a custom runner
class MyMiniTest
class Unit < MiniTest::Unit
def _run_suite(suite, type)
begin
suite.before_suite if suite.respond_to?(:before_suite)
super(suite, type)
ensure
suite.after_suite if suite.respond_to?(:after_suite)
end
end
end
end
MiniTest::Unit.runner = MyMiniTest::Unit.new
class GoogleTest < MiniTest::Unit::TestCase
def self.before_suite
p "before all tests"
@@driver = Selenium::WebDriver.for :firefox
@@driver.navigate.to 'http://www.google.com'
end
def self.after_suite
p "after all tests"
@@driver.quit
end
def setup
p 'setup before each test'
end
def teardown
p 'teardown after each test'
end
def test1
p 'test1'
assert_equal(0, @@driver.find_elements(:xpath, '//div').length)
end
def test2
p 'test2'
assert_equal(0, @@driver.find_elements(:xpath, '//a').length)
end
end
You can see the order that things are run by the output:
"before all tests"
"setup before each test"
"test1"
"teardown after each test"
"setup before each test"
"test2"
"teardown after each test"
"after all tests"
Note that variables you want to share across tests will need to be class variables (ie prefixed with @@).