conformity 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d8642508831e87cddb514c49e41a861c72e0763
4
- data.tar.gz: 49f1b297869149c45f33b30eeb9a10988269ab8d
3
+ metadata.gz: 01f3786cd9944fbc1a8275c48afcc2dc9128ea83
4
+ data.tar.gz: f0d8722c3d724f21c3bb712e34057911a7bd6f3a
5
5
  SHA512:
6
- metadata.gz: 04448922009d3e0e41f6d1b49502ab18b34d1b492f8d5efa21b3220e12001e164990a41904970df2b8dfb9407a43357cac744e1332ef44c2a6f6baf0854287c5
7
- data.tar.gz: f091d83d17f1206ca70f061b6b40eb88110bb2b27938e2375ff794af52f154f50a38a57b3b93efe1b428aafe43428d62ba37c1ce9213d88bac342f1a9221c9be
6
+ metadata.gz: 0ec8f708b330e8cb8d75d8e856f9f1b766e97cb2780cf73458c639a577153a299e9a75f7b153098e92a86ee90dbdd58debee712e3ad55e143a31d7add4354a13
7
+ data.tar.gz: 643358251d8989f885d6cd189d0ad5fda13402fd3f548420a4bfedb3c17ab2dd66dd24d1b30bf9452f2cf431e013e4379297c5aefc398891df9e886533232396
@@ -0,0 +1,38 @@
1
+ module Conformity
2
+ class Actions
3
+ attr_reader :session
4
+
5
+ ACTIONS = [:visit, :find, :fill_in, :select, :check, :uncheck, :choose,
6
+ :click_on, :wait]
7
+
8
+ def initialize(session = Capybara.current_session)
9
+ @session = session
10
+ end
11
+
12
+ def field(name, options={})
13
+ Field.value(name)
14
+ end
15
+
16
+ def method_missing(name, *args, &block)
17
+ if ACTIONS.include?(name)
18
+ session.send(name, *args, &block)
19
+ screenshot(name) if Conformity.log_screenshots
20
+
21
+ # ignore SuccessConditions
22
+ elsif [:has_status_code?, :has_content?]
23
+ else super
24
+ end
25
+ end
26
+
27
+ # so Kernel#select isn't called
28
+ def select(*args, &block)
29
+ session.send(:select, *args, &block)
30
+ end
31
+
32
+ def screenshot(name)
33
+ file_name = "#{Time.now.to_i}_#{name}.png"
34
+ full_path = Conformity.screenshots_folder + "/" + file_name
35
+ session.send(:save_screenshot, full_path)
36
+ end
37
+ end
38
+ end
@@ -1,13 +1,31 @@
1
1
  module Conformity
2
2
  class Field
3
- attr_reader :name
4
- attr_writer :value
3
+ attr_reader :name, :options
5
4
  attr_accessor :type
6
5
 
6
+ class << self
7
+ def find(name)
8
+ fields[name]
9
+ end
10
+
11
+ def value(name)
12
+ find(name).value
13
+ end
14
+
15
+ def fields
16
+ @fields ||= {}
17
+ end
18
+
19
+ def add_field(field)
20
+ fields[field.name] = field
21
+ end
22
+ end
23
+
7
24
  def initialize(name, options = {})
8
25
  @name = name
9
26
  @options = options[:options]
10
27
  @required = options[:required] || false
28
+ self.class.add_field(self)
11
29
  end
12
30
 
13
31
  def required?
@@ -21,5 +39,13 @@ module Conformity
21
39
  raise FieldError, "Required field '#{name}' not set"
22
40
  end
23
41
  end
42
+
43
+ def value=(value)
44
+ if @options && !@options.include?(value)
45
+ raise FieldError, "#{value} not in #{@options}"
46
+ end
47
+
48
+ @value = value
49
+ end
24
50
  end
25
51
  end
@@ -2,8 +2,7 @@ module Conformity
2
2
  class Form
3
3
  extend Forwardable
4
4
 
5
- METHODS = [:visit, :find, :fill_in, :select, :check, :uncheck, :choose,
6
- :click_on, :wait]
5
+ attr_reader :actions, :block, :success_conditions
7
6
 
