这两天搞了一下selenium-on-rails, 作了一些讨论

0. 建议单独建立一个环境 selenium,用法见doc
0.1. 可以另开一个数据库用于selenium测试,但一般会用test库,以及测试夹具,但是有些只适合selenium测试的夹具,为了不影响其他测试,需要把这些夹具单独存放并保证不影响其他测试
举个例子[源自doc],建立空的夹具文件放在test/fixtures/blank下,然后http://localhost:3000/selenium/setup?fixtures=blank/* [可能需要转义一下],就会装载blank下的所有夹具,实际效果是讲数据库表清空

1. 关于测试夹具 fixtures 装入无效
有时会存在莫名其妙的无效问题,举个例子
使用selenium-on-rails的一些讨论 [0,1]open "/selenium/setup?fixtures=blank/*"
使用selenium-on-rails的一些讨论 [0,1]open 
"/selenium/setup?fixtures=all"  # => 这一行会失效

有时候第一行也可能直接失效
查看selenium-on-rails的源代码
 1使用selenium-on-rails的一些讨论 [0,1]#selenium-on-rails/lib/selenium-on-rails/fixture_loader.rb
 2使用selenium-on-rails的一些讨论 [0,1]
 3使用selenium-on-rails的一些讨论 [0,1]  def load_fixtures fixtures_param
 4使用selenium-on-rails的一些讨论 [0,1]    available = nil
 5使用selenium-on-rails的一些讨论 [0,1]    fixtures = fixtures_param.split(/\s*,\s*/).collect do |f|
 6使用selenium-on-rails的一些讨论 [0,1]      fixture_set = File.dirname f
 7使用selenium-on-rails的一些讨论 [0,1]      fixture_set = '' if fixture_set == '.'
 8使用selenium-on-rails的一些讨论 [0,1]      fixture = File.basename f
 9使用selenium-on-rails的一些讨论 [0,1]      if fixture == 'all'
10使用selenium-on-rails的一些讨论 [0,1]        available ||= available_fixtures
11使用selenium-on-rails的一些讨论 [0,1]        available[fixture_set]
12使用selenium-on-rails的一些讨论 [0,1]      else
13使用selenium-on-rails的一些讨论 [0,1]        f
14使用selenium-on-rails的一些讨论 [0,1]      end
15使用selenium-on-rails的一些讨论 [0,1]    end
16使用selenium-on-rails的一些讨论 [0,1]    fixtures.flatten!
17  end

第20行是我人为加入的,去掉其注释后问题可解决
原理:
在console里作了一下实验
使用selenium-on-rails的一些讨论 [0,1] Fixtures.create_fixtures(File.join(RAILS_ROOT,"test/fixtures"),"teachers")  
使用selenium-on-rails的一些讨论 [0,1]#
=> 这句会成功
使用selenium-on-rails的一些讨论 [0,1]
使用selenium-on-rails的一些讨论 [0,1]Fixtures.create_fixtures(File.join(RAILS_ROOT,
"test/fixtures"),"blank/teachers")
使用selenium-on-rails的一些讨论 [0,1]#
=> 这句和上句输出一样,并未把库清空
使用selenium-on-rails的一些讨论 [0,1]
下面
使用selenium-on-rails的一些讨论 [0,1]Fixtures.create_fixtures(File.join(RAILS_ROOT,"test/fixtures"),"teachers")  
使用selenium-on-rails的一些讨论 [0,1]#
=> 这句会成功
使用selenium-on-rails的一些讨论 [0,1]
使用selenium-on-rails的一些讨论 [0,1]Fixtures.reset_cache
使用selenium-on-rails的一些讨论 [0,1]#
=> 清空fixtures缓存,rails为了保证fixtures加载速度,启用了缓存机制
使用selenium-on-rails的一些讨论 [0,1]
使用selenium-on-rails的一些讨论 [0,1]Fixtures.create_fixtures(File.join(RAILS_ROOT,
"test/fixtures"),"blank/teachers")
使用selenium-on-rails的一些讨论 [0,1]#
=> 清空库


相关文章: