capy 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,6 +31,19 @@ Change the browser:
31
31
 
32
32
  $ capy -b firefox example.capy
33
33
 
34
+ ### JavaScript Mode
35
+
36
+ # example.js
37
+
38
+ location 'http://www.wikipedia.org/'
39
+ ...
40
+
41
+ run capy with '-j' option
42
+
43
+ $ capy -j example.js
44
+
45
+ See also the [examples](https://github.com/jugyo/capy/tree/master/examples).
46
+
34
47
  ## Contributing
35
48
 
36
49
  1. Fork it
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task :default => :spec
data/bin/capy CHANGED
@@ -2,4 +2,4 @@
2
2
  require 'capy'
3
3
  args = ARGV.dup
4
4
  ARGV.clear
5
- Capy.run(args)
5
+ exit Capy.run(args)
data/capy.gemspec CHANGED
@@ -18,4 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_runtime_dependency 'slop', '~>3.1'
19
19
  gem.add_runtime_dependency 'capybara'
20
20
  gem.add_runtime_dependency 'colored'
21
+
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "rr"
21
24
  end
File without changes
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env capy -s -j
2
+ location.href = 'http://twitter.com'
File without changes
data/lib/capy.rb CHANGED
@@ -6,14 +6,17 @@ require 'capybara/dsl'
6
6
 
7
7
  module Capy
8
8
  class << self
9
- attr_reader :opts
9
+ attr_reader :opts, :mode
10
10
 
11
11
  def run(args)
12
- @opts = Slop.parse!(args, :help => true) do
12
+ @opts = Slop.parse!(args, :help => true, :strict => true) do
13
13
  banner "capy [script.capy]\n"
14
14
  on :b, :browser=, 'chrome, firefox', :default => 'chrome'
15
+ on :j, :js, 'eval script as javascript with -a option'
16
+ on :a, :'app-host=', 'app host'
17
+ on :s, :'stop', 'stop after eval script'
15
18
  end
16
- exit if opts.help?
19
+ return 1 if opts.help?
17
20
 
18
21
  trap('INT') { exit }
19
22
 
@@ -21,21 +24,39 @@ module Capy
21
24
  Capybara::Selenium::Driver.new(app, :browser => opts[:browser].to_sym)
22
25
  end
23
26
  Capybara.current_driver = :selenium
27
+ Capybara.app_host = opts[:'app-host']
28
+
29
+ @mode = opts.js? ? :javascript : :capybara
30
+
31
+ evaluater = Evaluater.new
32
+ evaluater.visit (Capybara.app_host) if Capybara.app_host
24
33
 
25
34
  if args.empty?
26
- start_shell
35
+ start_shell evaluater
27
36
  else
28
37
  args.each do |script_file|
29
- abort "No such file: #{script_file}".red unless File.exists?(script_file)
38
+ unless File.exists?(script_file)
39
+ puts "No such file: #{script_file}".red
40
+ return 1
41
+ end
30
42
  puts "Running: #{script_file} ..."
31
- eval_script script_file
43
+ evaluater.eval_script File.read(script_file), mode
44
+ start_shell evaluater if opts.stop?
32
45
  end
33
46
  end
47
+
48
+ 0
49
+ rescue Slop::InvalidOptionError => e
50
+ puts e.message.red
51
+ 1
52
+ rescue => e
53
+ error e
54
+ 1
34
55
  end
35
56
 
36
57
  EXIT_COMMANDS = %w(exit quit)
37
58
 
38
- def start_shell(evaluater = Evaluater.new)
59
+ def start_shell(evaluater)
39
60
  return if @_start_shell
40
61
  @_start_shell = true
41
62
 
@@ -65,7 +86,7 @@ module Capy
65
86
  return
66
87
  else
67
88
  begin
68
- result = evaluater.instance_eval(buf)
89
+ result = evaluater.eval_script(buf, mode)
69
90
  puts "=> #{result.inspect}".cyan
70
91
  rescue => e
71
92
  error e
@@ -76,17 +97,10 @@ module Capy
76
97
  @_start_shell = false
77
98
  end
78
99
 
79
- def eval_script(script_file)
80
- evaluater = Evaluater.new
81
- evaluater.instance_eval(File.read(script_file), script_file, 1)
82
- rescue => e
83
- error e
84
- end
85
-
86
100
  private
87
101
 
88
102
  def error(e)
89
- puts "#{e.backtrace[0]} #{e}".red
103
+ puts "#{e.backtrace[0]} #{e} (#{e.class})".red
90
104
  puts "\t#{e.backtrace[1..-1].join("\n\t")}".red
91
105
  end
92
106
  end
@@ -94,6 +108,24 @@ module Capy
94
108
  class Evaluater
95
109
  include Capybara::DSL
96
110
 
111
+ def eval_script(script, mode)
112
+ case mode
113
+ when :javascript
114
+ javascript script
115
+ else
116
+ capybara script
117
+ end
118
+ end
119
+
120
+ def javascript(script)
121
+ page.evaluate_script script.sub(/\A#!.*\n/, '')
122
+ end
123
+ alias_method :js, :javascript
124
+
125
+ def capybara(script)
126
+ instance_eval script
127
+ end
128
+
97
129
  def host(app_host)
98
130
  Capybara.app_host = app_host
99
131
  end
data/lib/capy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Capy
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/spec/capy_spec.rb ADDED
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capy do
4
+ before do
5
+ stub(Capy).start_shell.with_any_args
6
+ end
7
+
8
+ context '$ capy' do
9
+ it 'start shell' do
10
+ mock(Capy).start_shell.with_any_args
11
+ Capy.run([]).should == 0
12
+ end
13
+ end
14
+
15
+ context '$ capy -h' do
16
+ it 'show help' do
17
+ Capy.run(%w(-h)).should == 1
18
+ end
19
+ end
20
+
21
+ context '$ capy -b firefox' do
22
+
23
+ end
24
+
25
+ context '$ capy -a http://google.com/' do
26
+ it 'visit http://google.com/' do
27
+ any_instance_of(Capy::Evaluater) do |evaluater|
28
+ mock(evaluater).visit('http://google.com/')
29
+ end
30
+ mock(Capy).start_shell.with_any_args
31
+ Capy.run(%w(-a http://google.com/)).should == 0
32
+ end
33
+ end
34
+
35
+ context '$ capy foo.capy' do
36
+ context 'file is exists' do
37
+ it 'eval script' do
38
+ file = File.expand_path('../fixtures/foo.capy', __FILE__)
39
+ script = File.read(file)
40
+ any_instance_of(Capy::Evaluater) do |evaluater|
41
+ mock(evaluater).eval_script(script, :capybara)
42
+ end
43
+ Capy.run([file]).should == 0
44
+ end
45
+ end
46
+
47
+ context 'file is not exists' do
48
+ it 'does not eval script' do
49
+ any_instance_of(Capy::Evaluater) do |evaluater|
50
+ mock(evaluater).eval_script.with_any_args.never
51
+ end
52
+ Capy.run(%w(bar.capy)).should == 1
53
+ end
54
+ end
55
+ end
56
+
57
+ context '$ capy foo.capy' do
58
+ it 'eval script and stop' do
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
62
+ end
63
+ mock(Capy).start_shell.with_any_args
64
+ Capy.run(['-s', file]).should == 0
65
+ end
66
+ end
67
+
68
+ context '$ capy -j foo.js' do
69
+ it 'does not eval script' do
70
+ mock(Capy).eval_script_file.with_any_args.never
71
+ Capy.run(%w(-j foo.js)).should == 1
72
+ end
73
+ end
74
+
75
+ context '$ capy -j foo.js' do
76
+ context 'file is exists' do
77
+ it 'eval script' do
78
+ file = File.expand_path('../fixtures/foo.js', __FILE__)
79
+ script = File.read(file)
80
+ any_instance_of(Capy::Evaluater) do |evaluater|
81
+ mock(evaluater).eval_script(script, :javascript)
82
+ end
83
+ Capy.run(['-j', file]).should == 0
84
+ end
85
+ end
86
+
87
+ context 'file is not exists' do
88
+ it 'does not eval script' do
89
+ mock(Capy).eval_script_file.with_any_args.never
90
+ Capy.run(%w(bar.capy)).should == 1
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1 @@
1
+ visit 'http://example.com/'
@@ -0,0 +1 @@
1
+ alert('foo');
@@ -0,0 +1,7 @@
1
+ require 'capy'
2
+ require 'rspec'
3
+ require 'rr'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rr
7
+ 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.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slop
16
- requirement: &70095394509140 !ruby/object:Gem::Requirement
16
+ requirement: &70160342289140 !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: *70095394509140
24
+ version_requirements: *70160342289140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara
27
- requirement: &70095394508720 !ruby/object:Gem::Requirement
27
+ requirement: &70160342288100 !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: *70095394508720
35
+ version_requirements: *70160342288100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: colored
38
- requirement: &70095394508260 !ruby/object:Gem::Requirement
38
+ requirement: &70160342287140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,29 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70095394508260
46
+ version_requirements: *70160342287140
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70160342286280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70160342286280
58
+ - !ruby/object:Gem::Dependency
59
+ name: rr
60
+ requirement: &70160342281840 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70160342281840
47
69
  description: Capybara Script Runner
48
70
  email:
49
71
  - jugyo.org@gmail.com
@@ -60,9 +82,14 @@ files:
60
82
  - bin/capy
61
83
  - capy.gemspec
62
84
  - examples/google_search.capy
85
+ - examples/twitter.js
63
86
  - examples/wikipedia_search.capy
64
87
  - lib/capy.rb
65
88
  - lib/capy/version.rb
89
+ - spec/capy_spec.rb
90
+ - spec/fixtures/foo.capy
91
+ - spec/fixtures/foo.js
92
+ - spec/spec_helper.rb
66
93
  homepage: https://github.com/jugyo
67
94
  licenses: []
68
95
  post_install_message:
@@ -87,4 +114,8 @@ rubygems_version: 1.8.10
87
114
  signing_key:
88
115
  specification_version: 3
89
116
  summary: The capy command to run the script written in Capybara DSL.
90
- test_files: []
117
+ test_files:
118
+ - spec/capy_spec.rb
119
+ - spec/fixtures/foo.capy
120
+ - spec/fixtures/foo.js
121
+ - spec/spec_helper.rb