8
7
  def_delegators :@success_conditions, :has_content?, :has_status_code?,
9
8
  :success?
@@ -11,7 +10,9 @@ module Conformity
11
10
  def initialize(host = '', &block)
12
11
  set_host(host)
13
12
  @success_conditions = SuccessConditions.new(Capybara.current_session)
14
- instance_eval(&block) if block
13
+ @actions = Actions.new
14
+ @block = block
15
+ instance_eval(&block)
15
16
  end
16
17
 
17
18
  def set_host(host)
@@ -35,7 +36,7 @@ module Conformity
35
36
  Capybara.current_session.reset!
36
37
 
37
38
  begin
38
- actions.each(&:run)
39
+ actions.instance_eval(&block)
39
40
  rescue => e
40
41
  raise FillError, "#{e.message}"
41
42
  end
@@ -49,29 +50,12 @@ module Conformity
49
50
  end
50
51
 
51
52
  def method_missing(name, *args, &block)
52
- if METHODS.include?(name)
53
- action = Action.new(name, args, block)
54
-
55
- case action.name
56
- when :fill_in then action.field.type = :text
57
- when :choose then action.field.type = :radio
58
- when :select then action.field.type = :select
59
- end
60
-
61
- actions << action
62
- else
63
- super
64
- end
65
- end
66
-
67
- # When passing a block to iniatialize using instance_eval the Kernel#select
68
- # method gets called instead of method_missing
69
- def select(*args, &block)
70
- method_missing(:select, *args, &block)
53
+ # ignore actions
54
+ super unless Actions::ACTIONS.include?(name)
71
55
  end
72
56
 
73
- def actions
74
- @actions ||= []
57
+ # so Kernel#select isn't called
58
+ def select(*args)
75
59
  end
76
60
  end
77
61
  end
@@ -1,3 +1,3 @@
1
1
  module Conformity
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/conformity.rb CHANGED
@@ -8,7 +8,7 @@ rescue Gem::LoadError
8
8
  poltergeist = false
9
9
  end
10
10
 
11
- require 'conformity/action'
11
+ require 'conformity/actions'
12
12
  require 'conformity/form'
13
13
  require 'conformity/field'
14
14
  require 'conformity/success_conditions'
@@ -18,6 +18,19 @@ module Conformity
18
18
  class ActionError < ConformityError; end
19
19
  class FieldError < ConformityError; end
20
20
  class FillError < ConformityError; end
21
+
22
+ class << self
23
+ attr_writer :log_screenshots
24
+ attr_accessor :screenshots_folder
25
+
26
+ def configure
27
+ yield self
28
+ end
29
+
30
+ def log_screenshots
31
+ @log_screenshots ||= false
32
+ end
33
+ end
21
34
  end
22
35
 
23
36
  Capybara.configure do |config|
@@ -0,0 +1,74 @@
1
+ require_relative 'spec_helper'
2
+ require 'capybara'
3
+ require 'conformity/actions'
4
+
5
+
6
+ include Conformity
7
+ describe Conformity::Actions do
8
+ before :each do
9
+ @field = double('field', value: 'quack')
10
+ end
11
+
12
+ describe "#field" do
13
+ it "get's the value of the field" do
14
+ actions = Actions.new
15
+
16
+ allow(Field).to receive(:value).with(:name) { "Jane Doe" }
17
+
18
+ expect(actions.field(:name)).to match("Jane Doe")
19
+ end
20
+ end
21
+
22
+ describe "#method_missing" do
23
+ it "delegates action calls to session" do
24
+ session = double
25
+ actions = Actions.new(session)
26
+
27
+ expect(session).to receive(:visit).with("/")
28
+ actions.visit('/')
29
+ end
30
+
31
+ it "ignores success conditions" do
32
+ session = double
33
+ actions = Actions.new(session)
34
+
35
+ expect { actions.has_status_code?; actions.has_content? }.to_not raise_error
36
+ end
37
+ end
38
+
39
+ describe "#select" do
40
+ it "is called on session" do
41
+ session = double
42
+ actions = Actions.new(session)
43
+
44
+ expect(session).to receive(:select)
45
+ actions.select('Yes')
46
+ end
47
+
48
+ it "is not called on Kernel" do
49
+ skip "not sure how to test"
50
+ session = double
51
+ actions = Actions.new(session)
52
+
53
+ expect(Kernel).to_not receive(:select)
54
+ actions.select('Yes')
55
+ end
56
+ end
57
+
58
+ describe "logging" do
59
+ it "calls session to save a screenshot" do
60
+ session = double
61
+ Conformity.configure do |c|
62
+ c.log_screenshots = true
63
+ c.screenshots_folder = "."
64
+ end
65
+
66
+ allow(Time).to receive(:now) { 0 }
67
+ allow(session).to receive(:visit)
68
+ expect(session).to receive(:save_screenshot).with("./0_visit.png")
69
+
70
+ actions = Actions.new(session)
71
+ actions.visit('/')
72
+ end
73
+ end
74
+ end
data/spec/field_spec.rb CHANGED
@@ -26,6 +26,19 @@ describe Conformity::Field do
26
26
  end
