ActiveRecord是rails的框架,我们在selenium测试中肯定需要初始化数据或者清理数据库数据以保证各个测试之间的独立性,可以这样用ActiveRecord来初始化或清理数据库
require 'test/unit'
require 'selenium'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "dcrm",
:password => "dcrm",
:database => "dcrm2_test"
)
class FunctionalTestCase < Test::Unit::TestCase
@wait_default_time="30000"
def setup
initBrowser unless @selenium
end
def teardown
logout
clear_database
closeBrowser
end
def logout
@selenium.open "http://localhost:3000/logout"
wait_default
end
def closeBrowser()
@selenium.stop
end
def initBrowser
@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://localhost:4444", 10000);
@selenium.start
end
def wait_default
@selenium.wait_for_page_to_load @wait_default_time
end
def test_logout
logout
assert @selenium.is_text_present("用户登录")
end
def clear_database
connection = ActiveRecord::Base.connection
connection.execute("DELETE from channels")
end
end 其中
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "dcrm",
:password => "dcrm",
:database => "dcrm2_development"
) 建立了DB Connection