page_magic 2.0.0 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92b6df866c2bbcae9351e24417bf141c50101f96b74ce6a1225318998c30da9b
4
- data.tar.gz: 2a605e1c74eda006717474066d6824806a20f35ca05ddeef75d684951ebea257
3
+ metadata.gz: 7cd51a69161a6e25593df1ee746057e38a23dbd5debcc5d22ea66cd0376df4c5
4
+ data.tar.gz: aea97edeac9689e324e7077fcd208091b5704bd2efb2cc6d62b9980d2f9f15cd
5
5
  SHA512:
6
- metadata.gz: 773b643dcf348bd77718e53e924754dd47d23e2edc1bbc1c904a82d332a66b00f9d7285c409a91be37d7ecca27cdbb11f62d6b938ea47e7a9396a2dd97a724d3
7
- data.tar.gz: 7864c97e2e3b196809835ad8a9d4ede8fd44a7ad84276a3bd06d72fb706b2400437ded32301f5e5bb299d13cbfbe2314d606c57fc1de809dfa396e3d8070de85
6
+ metadata.gz: 47914506ca6a1eaa24bacb0c83a0a8ff750bdf2a7a11bc5d3822218f75e9da0bca9a949b263d6beead2c549c740392ea431ff53bc1cc14cbae1b84b1426f8fea
7
+ data.tar.gz: 2a4c5ae39523e8835321d53ab463ca9d20fe9fe3ec0ddf0b30fb22fbf11d201f2be0e7642c337c4d4f4e9fd19a7cedc64a89d45bbee363296a05b9ead5e0bb51
data/Gemfile CHANGED
@@ -8,9 +8,9 @@ gem 'capybara', '>=3.0'
8
8
  group :test do
9
9
  gem 'poltergeist'
10
10
  gem 'rspec', require: 'rspec/core/rake_task'
11
+ gem 'selenium-webdriver'
11
12
  gem 'simplecov', require: false
12
13
  gem 'sinatra'
13
- gem 'watir'
14
14
  end
15
15
 
16
16
  group :development do
data/Gemfile.lock CHANGED
@@ -144,9 +144,6 @@ GEM
144
144
  tzinfo (2.0.4)
145
145
  concurrent-ruby (~> 1.0)
146
146
  unicode-display_width (2.0.0)
147
- watir (6.19.1)
148
- regexp_parser (>= 1.2, < 3)
149
- selenium-webdriver (>= 3.142.7)
150
147
  websocket-driver (0.7.3)
151
148
  websocket-extensions (>= 0.1.0)
152
149
  websocket-extensions (0.1.5)
@@ -169,9 +166,9 @@ DEPENDENCIES
169
166
  rubocop
170
167
  rubocop-rspec
171
168
  ruby-debug-ide
169
+ selenium-webdriver
172
170
  simplecov
173
171
  sinatra
174
- watir
175
172
  yard (~> 0.8)
176
173
 
177
174
  BUNDLED WITH