27
27
  end
28
28
 
29
+ describe "#value=" do
30
+ it "sets the value" do
31
+ field = Field.new(:boolean, options: ["true", "false"])
32
+ field.value = "true"
33
+ expect(field.value).to match("true")
34
+ end
35
+
36
+ it "if options are given it raises an error when trying to set a value not in options" do
37
+ field = Field.new(:field, options: ["one", "two", "three"])
38
+ expect { field.value = "four" }.to raise_error(Conformity::FieldError)
39
+ end
40
+ end
41
+
29
42
  describe "#required?" do
30
43
  it "returns true if a field is required" do
31
44
  field = Field.new(:field, required: true)
@@ -37,4 +50,23 @@ describe Conformity::Field do
37
50
  expect(field.required?).to be_falsey
38
51
  end
39
52
  end
53
+
54
+ describe ".find" do
55
+ it "returns a field with the given name" do
56
+ to_find = Field.new(:a)
57
+ Field.new(:b)
58
+ Field.new(:c)
59
+
60
+ expect(Field.find(:a)).to be(to_find)
61
+ end
62
+ end
63
+
64
+ describe ".value" do
65
+ it "returns the value for a field with name" do
66
+ field = Field.new(:field)
67
+ field.value = "blue"
68
+
69
+ expect(Field.value(:field)).to match("blue")
70
+ end
71
+ end
40
72
  end
data/spec/form_spec.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  require_relative 'spec_helper'
2
2
  require 'conformity/form'
3
+ require 'pry'
3
4
 
4
5
 
5
- describe Conformity::Form do
6
- before :each do
7
- @form = Form.new
8
- end
9
-
6
+ include Conformity
7
+ describe Form do
8
+ before :each do
9
+ @form = Form.new { } # no actions for test
10
+ end
10
11
  describe '#initialize' do
11
- it 'can initialize with a block' do
12
+ it "initializes with a block" do
12
13
  expect do
13
14
  Form.new do
14
15
  visit '/page'
@@ -22,6 +23,10 @@ describe Conformity::Form do
22
23
  end
23
24
  end.to_not raise_error
24
25
  end
26
+
27
+ it "raises an error when not given a block" do
28
+ expect { Form.new }.to raise_error
29
+ end
25
30
  end
26
31
 
27
32
  describe '#field' do
@@ -56,8 +61,8 @@ describe Conformity::Form do
56
61
  end
57
62
 
58
63
  describe '#fill' do
59
- it 'runs the actions' do
60
- skip "didn't write test yet!"
64
+ it "delegates to actions" do
65
+ skip "test not implemented"
61
66
  end
62
67
 
63
68
  it 'raises FillError on failure' do
@@ -70,35 +75,9 @@ describe Conformity::Form do
70
75
  end
71
76
  end
72
77
 
