0. 建议单独建立一个环境 selenium,用法见doc
0.1. 可以另开一个数据库用于selenium测试,但一般会用test库,以及测试夹具,但是有些只适合selenium测试的夹具,为了不影响其他测试,需要把这些夹具单独存放并保证不影响其他测试
举个例子[源自doc],建立空的夹具文件放在test/fixtures/blank下,然后http://localhost:3000/selenium/setup?fixtures=blank/* [可能需要转义一下],就会装载blank下的所有夹具,实际效果是讲数据库表清空
1. 关于测试夹具 fixtures 装入无效
有时会存在莫名其妙的无效问题,举个例子
open
"
/selenium/setup?fixtures=blank/*
"
open " /selenium/setup?fixtures=all " # => 这一行会失效
open " /selenium/setup?fixtures=all " # => 这一行会失效
有时候第一行也可能直接失效
查看selenium-on-rails的源代码
1
#selenium
-
on
-
rails
/
lib
/
selenium
-
on
-
rails
/
fixture_loader.rb
2
3 def load_fixtures fixtures_param
4 available = nil
5 fixtures = fixtures_param.split( / \s * ,\s */ ).collect do | f |
6 fixture_set = File.dirname f
7 fixture_set = '' if fixture_set == ' . '
8 fixture = File.basename f
9 if fixture == ' all '
10 available ||= available_fixtures
11 available[fixture_set]
12 else
13 f
14 end
15 end
16 fixtures.flatten !
17 fixtures.reject ! {|f| f.blank? }
18
19 if fixtures.any ?
20 # Fixtures.reset_cache ## tachikmoma add
21 Fixtures.create_fixtures fixtures_path, fixtures
22 end
23 fixtures
24 end
2
3 def load_fixtures fixtures_param
4 available = nil
5 fixtures = fixtures_param.split( / \s * ,\s */ ).collect do | f |
6 fixture_set = File.dirname f
7 fixture_set = '' if fixture_set == ' . '
8 fixture = File.basename f
9 if fixture == ' all '
10 available ||= available_fixtures
11 available[fixture_set]
12 else
13 f
14 end
15 end
16 fixtures.flatten !
17 fixtures.reject ! {|f| f.blank? }
18
19 if fixtures.any ?
20 # Fixtures.reset_cache ## tachikmoma add
21 Fixtures.create_fixtures fixtures_path, fixtures
22 end
23 fixtures
24 end
第20行是我人为加入的,去掉其注释后问题可解决
原理:
在console里作了一下实验
Fixtures.create_fixtures(File.join(RAILS_ROOT,
"
test/fixtures
"
),
"
teachers
"
)
# => 这句会成功
Fixtures.create_fixtures(File.join(RAILS_ROOT, " test/fixtures " ), " blank/teachers " )
# => 这句和上句输出一样,并未把库清空
下面
# => 这句会成功
Fixtures.create_fixtures(File.join(RAILS_ROOT, " test/fixtures " ), " blank/teachers " )
# => 这句和上句输出一样,并未把库清空
Fixtures.create_fixtures(File.join(RAILS_ROOT,
"
test/fixtures
"
),
"
teachers
"
)
# => 这句会成功
Fixtures.reset_cache
# => 清空fixtures缓存,rails为了保证fixtures加载速度,启用了缓存机制
Fixtures.create_fixtures(File.join(RAILS_ROOT, " test/fixtures " ), " blank/teachers " )
# => 清空库
# => 这句会成功
Fixtures.reset_cache
# => 清空fixtures缓存,rails为了保证fixtures加载速度,启用了缓存机制
Fixtures.create_fixtures(File.join(RAILS_ROOT, " test/fixtures " ), " blank/teachers " )
# => 清空库