prickle 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,13 @@ To enable this feature you need to set the *Prickle::Capybara.wait_time* propert
32
32
  Prickle::Capybara.wait_time = 5
33
33
  ```
34
34
 
35
- If you only want to extend the wait time for a particular feature, then you need to reset the wait time using *Prickle::Capybara = nil*.
35
+ If you only want to extend the wait time for a particular feature, then you need to reset the wait time using *Prickle::Capybara = nil* after your call..
36
+
37
+ ```ruby
38
+ Prickle::Capybara.wait_time = 5
39
+ element(:href => "http://google.com").click
40
+ Prickle::Capybara.wait_time = nil # reset wait time
41
+ ```
36
42
 
37
43
  ## Usage
38
44
 
@@ -68,6 +74,47 @@ element(:name => "flower").contains_text? "Roses"
68
74
  element(:name => "flower").has_text? "Anemone" # exact match
69
75
  ```
70
76
 
77
+ ### Popup
78
+
79
+ **Selenium**
80
+
81
+ ```ruby
82
+ popup.confirm
83
+ popup.dismiss
84
+ popup.message
85
+
86
+ popup.contains_message? "<text>"
87
+ ```
88
+
89
+ **Webkit**
90
+
91
+ ```ruby
92
+ popup.confirm {
93
+ #block that triggers confirmation dialog
94
+ }
95
+
96
+ popup.dismiss {
97
+ #block that triggers confirmation dialog
98
+ }
99
+
100
+ popup.accept {
101
+ #block that triggers alert
102
+ }
103
+ ```
104
+
105
+ Verifying popup messageo
106
+
107
+ ```ruby
108
+
109
+ alert = popup.accept {
110
+ #block that triggers alert
111
+ }
112
+
113
+ alert.contains_message? "<text>"
114
+
115
+ ```
116
+
117
+
71
118
  ## Alternative syntax
72
119
 
73
120
  ### Find
@@ -90,16 +137,6 @@ click_input_by_name "blue" #click_<element_tag>_by_name "<name>"
90
137
  div_contains_text? "text" #<element_tag>_contains_text? "text"