data/README.md CHANGED
@@ -56,6 +56,8 @@ Check it out :)
56
56
  - [Helper Methods](#helper-methods)
57
57
  - [Dynamic Selectors](#dynamic-selectors)
58
58
  - [Starting a session](#starting-a-session)
59
+ - [Using an existing session](#using-an-existing-session)
60
+ - [Rack applications and Rack::Test](#rack-applications-and-racktest)
59
61
  - [Page mapping](#page-mapping)
60
62
  - [Mapping against query string parameters](#mapping-against-query-string-parameters)
61
63
  - [Mapping against fragment identifiers](#mapping-against-fragment-identifiers)
@@ -325,9 +327,16 @@ session = PageMagic.session(browser: :chrome, url: 'https://www.github.com')
325
327
  ```
326
328
 
327
329
  Your session won't do much besides navigating to the given url until you have [mapped pages](#page-mapping) to it, so
328
- take a look at this next!
330
+ take a look at this next!
329
331
 
330
332
  **Note** PageMagic supports having multiple sessions using different browsers at the same time :)
333
+
334
+ ## Using an existing session
335
+ If you are introducing PageMagic in to a test suite that already makes use of Capybara, PageMagic can be configured to
336
+ make use of the session that is already configure like this:
337
+ ```ruby
338
+ session = PageMagic.session(session: Capybara.current_session)
339
+ ```
331
340
 
332
341
  ## Rack applications and Rack::Test
333
342
  To run a session against a rack application instead of a live site, simply supply the rack application when creating
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.0.5
data/lib/page_magic.rb CHANGED
@@ -50,16 +50,21 @@ module PageMagic
50
50
  # @param [Symbol] browser name of browser
51
51
  # @param [String] url url to start the session on
52
52
  # @param [Hash] options browser driver specific options
53
+ # @param [Capybara::Session] session - use you own already configure capybara session e.g. Capybara.current_session
53
54
  # @return [Session] configured session
54
- def session(url: nil, application: nil, browser: :rack_test, options: {})
55
- driver = drivers.find(browser)
56
- raise UnsupportedBrowserException unless driver
55
+ def session(session: nil, url: nil, application: nil, browser: :rack_test, options: {})
56
+ session ||= begin
57
+ driver = drivers.find(browser)
58
+ raise UnsupportedBrowserException unless driver
57
59
 
58
- Capybara.register_driver browser do |app|
59
- driver.build(app, browser: browser, options: options)
60
+ Capybara.register_driver browser do |app|
61
+ driver.build(app, browser: browser, options: options)
62
+ end
63
+
64
+ Capybara::Session.new(browser, application)
60
65
  end
61
66
 
62
- Session.new(Capybara::Session.new(browser, application), url)
67
+ Session.new(session, url)
63
68
  end
64
69
  end
65
70
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  PageMagic::Drivers::Selenium = PageMagic::Driver.new(:chrome, :firefox) do |app, options, browser|
4
- require 'watir'
4
+ require 'capybara/selenium/driver'
5
5
  Capybara::Selenium::Driver.new(app, **options.dup.merge(browser: browser))
6
6
  end
@@ -73,7 +73,9 @@ module PageMagic
73
73
  # Selector built using supplied configuration
74
74
  # @return [PageMagic::Element::Selector::Model]
75
75
  def selector
76
- selector = self[:selector]
76
+ selector = self[:selector] || definition_class.selector
77
+ raise PageMagic::InvalidConfigurationException, INVALID_SELECTOR_MSG unless validate_selector?(selector)
78
+
77
79
  Element::Selector.find(selector.keys.first).build(type, selector.values.first, options: options)
78
80
  end
79
81
 
@@ -81,7 +83,6 @@ module PageMagic
81
83
  # @raise [PageMagic::InvalidConfigurationException]
82
84
  # @return [PageMagic::Elements::Config]
83
85
  def validate!
84
- raise PageMagic::InvalidConfigurationException, INVALID_SELECTOR_MSG unless element || valid_selector?
85
86
  raise PageMagic::InvalidConfigurationException, 'element type required' unless type
86
87
  raise PageMagic::InvalidConfigurationException, INVALID_ELEMENT_CLASS_MSG unless valid_element_class?
87
88
 
@@ -90,8 +91,7 @@ module PageMagic
90
91
 
91
92
  private
92
93
 
93
- def valid_selector?
94
- selector = self[:selector]
94
+ def validate_selector?(selector)
95
95
  selector.is_a?(Hash) && !selector.empty?
96
96
  end
97
97
 
@@ -3,7 +3,6 @@
3
3
  require_relative '../active_support/core_ext/object/to_query'
4
4
  require_relative 'comparator'
5
5
 
6
-
7
6
  module PageMagic
8
7
  # models mapping used to relate pages to uris
9
8
  class Mapping
@@ -18,6 +18,8 @@ module PageMagic
18
18
  # Create a new session instance
19
19
  # @param [Object] capybara_session an instance of a capybara session
20
20
  # @param [String] base_url url to start the session at.
21
+ #
22
+
21
23
  def initialize(capybara_session, base_url = nil)
22
24
  @raw_session = capybara_session
23
25
  @base_url = base_url
@@ -32,16 +34,6 @@ module PageMagic
32
34
  @current_page
33
35
  end
34
36
 
35
- # @return [String] path in the browser
36
- def current_path
37
- raw_session.current_path
38
- end
39
-
40
- # @return [String] full url in the browser
41
- def current_url
42
- raw_session.current_url
43
- end
44
-
45
37
  # Map paths to Page classes. The session will auto load page objects from these mapping when
46
38
  # the {Session#current_path}
47
39
  # is matched.
@@ -54,26 +46,28 @@ module PageMagic
54
46
  @transitions = Transitions.new(transitions)
55
47
  end
56
48
 
57
- # execute javascript on the browser
58
- # @param [String] script the script to be executed
59
- # @return [Object] object returned by the capybara execute_script method
60
- # @raise [NotSupportedException] if the capybara driver in use does not support execute script
61
- def execute_script(script)
62
- raw_session.execute_script(script)
63
- rescue Capybara::NotSupportedByDriverError
64
- raise NotSupportedException, UNSUPPORTED_OPERATION_MSG
49
+ def is_a?(klass)
50
+ return true if klass == Capybara::Session
51
+
52
+ super
65
53
  end
66
54
 
67
55
  # proxies unknown method calls to the currently loaded page object
68
56
  # @return [Object] returned object from the page object method call
69
57
  def method_missing(name, *args, &block)
58
+ return raw_session.send(name, *args, &block) if raw_session.respond_to?(name)
59
+
70
60
  current_page.send(name, *args, &block)
71
61
  end
72
62
 
63
+ def on?(page_class)
64
+ current_page.is_a?(page_class)
65
+ end
66
+
73
67
  # @param args see {::Object#respond_to?}
74
68
  # @return [Boolean] true if self or the current page object responds to the give method name
75
69
  def respond_to_missing?(*args)
76
- super || current_page.respond_to?(*args)
70
+ super || current_page.respond_to?(*args) || raw_session.respond_to?(*args)
77
71
  end
78
72
 
79
73
  # Direct the browser to the given page or url. {Session#current_page} will be set be an instance of the given/mapped
@@ -89,13 +83,14 @@ module PageMagic
89
83
  # @raise [InvalidURLException] if a page is supplied and there isn't a mapped path for it
90
84
  # @raise [InvalidURLException] if neither a page or url are supplied
91
85
  # @raise [InvalidURLException] if the mapped path for a page is a Regexp
92
- def visit(page = nil, url: nil)
93
- url ||= transitions.url_for(page, base_url: base_url)
86
+ # @return [PageMagic::Session] - returns self
87
+ def visit(page_or_url = nil, url: nil)
88
+ url ||= page_or_url.is_a?(String) ? page_or_url : transitions.url_for(page_or_url, base_url: base_url)
94
89
 
95
90
  raise InvalidURLException, URL_MISSING_MSG unless url
96
91
 
97
92
  raw_session.visit(url)
98
- @current_page = initialize_page(page) if page
93
+ @current_page = initialize_page(page_or_url) unless page_or_url.is_a?(String)
99
94
  self
100
95
  end
101
96
 
@@ -59,6 +59,24 @@ RSpec.describe PageMagic::Elements::Config do
59
59
  expect(options.selector).to eq(PageMagic::Element::Selector.find(:id).build(:field, 'child'))
60
60
  end
61
61
 
62
+ context 'when is empty hash' do
63
+ it 'raises an error' do
64
+ options = described_class.build([{}], :field)
65
+ expect { options.selector }.to raise_exception(PageMagic::InvalidConfigurationException,
66
+ described_class::INVALID_SELECTOR_MSG)
67
+ end
68
+ end
69
+
70
+ context 'when defined on both class and as parameter' do
71
+ it 'uses the supplied selector' do
72
+ options = described_class.build([{ css: 'supplied_selector' }], :field)
73
+ options.definition_class = Class.new(PageMagic::Element) do
74
+ selector css: 'class_selector'
75
+ end
76
+ expect(options.selector).to eq(PageMagic::Element::Selector.find(:css).build(:field, 'supplied_selector'))
77
+ end
78
+ end
79
+
62
80
  context 'when page_element class supplied' do
63
81
  it 'uses the selector on the class' do
64
82
  expected_selector = { css: 'class' }
@@ -91,6 +109,29 @@ RSpec.describe PageMagic::Elements::Config do
91
109
  end
92
110
  end
93
111
  end
112
+
113
+ context 'when no selector on class or page_element' do
114
+ context 'when dynamically assigned element definition block' do
115
+ it 'uses it' do
116
+ options = described_class.build([], :field)
117
+
118
+ options.definition_class = Class.new(PageMagic::Element) do
119
+ selector({ css: 'class' })
120
+ end
121
+
122
+ expect(options.selector).to eq(PageMagic::Element::Selector.find(:css).build(:field, 'class'))
123
+ end
124
+ end
125
+
126
+ context 'when not dynamically assigned' do
127
+ it 'raises and exception' do
128
+ options = described_class.build([], :field)
129
+ options.definition_class = Class.new(PageMagic::Element)
130
+ expect { options.selector }.to raise_exception(PageMagic::InvalidConfigurationException,
131
+ described_class::INVALID_SELECTOR_MSG)
132
+ end
133
+ end
134
+ end
94
135
  end
95
136
 
96
137
  it 'sets prefetched options' do
@@ -119,44 +160,6 @@ RSpec.describe PageMagic::Elements::Config do
119
160
  }
120
161
  end
121
162
 
122
- describe 'selector' do
123
- context 'when nil' do
124
- context 'and prefetched `element` is nil' do
125
- it 'raise an error' do
126
- subject = described_class.new(options.except(:selector, :element))
127
- expect { subject.validate! }.to raise_exception(PageMagic::InvalidConfigurationException,
128
- described_class::INVALID_SELECTOR_MSG)
129
- end
130
- end
131
-
132
- context 'when `element` is not nil' do
133
- it 'does not raise an error' do
134
- subject = described_class.new(options.except(:selector))
135
- expect { subject.validate! }.not_to raise_exception
136
- end
137
- end
138
- end
139
-
140
- context 'when is empty hash' do
141
- it 'raises an error' do
142
- subject = described_class.new(options.update(selector: {}).except(:element))
143
- expect { subject.validate! }.to raise_exception(PageMagic::InvalidConfigurationException,
144
- described_class::INVALID_SELECTOR_MSG)
145
- end
146
- end
147
-
148
- context 'when defined on both class and as parameter' do
149
- it 'uses the supplied selector' do
150
- element_class = Class.new(PageMagic::Element) do
151
- selector css: 'selector'
152
- end
153
-
154
- subject = described_class.new(options.update(element_class: element_class))
155
- expect { subject.validate! }.not_to raise_exception
156
- end
157
- end
158
- end
159
-
160
163
  context 'when type nil' do
161
164
  it 'raise an error' do
162
165
  subject = described_class.new(options.except(:type))
@@ -40,20 +40,6 @@ RSpec.describe PageMagic::Session do
40
40
  end
41
41
  end
42
42
 
43
- describe '#current_path' do
44
- it "returns the browser's current path" do
45
- browser.visit('/url')
46
- expect(session.current_path).to eq(browser.current_path)
47
- end
48
- end
49
-
50
- describe '#current_url' do
51
- it "returns the browser's current url" do
52
- browser.visit('/url')
53
- expect(session.current_url).to eq(session.current_url)
54
- end
55
- end
56
-
57
43
  describe '#define_page_mappings' do
58
44
  context 'when the mapping includes a literal' do
59
45
  it 'creates a matcher to contain the specification' do
@@ -71,22 +57,6 @@ RSpec.describe PageMagic::Session do
71
57
  end
72
58
  end
73
59
 
74
- describe '#execute_script' do
75
- it 'calls the execute script method on the capybara session' do
76
- allow(browser).to receive(:execute_script).with(:script).and_return(:result)
77
- expect(session.execute_script(:script)).to be(:result)
78
- end
79
-
80
- context 'when the Capybara session does not support #execute_script' do
81
- let(:browser) { Capybara::Driver::Base.new }
82
-
83
- it 'raises an error' do
84
- expected_message = described_class::UNSUPPORTED_OPERATION_MSG
85
- expect { session.execute_script(:script) }.to raise_error(PageMagic::NotSupportedException, expected_message)
86
- end
87
- end
88
- end
89
-
90
60
  describe '#method_missing' do
91
61
  before do
92
62
  page.class_eval do
@@ -100,6 +70,21 @@ RSpec.describe PageMagic::Session do
100
70
  session = described_class.new(browser).visit(page, url: 'http://base.url')
101
71
  expect(session.my_method).to be(:called)
102
72
  end
73
+
74
+ context 'when method not on current page' do
75
+ it 'delegates to the capybara session' do
76
+ session = described_class.new(browser).visit(page, url: 'http://base.url')
77
+ expect(session).to have_text('ok')
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#is_a?' do
83
+ context 'when other is a `Capybara::Session`' do
84
+ it 'returns true' do
85
+ expect(described_class.new(browser).is_a?(Capybara::Session)).to eq(true)
86
+ end
87
+ end
103
88
  end
104
89
 
105
90
  describe '#respond_to?' do
@@ -123,6 +108,13 @@ RSpec.describe PageMagic::Session do
123
108
  expect(session.respond_to?(:my_method)).to eq(true)
124
109
  end
125
110
  end
111
+
112
+ context 'when method not on self or the current page' do
113
+ it 'checks the capybara session' do
114
+ session = described_class.new(browser)
115
+ expect(session).to respond_to(:has_text?)
116
+ end
117
+ end
126
118
  end
127
119
 
128
120
  describe '#visit' do
@@ -154,10 +146,17 @@ RSpec.describe PageMagic::Session do
154
146
  end
155
147
  end
156
148
 
149
+ context 'when a page and `:url` supplied' do
150
+ it 'uses the url' do
151
+ session.visit(page, url: 'http://other.url/')
152
+ expect(session.current_url).to eq('http://other.url/')
153
+ end
154
+ end
155
+
157
156
  context 'when `url` is supplied' do
158
157
  it 'visits that url' do
159
158
  expected_url = 'http://base.url/page'
160
- session.visit(url: expected_url)
159
+ session.visit(expected_url)
161
160
  expect(browser.current_url).to eq(expected_url)
162
161
  end
163
162
  end
@@ -89,6 +89,13 @@ RSpec.describe PageMagic do
89
89
  end
90
90
  end
91
91
 
92
+ context 'when `session` is specified' do
93
+ it 'uses it' do
94
+ session = described_class.session(session: :custom_session)
95
+ expect(session.raw_session).to be(:custom_session)
96
+ end
97
+ end
98
+
92
99
  context 'when `:options` is specified' do
93
100
  it 'passes the options to the browser driver' do
94
101
  options = { option: :config }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: page_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Davis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-01 00:00:00.000000000 Z
11
+ date: 2021-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport