论坛首页 编程语言技术论坛

在selenium测试中使用ActiveRecord

浏览 3377 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-10  
ActiveRecord是rails的框架,我们在selenium测试中肯定需要初始化数据或者清理数据库数据以保证各个测试之间的独立性,可以这样用ActiveRecord来初始化或清理数据库
ruby 代码
 
  1. require 'test/unit'  
  2. require 'selenium'  
  3. require 'active_record'  
  4. ActiveRecord::Base.establish_connection(    
  5.   :adapter => "mysql",    
  6.   :host => "localhost",    
  7.   :username => "dcrm",    
  8.   :password => "dcrm",    
  9.   :database => "dcrm2_test"    
  10. )    
  11.   
  12. class FunctionalTestCase < Test::Unit::TestCase  
  13.     
  14.   @wait_default_time="30000"  
  15.   
  16.   def setup  
  17.     initBrowser unless @selenium  
  18.   end  
  19.      
  20.   def teardown  
  21.     logout  
  22.     clear_database  
  23.     closeBrowser  
  24.   end      
  25.     
  26.   def logout  
  27.     @selenium.open "http://localhost:3000/logout"      
  28.     wait_default  
  29.   end  
  30.     
  31.   def closeBrowser()  
  32.     @selenium.stop  
  33.   end  
  34.     
  35.   def initBrowser  
  36.     @selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore""http://localhost:4444", 10000);  
  37.     @selenium.start  
  38.   end  
  39.     
  40.   def wait_default  
  41.     @selenium.wait_for_page_to_load @wait_default_time  
  42.   end  
  43.     
  44.   def test_logout      
  45.     logout      
  46.     @selenium.is_text_present("用户登录")  
  47.   end  
  48.     
  49.   def clear_database  
  50.     connection = ActiveRecord::Base.connection  
  51.     connection.execute("DELETE from channels")   
  52.     ......  
  53.   end    
  54.    
  55. end  

其中
ruby 代码
 
  1. require 'active_record'  
  2. ActiveRecord::Base.establish_connection(    
  3.   :adapter => "mysql",    
  4.   :host => "localhost",    
  5.   :username => "dcrm",    
  6.   :password => "dcrm",    
  7.   :database => "dcrm2_development"    
  8. )  

建立了DB Connection
   发表时间:2007-08-13  
怎么多没看到MM的
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics