capy 1.4.4 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,6 +39,10 @@ and capy with '-w' option
39
39
 
40
40
  $ capy -w example.capy
41
41
 
42
+ ### Import other files
43
+
44
+ import 'examples/google_search.capy'
45
+
42
46
  ### To access the elements within IFrame
43
47
 
44
48
  within_frame('IFRAME_ID') do
@@ -3,6 +3,7 @@ require "capy/version"
3
3
  require "slop"
4
4
  require "colored"
5
5
  require 'capybara/dsl'
6
+ require 'capy/evaluator'
6
7
 
7
8
  module Capy
8
9
  class << self
@@ -25,12 +26,12 @@ module Capy
25
26
 
26
27
  @mode = opts.js? ? :javascript : :capybara
27
28
 
28
- evaluater = Evaluater.new
29
+ evaluator = Evaluator.new
29
30
 
30
- evaluater.visit Capybara.app_host if Capybara.app_host
31
+ evaluator.visit Capybara.app_host if Capybara.app_host
31
32
 
32
33
  if args.empty?
33
- start_shell evaluater
34
+ start_shell evaluator
34
35
  else
35
36
  args.each do |script_file|
36
37
  unless File.exists?(script_file)
@@ -38,9 +39,9 @@ module Capy
38
39
  return 1
39
40
  end
40
41
  puts "Running: #{script_file} ..."
41
- result = evaluater.eval_script File.read(script_file), mode
42
+ result = evaluator.eval_script File.read(script_file), mode, script_file
42
43
  puts "=> #{result.inspect}".cyan
43
- start_shell evaluater if opts.stop?
44
+ start_shell evaluator if opts.stop?
44
45
  end
45
46
  end
46
47
 
@@ -71,12 +72,12 @@ module Capy
71
72
 
72
73
  EXIT_COMMANDS = %w(exit quit)
73
74
 
74
- def start_shell(evaluater)
75
+ def start_shell(evaluator)
75
76
  return if @_start_shell
76
77
  @_start_shell = true
77
78
 
78
79
  Readline.completion_proc = lambda do |text|