91
138
  ```
92
139
 
93
- ## Popup actions
94
-
95
- ```ruby
96
- confirm_popup # can be used for both confirmation boxed and alert boxes
97
- dismiss_popup
98
- popup_message
99
-
100
- popup_message_contains? "<text>"
101
- ```
102
-
103
140
  ## Capturing screenshots
104
141
 
105
142
  Configure the directory where you want the screenshots to be saved
@@ -22,6 +22,15 @@ module Prickle
22
22
  find_element
23
23
  end
24
24
 
25
+ private
26
+
27
+ ALIASES = { "find" => :exists? }
28
+
29
+ def self.for properties
30
+ element = Element::extract_method_missing properties
31
+ method = ALIASES[element[:method]] || element[:method].to_sym
32
+ [ method, element[:args] ].compact
33
+ end
25
34
  end
26
35
  end
27
36
  end
@@ -7,6 +7,7 @@ module Prickle
7
7
  class Element
8
8
 
9
9
  include ::Capybara::DSL
10
+ include Prickle::Capybara::Actions
10
11
 
11
12
  OF_ANY_TYPE = "*"
12
13
  CONVERTED_TYPES = { :link => 'a',
@@ -15,6 +16,8 @@ module Prickle
15
16
 
16
17
  private
17
18
 
19
+ MISSING_METHOD_REGEX = /(^.*)_(contains_text\?)|(click|find)_(.*)_by_name/
20
+
18
21
  def initialize type=OF_ANY_TYPE, identifier
19
22
  @identifier = identifier
20
23
  @type = type
@@ -58,10 +61,12 @@ module Prickle
58
61
  Exceptions::ElementNotFound.new(@type, identifier, @text, caught_exception)
59
62
  end
60
63
 
61
- public
62
-
63
- include Prickle::Capybara::Actions
64
-
64
+ def self.extract_method_missing properties
65
+ element = { }
66
+ element[:method] = properties[1] || properties[2]
67
+ element[:args] = properties[4][1]
68
+ element
69
+ end
65
70
  end
66
71
  end
67
72
  end
@@ -0,0 +1,31 @@
1
+ module Prickle
2
+ module Capybara
3
+ module Popups
4
+ class Selenium
5
+
6
+ include ::Capybara::DSL
7
+
8
+ def initialize
9
+ @popup = page.driver.browser.switch_to.alert
10
+ end
11
+
12
+ def confirm
13
+ @popup.accept
14
+ end
15
+
16
+ def dismiss
17
+ @popup.dismiss
18
+ end
19
+
20
+ def message
21
+ @popup.text
22
+ end
23
+
24
+ def contains_message? message
25
+ raise Exceptions::MessageNotContainedInPopup.new(self.message) unless self.message.eql? message
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,68 @@
1
+ module Prickle
2
+ module Capybara
3
+ module Popups
4
+ class Webkit
5
+
6
+ include ::Capybara::DSL
7
+
8
+ def confirm &block
9
+ set_type_to :confirm
10
+ manage_popup true, &block
11
+ end
12
+
13
+ def dismiss &block
14
+ set_type_to :confirm
15
+ manage_popup false, &block
16
+ end
17
+
18
+ def accept &block
19
+ set_type_to :alert
20
+ manage_popup true, &block
21
+ end
22
+
23
+ def message
24
+ @message
25
+ end
26
+
27
+ def contains_message? message
28
+ raise Exceptions::MessageNotContainedInPopup.new(self.message) unless self.message.include? message
29
+ end
30
+
31
+ private
32
+
33
+ def set_type_to type
34
+ @type = type
35
+ end
36
+
37
+ def type
38
+ @type.to_s
39
+ end
40
+
41
+ def manage_popup accept
42
+ listen_and accept
43
+ yield
44
+ restore
45
+ capture_message
46
+ self
47
+ end
48
+
49
+ def listen_and accept
50
+ page.execute_script %{
51
+ window.original_#{type}_function = window.#{type}
52
+ window.#{type}_msg = null
53
+ window.#{type} = function(msg) { window.#{type}_msg = msg; return #{!!accept}; }
54
+ }
55
+ end
56
+
57
+ def restore
58
+ page.execute_script "window.#{type} = window.original_#{type}_function"
59
+ end
60
+
61
+ def capture_message
62
+ @message = page.evaluate_script "window.#{type}_msg"
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'popup/webkit'
2
+ require_relative 'popup/selenium'
3
+
4
+ module Prickle
5
+ module Capybara
6
+ class Popup
7
+
8
+ def initialize
9
+ return @base = Popups::Webkit.new if ::Capybara.current_driver == :webkit or ::Capybara.javascript_driver == :webkit
10
+ @base = Popups::Selenium.new
11
+ end
12
+
13
+
14
+ def method_missing method, *args, &block
15
+ if @base.respond_to? method
16
+ @base.send method, *args, &block
17
+ else
18
+ super
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative 'capybara/element'
2
+ require_relative 'capybara/popup'
2
3
  require_relative 'exceptions'
3
4
  require_relative 'core_ext/symbol'
4
5
 
@@ -14,13 +15,6 @@ module Prickle
14
15
  find_by_name(name).click
15
16
  end
16
17
 
17
- def confirm_popup
18
- page.driver.browser.switch_to.alert.accept
19
- end
20
-
21
- def dismiss_popup
22
- page.driver.browser.switch_to.alert.dismiss
23
- end
24
18
 
25
19
  def element type=Element::OF_ANY_TYPE, identifier
26
20
  Element.new type, identifier
@@ -30,12 +24,24 @@ module Prickle
30
24
  element(type, :name => name).exists?
31
25
  end
32
26
 
27
+ def popup
28
+ Popup.new
29
+ end
30
+
31
+ def confirm_popup
32
+ popup.confirm
33
+ end
34
+
35
+ def dismiss_popup
36
+ popup.dismiss
37
+ end
38
+
33
39
  def popup_message
34
- page.driver.browser.switch_to.alert.text
40
+ popup.message
35
41
  end
36
42
 
37
43
  def popup_message_contains? message
38
- raise Exceptions::MessageNotContainedInPopup.new(message) unless popup_message.eql? message
44
+ popup.contains_message? message
39
45
  end
40
46
 
41
47
  def capture_screen name=screenshot_name
@@ -46,20 +52,22 @@ module Prickle
46
52
 
47
53
  TIME_FORMATTER = "%Y%m%d-%H.%M.%s"
48
54
 
55
+ def screenshot_name
56
+ Time.now.strftime(TIME_FORMATTER)
57
+ end
58
+
49
59
  def method_missing method, *args
50
- if method =~ /(^.*)_contains_text\?$/
51
- element($1, :name => args.first).contains_text? args[1]
52
- elsif method =~ /^click_(.*)_by_name$/
53
- element($1, :name => args.first).click
54
- elsif method =~ /^find_(.*)_by_name$/
55
- element($1, :name => args.first).exists?
60
+ if method =~ Element::MISSING_METHOD_REGEX
61
+ call_element_with $1, $2, $3, $4, args
56
62
  else
57
63
  super
58
64
  end
59
65
  end
60
66
 
61
- def screenshot_name
62
- Time.now.strftime(TIME_FORMATTER)
67
+ def call_element_with *properties
68
+ type = properties[0] || properties[3]
69
+ name = properties[4][0]
70
+ element(type, :name => name).send *Actions::for(properties)
63
71
  end
64
72
  end
65
73
  end
@@ -1,3 +1,3 @@
1
1
  module Prickle
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
data/prickle.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency "rspec", "~> 2.8.0"
26
26
  s.add_development_dependency "sinatra", "~> 1.3.2"
27
27
  s.add_development_dependency "rake"
28
+ s.add_development_dependency "capybara-webkit"
28
29
  s.add_development_dependency "reek"
29
30
  end
30
31
 
File without changes
File without changes
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prickle::Capybara::Popup do
4
+ let(:prickly) { Prickly.new }
5
+
6
+ before do
7
+ Prickle::Capybara.wait_time = nil
8
+ end
9
+
10
+ before(:each) do
11
+ prickly.visit '/'
12
+ end
13
+
14
+ context 'Managing selenium popups', :javascript => true do
15
+
16
+ it 'can confirm an alert box' do
17
+ prickly.click_by_name 'popups'
18
+ prickly.confirm_popup
19
+ end
20
+
21
+ it 'can confirm a popup' do
22
+ prickly.click_by_name 'confirm_box'
23
+ prickly.confirm_popup
24
+ end
25
+
26
+ it 'can dismiss a popup' do
27
+ prickly.click_by_name 'confirm_box'
28
+ prickly.dismiss_popup
29
+ prickly.popup_message.should eq "Aborting."
30
+ prickly.confirm_popup
31
+ end
32
+
33
+ context "matching text" do
34
+ it "can verify the content of a popup" do
35
+ prickly.click_by_name "popups"
36
+ prickly.popup_message_contains? "Hello"
37
+ prickly.dismiss_popup
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Prickle::Capybara::Popup do
4
+ let(:prickly) { Prickly.new }
5
+
6
+ before do
7
+ Prickle::Capybara.wait_time = nil
8
+ Capybara.current_driver = :webkit
9
+ end
10
+
11
+ before(:each) do
12
+ prickly.visit '/'
13
+ end
14
+
15
+ after do
16
+ Capybara.use_default_driver
17
+ end
18
+
19
+ context 'Managing webkit popups', :js => true, :driver => :webkit do
20
+ it 'can confirm an alert box' do
21
+
22
+ alert = prickly.popup.accept {
23
+ prickly.click_by_name 'popups'
24
+ }
25
+
26
+ alert.contains_message? "Hello"
27
+ end
28
+
29
+ it 'can confirm a popup' do
30
+ alert = prickly.popup.confirm {
31
+ prickly.click_by_name 'confirm_box'
32
+ }
33
+ alert.contains_message? "Click yes to continue"
34
+ end
35
+
36
+ it 'can dismiss a popup' do
37
+ alert = prickly.popup.dismiss {
38
+ prickly.click_by_name 'confirm_box'
39
+ }
40
+
41
+ alert.contains_message? "Click yes to continue"
42
+ end
43
+
44
+ end
45
+ end
@@ -4,43 +4,11 @@ describe Prickle::Capybara do
4
4
  let(:prickly) { Prickly.new }
5
5
 
6
6
  before do
7
- Capybara.default_driver = :selenium
8
7
  Prickle::Capybara.wait_time = nil
9
- end
10
-
11
- before(:each) do
12
8
  prickly.visit '/'
13
9
  end
14
10
 
15
- context 'Managing popups', :javascript => true do
16
-
17
- it 'can confirm an alert box' do
18
- prickly.click_by_name 'popups'
19
- prickly.confirm_popup
20
- end
21
-
22
- it 'can confirm a popup' do
23
- prickly.click_by_name 'confirm_box'
24
- prickly.confirm_popup
25
- end
26
-
27
- it 'can dismiss a popup' do
28
- prickly.click_by_name 'confirm_box'
29
- prickly.dismiss_popup
30
- prickly.popup_message.should eq "Aborting."
31
- prickly.confirm_popup
32
- end
33
-
34
- context "matching text" do
35
- it "can verify the content of a popup" do
36
- prickly.click_by_name "popups"
37
- prickly.popup_message_contains? "Hello"
38
- prickly.dismiss_popup
39
- end
40
- end
41
- end
42
-
43
- context 'Screenshots', :javascript => true do
11
+ context 'Screenshots', :javascript => true, :driver => :selenium do
44
12
 
45
13
  it 'can capture the screen' do
46
14
  screenshot_name = Time.now.strftime("%Y%m%d-%H.%M.%s")
@@ -4,11 +4,10 @@ describe Prickle::Capybara do
4
4
  let(:prickly) { Prickly.new }
5
5
 
6
6
  before do
7
- Capybara.default_driver = :selenium
8
7
  prickly.visit '/'
9
8
  end
10
9
 
11
- context 'Extended waits', :javascript => true do
10
+ context 'Extended waits', :javascript => true, :driver => :selenium do
12
11
 
13
12
  before(:each) do
14
13
  Prickle::Capybara.wait_time = nil
@@ -23,7 +22,7 @@ describe Prickle::Capybara do
23
22
 
24
23
  it "can fail if an element doesn't appear after the default wait time" do
25
24
  Prickle::Capybara.wait_time = 1
26
- expect { prickly.element(:name => 'lagged').contains_text? "I lag" }.to raise_error Capybara::TimeoutError
25
+ expect { prickly.element(:name => 'never_appear').contains_text? "I lag" }.to raise_error Capybara::TimeoutError
27
26
  end
28
27
  end
29
28
 
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "lib", "prickle", "capybara")
2
2
 
3
3
  require 'rspec'
4
+ require 'capybara/rspec'
5
+ require 'capybara-webkit'
4
6
  require 'prickle/capybara'
5
7
 
6
8
  class Prickly
@@ -13,3 +15,4 @@ require_relative 'stub/app'
13
15
  Capybara.app = Sinatra::Application
14
16
  Capybara.default_wait_time = 0
15
17
  Prickle::Capybara.image_dir = File.dirname(__FILE__) + "/tmp/"
18
+ Capybara.default_driver = :selenium
data/spec/stub/index.html CHANGED
@@ -13,8 +13,9 @@
13
13
  <li name="purple">Im not purple!</li>
14
14
  <a name="orangey">Me too!!</a>
15
15
 
16
- <div name='lagged' class='hidden'> I lag</div>
17
- <div name='orangey' class='pink'> Blue hippos</div>
16
+ <div name='lagged' class='hidden' style='display: none;'> I lag</div>
17
+ <div name='never_appear' style='display: none;'> I lag</div>
18
+ <div name='orangey' class='pink'> Blue hippos</div>
18
19
 
19
20
  <p id='coffee'>Capuccino</p>
20
21
 
@@ -28,7 +29,6 @@
28
29
 
29
30
  <script>
30
31
  $(document).ready(function() {
31
- $('.hidden').hide();
32
32
  setTimeout(showHidden, 3000);
33
33
  });
34
34
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prickle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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-02-12 00:00:00.000000000 Z
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &70174028443700 !ruby/object:Gem::Requirement
16
+ requirement: &70098719150400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70174028443700
24
+ version_requirements: *70098719150400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara
27
- requirement: &70174028443100 !ruby/object:Gem::Requirement
27
+ requirement: &70098719149760 !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: :development
34
34
  prerelease: false
35
- version_requirements: *70174028443100
35
+ version_requirements: *70098719149760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70174028442480 !ruby/object:Gem::Requirement
38
+ requirement: &70098719149100 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.8.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70174028442480
46
+ version_requirements: *70098719149100
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &70174028441920 !ruby/object:Gem::Requirement
49
+ requirement: &70098719148440 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.3.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70174028441920
57
+ version_requirements: *70098719148440
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70174028441480 !ruby/object:Gem::Requirement
60
+ requirement: &70098719147940 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,21 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70174028441480
68
+ version_requirements: *70098719147940
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara-webkit
71
+ requirement: &70098719147380 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70098719147380
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: reek
71
- requirement: &70174028440740 !ruby/object:Gem::Requirement
82
+ requirement: &70098719163000 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70174028440740
90
+ version_requirements: *70098719163000
80
91
  description: A simple DSL extending Capybara.
81
92
  email: despo@extractmethod.com
82
93
  executables: []
@@ -93,6 +104,9 @@ files:
93
104
  - lib/prickle/capybara.rb
94
105
  - lib/prickle/capybara/actions.rb
95
106
  - lib/prickle/capybara/element.rb
107
+ - lib/prickle/capybara/popup.rb
108
+ - lib/prickle/capybara/popup/selenium.rb
109
+ - lib/prickle/capybara/popup/webkit.rb
96
110
  - lib/prickle/capybara/xpath.rb
97
111
  - lib/prickle/capybara/xpath/expression.rb
98
112
  - lib/prickle/core_ext/symbol.rb
@@ -101,10 +115,12 @@ files:
101
115
  - lib/prickle/version.rb
102
116
  - prickle.gemspec
103
117
  - prickle.png
118
+ - spec/capybara/actions_spec.rb
119
+ - spec/capybara/element_spec.rb
120
+ - spec/capybara/popups/selenium_spec.rb
121
+ - spec/capybara/popups/webkit_spec.rb
104
122
  - spec/capybara_spec.rb
105
- - spec/element_spec.rb
106
123
  - spec/extended_wait_spec.rb
107
- - spec/finders_spec.rb
108
124
  - spec/spec_helper.rb
109
125
  - spec/stub/app.rb
110
126
  - spec/stub/index.html