73
- describe '#method_missing' do
74
- it 'does not execute actions immediately' do
75
- @form.fill_in "#some_id", with: @form.field(:name)
76
- expect(Capybara::DSL).to_not receive(:fill_in)
77
- end
78
-
79
- describe 'field types' do
80
- it 'sets the field type for fill_in' do
81
- field = double
82
- expect(field).to receive(:type=).with(:text)
83
-
84
- @form.fill_in :name, with: field
85
- end
86
-
87
- it 'sets the field type for choose' do
88
- field = double
89
- expect(field).to receive(:type=).with(:radio)
90
-
91
- @form.choose field
92
- end
93
-
94
- # this tests fails and I don't know why! (rspec's fault not mine.. I think)
95
- it 'sets the field type for select' do
96
- skip "RSpec's fault"
97
- field = double
98
- expect(field).to receive(:type=).with(:select)
99
-
100
- @form.select field
101
- end
78
+ describe "#method_missing" do
79
+ it "ignores actions" do
80
+ expect { @form.fill_in "Name", with: @form.field(:name) }.to_not raise_error
102
81
  end
103
82
  end
104
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conformity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikias Kalpaxis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-08 00:00:00.000000000 Z
11
+ date: 2014-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -66,12 +66,12 @@ files:
66
66
  - Rakefile
67
67
  - conformity.gemspec
68
68
  - lib/conformity.rb
69
- - lib/conformity/action.rb
69
+ - lib/conformity/actions.rb
70
70
  - lib/conformity/field.rb
71
71
  - lib/conformity/form.rb
72
72
  - lib/conformity/success_conditions.rb
73
73
  - lib/conformity/version.rb
74
- - spec/action_spec.rb
74
+ - spec/actions_spec.rb
75
75
  - spec/field_spec.rb
76
76
  - spec/form_spec.rb
77
77
  - spec/spec_helper.rb
@@ -101,7 +101,7 @@ signing_key:
101
101
  specification_version: 4
102
102
  summary: Automates form submissions by building upon Capybara.
103
103
  test_files:
104
- - spec/action_spec.rb
104
+ - spec/actions_spec.rb
105
105
  - spec/field_spec.rb
106
106
  - spec/form_spec.rb
107
107
  - spec/spec_helper.rb
@@ -1,37 +0,0 @@
1
- module Conformity
2
- class Action
3
- include Capybara::DSL
4
-
5
- attr_reader :name, :args, :block
6
-
7
- def initialize(name, args = [], block = nil)
8
- @name = name
9
- @args = args
10
- @block = block
11
- end
12
-
13
- def field
14
- @field ||= case name
15
- when :fill_in then args.last[:with]
16
- when :choose then args.first
17
- when :select then args.first
18
- end
19
- end
20
-
21
- def run
22
- case name
23
- when :fill_in
24
- return unless field.value
25
- args.last[:with] = field.value
26
- when :choose, :select
27
- return unless field.value
28
- args.first = field.value
29
- end if field
30
-
31
- result = send(name, *args, &block)
32
- if !result && result['status'] == 'fail'
33
- raise ActionError, "Capybara status: #{result}"
34
- end
35
- end
36
- end
37
- end
data/spec/action_spec.rb DELETED
@@ -1,39 +0,0 @@
1
- require_relative 'spec_helper'
2
- require 'conformity/action'
3
-
4
-
5
- describe Conformity::Action do
6
- before :each do
7
- @field = double('field', value: 'quack')
8
- end
9
-
10
- describe '#field' do
11
- it 'locates the field duck type for fill_in' do
12
- action = Action.new(:fill_in, ['#name', with: @field])
13
-
14
- expect(action.field).to respond_to(:value)
15
- end
16
-
17
- it 'locates the field duck type for choose' do
18
- action = Action.new(:choose, [@field])
19
-
20
- expect(action.field).to respond_to(:value)
21
- end
22
-
23
- it 'locates the field duck type for select' do
24
- action = Action.new(:select, [@field])
25
-
26
- expect(action.field).to respond_to(:value)
27
- end
28
- end
29
-
30
- describe '#run' do
31
- it 'delegates to Capybara::DSL' do
32
- skip
33
- action = Action.new(:click_on, ['#submit'])
34
-
35
- expect(Capybara::DSL).to receive(:check).with('#submit')
36
- action.run
37
- end
38
- end
39
- end