79
- (Evaluater.instance_methods - Object.methods + EXIT_COMMANDS).grep(/^#{Regexp.quote(text.strip)}/)
80
+ (Evaluator.instance_methods - Object.methods + EXIT_COMMANDS).grep(/^#{Regexp.quote(text.strip)}/)
80
81
  end
81
82
 
82
83
  history_file = File.expand_path('~/.capy_history')
@@ -101,7 +102,7 @@ module Capy
101
102
  return
102
103
  else
103
104
  begin
104
- result = evaluater.eval_script(buf, mode)
105
+ result = evaluator.eval_script(buf, mode)
105
106
  puts "=> #{result.inspect}".cyan
106
107
  rescue Exception => e
107
108
  error e
@@ -119,78 +120,4 @@ module Capy
119
120
  puts "\t#{e.backtrace[1..-1].join("\n\t")}".red
120
121
  end
121
122
  end
122
-
123
- class Evaluater
124
- include Capybara::DSL
125
-
126
- def initialize
127
- if page.driver.respond_to?(:header)
128
- page.driver.header(
129
- 'user-agent',
130
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3'
131
- )
132
- end
133
- end
134
-
135
- def eval_script(script, mode)
136
- case mode
137
- when :javascript
138
- javascript script
139
- else
140
- capybara script
141
- end
142
- end
143
-
144
- def javascript(script)
145
- page.evaluate_script script.sub(/\A#!.*\n/, '')
146
- end
147
- alias_method :js, :javascript
148
-
149
- def capybara(script)
150
- instance_eval script
151
- end
152
-
153
- def take_screenshot(png_path = nil)
154
- png_path = gen_uniq_file_name('Screen Shot', 'png') unless png_path
155
- case Capybara.current_driver
156
- when :webkit
157
- driver.render(png_path)
158
- else
159
- browser.save_screenshot(png_path)
160
- end
161
- png_path
162
- end
163
-
164
- def driver
165
- page.driver
166
- end
167
-
168
- def browser
169
- driver.browser
170
- end
171
-
172
- def host(app_host)
173
- Capybara.app_host = app_host
174
- end
175
-
176
- def stop
177
- Capy.start_shell(self)
178
- end
179
-
180
- private
181
-
182
- def gen_uniq_file_name(prefix, extension)
183
- file_name = "#{prefix} #{Time.now}"
184
- i = 2
185
- while File.exists?("#{file_name}.#{extension}")
186
- file_name = if file_name =~ /\(\d+\)$/
187
- file_name.sub(/\(\d+\)$/, i.to_s)
188
- else
189
- file_name + " (#{i})"
190
- end
191
- i += 1
192
- end
193
- "#{file_name}.#{extension}"
194
- end
195
- end
196
123
  end
@@ -0,0 +1,97 @@
1
+ module Capy
2
+
3
+ class Evaluator
4
+ include Capybara::DSL
5
+
6
+ def initialize
7
+ if page.driver.respond_to?(:header)
8
+ page.driver.header(
9
+ 'user-agent',
10
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3'
11
+ )
12
+ end
13
+ end
14
+
15
+ def eval_script(script, mode, file_path = nil)
16
+ prev_file, @current_file = @current_file, file_path if file_path
17
+ prev_mode, @current_mode = @current_mode, mode
18
+
19
+ case mode
20
+ when :javascript
21
+ javascript script
22
+ else
23
+ capybara script
24
+ end
25
+ ensure
26
+ @current_file = prev_file if file_path
27
+ @current_mode = prev_mode
28
+ end
29
+
30
+ def import(file_name)
31
+ script_file = if @current_file
32
+ File.join(File.dirname(@current_file), file_name)
33
+ else
34
+ file_name
35
+ end
36
+
37
+ if File.exists?(script_file)
38
+ eval_script(File.read(script_file), @current_mode, script_file)
39
+ else
40
+ raise "No such file: #{script_file}"
41
+ end
42
+ end
43
+
44
+ def javascript(script)
45
+ page.evaluate_script script.sub(/\A#!.*\n/, '')
46
+ end
47
+ alias_method :js, :javascript
48
+
49
+ def capybara(script)
50
+ instance_eval script
51
+ end
52
+
53
+ def take_screenshot(png_path = nil)
54
+ png_path = gen_uniq_file_name('Screen Shot', 'png') unless png_path
55
+ case Capybara.current_driver
56
+ when :webkit
57
+ driver.render(png_path)
58
+ else
59
+ browser.save_screenshot(png_path)
60
+ end
61
+ png_path
62
+ end
63
+
64
+ def driver
65
+ page.driver
66
+ end
67
+
68
+ def browser
69
+ driver.browser
70
+ end
71
+
72
+ def host(app_host)
73
+ Capybara.app_host = app_host
74
+ end
75
+
76
+ def stop
77
+ Capy.start_shell(self)
78
+ end
79
+
80
+ private
81
+
82
+ def gen_uniq_file_name(prefix, extension)
83
+ file_name = "#{prefix} #{Time.now}"
84
+ i = 2
85
+ while File.exists?("#{file_name}.#{extension}")
86
+ file_name = if file_name =~ /\(\d+\)$/
87
+ file_name.sub(/\(\d+\)$/, i.to_s)
88
+ else
89
+ file_name + " (#{i})"
90
+ end
91
+ i += 1
92
+ end
93
+ "#{file_name}.#{extension}"
94
+ end
95
+ end
96
+
97
+ end
@@ -1,3 +1,3 @@
1
1
  module Capy
2
- VERSION = "1.4.4"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -24,8 +24,8 @@ describe Capy do
24
24
 
25
25
  context '$ capy -a http://google.com/' do
26
26
  it 'visit http://google.com/' do
27
- any_instance_of(Capy::Evaluater) do |evaluater|
28
- mock(evaluater).visit('http://google.com/')
27
+ any_instance_of(Capy::Evaluator) do |evaluator|
28
+ mock(evaluator).visit('http://google.com/')
29
29
  end
30
30
  mock(Capy).start_shell.with_any_args
31
31
  Capy.run(%w(-a http://google.com/)).should == 0
@@ -37,8 +37,8 @@ describe Capy do
37
37
  it 'eval script' do
38
38
  file = File.expand_path('../fixtures/foo.capy', __FILE__)
39
39
  script = File.read(file)
40
- any_instance_of(Capy::Evaluater) do |evaluater|
41
- mock(evaluater).eval_script(script, :capybara)
40
+ any_instance_of(Capy::Evaluator) do |evaluator|
41
+ mock(evaluator).eval_script(script, :capybara, file)
42
42
  end
43
43
  Capy.run([file]).should == 0
44
44
  end
@@ -46,8 +46,8 @@ describe Capy do
46
46
 
47
47
  context 'file is not exists' do
48
48
  it 'does not eval script' do
49
- any_instance_of(Capy::Evaluater) do |evaluater|
50
- mock(evaluater).eval_script.with_any_args.never
49
+ any_instance_of(Capy::Evaluator) do |evaluator|
50
+ mock(evaluator).eval_script.with_any_args.never
51
51
  end
52
52
  Capy.run(%w(bar.capy)).should == 1
53
53
  end
@@ -57,8 +57,8 @@ describe Capy do
57
57
  context '$ capy foo.capy' do
58
58
  it 'eval script and stop' do
59
59
  file = File.expand_path('../fixtures/foo.capy', __FILE__)
60
- any_instance_of(Capy::Evaluater) do |evaluater|
61
- mock(evaluater).eval_script.with_any_args
60
+ any_instance_of(Capy::Evaluator) do |evaluator|
61
+ mock(evaluator).eval_script.with_any_args
62
62
  end
63
63
  mock(Capy).start_shell.with_any_args
64
64
  Capy.run(['-s', file]).should == 0
@@ -77,8 +77,8 @@ describe Capy do
77
77
  it 'eval script' do
78
78
  file = File.expand_path('../fixtures/foo.js', __FILE__)
79
79
  script = File.read(file)
80
- any_instance_of(Capy::Evaluater) do |evaluater|
81
- mock(evaluater).eval_script(script, :javascript)
80
+ any_instance_of(Capy::Evaluator) do |evaluator|
81
+ mock(evaluator).eval_script(script, :javascript, file)
82
82
  end
83
83
  Capy.run(['-j', file]).should == 0
84
84
  end
@@ -93,7 +93,7 @@ describe Capy do
93
93
  end
94
94
 
95
95
  describe 'gen_uniq_file_name' do
96
- let!(:evaluater) { Capy::Evaluater.new }
96
+ let!(:evaluator) { Capy::Evaluator.new }
97
97
  let!(:now) { Time.now }
98
98
 
99
99
  before do
@@ -102,7 +102,7 @@ describe Capy do
102
102
 
103
103
  context 'when file does not exists' do
104
104
  it do
105
- evaluater.send(:gen_uniq_file_name, 'Foo', :png).should == "Foo #{now}.png"
105
+ evaluator.send(:gen_uniq_file_name, 'Foo', :png).should == "Foo #{now}.png"
106
106
  end
107
107
  end
108
108
 
@@ -110,7 +110,7 @@ describe Capy do
110
110
  it do
111
111
  mock(File).exists?("Foo #{now}.png") { true }
112
112
  mock(File).exists?("Foo #{now} (2).png") { false }
113
- evaluater.send(:gen_uniq_file_name, 'Foo', :png).should == "Foo #{now} (2).png"
113
+ evaluator.send(:gen_uniq_file_name, 'Foo', :png).should == "Foo #{now} (2).png"
114
114
  end
115
115
  end
116
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000Z
12
+ date: 2012-04-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slop
16
- requirement: &70245184336920 !ruby/object:Gem::Requirement
16
+ requirement: &70155630974880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70245184336920
24
+ version_requirements: *70155630974880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara
27
- requirement: &70245184336300 !ruby/object:Gem::Requirement
27
+ requirement: &70155630974460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70245184336300
35
+ version_requirements: *70155630974460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: colored
38
- requirement: &70245184335660 !ruby/object:Gem::Requirement
38
+ requirement: &70155630974000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70245184335660
46
+ version_requirements: *70155630974000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70245184334940 !ruby/object:Gem::Requirement
49
+ requirement: &70155630973580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70245184334940
57
+ version_requirements: *70155630973580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rr
60
- requirement: &70245184333980 !ruby/object:Gem::Requirement
60
+ requirement: &70155630973160 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70245184333980
68
+ version_requirements: *70155630973160
69
69
  description: Capybara Script Runner
70
70
  email:
71
71
  - jugyo.org@gmail.com
@@ -85,6 +85,7 @@ files:
85
85
  - examples/twitter.capy
86
86
  - examples/wikipedia_search.capy
87
87
  - lib/capy.rb
88
+ - lib/capy/evaluator.rb
88
89
  - lib/capy/version.rb
89
90
  - spec/capy_spec.rb
90
91
  - spec/fixtures/foo.capy