page_magic 1.0.0.alpha9 → 1.0.0.alpha10

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
  SHA1:
3
- metadata.gz: 913ffb021a5d651dd495d38676e28a3db2d0f51d
4
- data.tar.gz: b61ef053d8a6de10e24386904f5e469f5a34e72e
3
+ metadata.gz: 6d169c7cf7a4d49cfe0424e561f0a52cc732ec56
4
+ data.tar.gz: c8c26faa46d799b27f985e88bbec7ae21839b227
5
5
  SHA512:
6
- metadata.gz: ae0c743e1683f12841c77ba81d53ebe985faedbf0cd71d9560abe898a6b7f5473feb4ac256c58e8d1f2b3998843a32b7bc3665cc414e88f9502f584c832d86ef
7
- data.tar.gz: 48d8d12ad1dbb5008987e0fca4a78b897c84001826a72069ecf605b3412cb4c89c809473e0ad77168757ca236ef2ebbbdca967449b6319b42598f680d73be08f
6
+ metadata.gz: f9c32d2a85aad55ab12396ff2fad7da5b3e8bc319eef54ba34ec27182f24564853ed110b5b450ec8aea3bdeca2a70a2a992336a0e82186a5c9d32d66fa3e478c
7
+ data.tar.gz: 002e9bc54cc7e99e4c0d05996d1afeed3a68d141264d44bcef6e1ff47231f1083cc611bc0bb3a8445cbe30d96f37ee5d766a08c79e49df356156bd85bc57b7bf
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.alpha9
1
+ 1.0.0.alpha10
@@ -1,16 +1,23 @@
1
1
  require 'wait'
2
2
  module PageMagic
3
+ class InvalidURLException < Exception;end
4
+
3
5
  class Session
6
+ URL_MISSING_MSG = 'a url must be specified as either a parameter or on the page class'
7
+ REGEXP_MAPPING_MSG = 'URL could not be derived because mapping is a Regexp'
8
+
4
9
  attr_accessor :current_page, :raw_session, :transitions
5
10
 
6
11
  def initialize(browser)
7
12
  @raw_session = browser
13
+ @transitions = {}
8
14
  end
9
15
 
10
16
  def define_page_mappings(transitions)
11
17
  @transitions = transitions
12
18
  end
13
19
 
20
+
14
21
  def current_page
15
22
  if transitions
16
23
  mapping = find_mapped_page(current_path)
@@ -26,12 +33,29 @@ module PageMagic
26
33
  transitions[mapping]
27
34
  end
28
35
 
29
- def visit(page, url: nil)
30
- raw_session.visit url || page.url
31
- @current_page = page.new self
36
+
37
+
38
+ def visit(page_or_url, url:nil)
39
+ if page_or_url.is_a?(String)
40
+ raw_session.visit page_or_url
41
+ elsif page_or_url.ancestors.include?(PageMagic)
42
+ page = page_or_url
43
+ if url
44
+ raw_session.visit(url)
45
+ elsif page.url
46
+ raw_session.visit(page.url)
47
+ elsif path = transitions.key(page)
48
+ raise InvalidURLException, REGEXP_MAPPING_MSG if path.is_a?(Regexp)
49
+ raw_session.visit("#{current_url}#{path}")
50
+ else
51
+ raise InvalidURLException, URL_MISSING_MSG
52
+ end
53
+ @current_page = page.new self
54
+ end
32
55
  self
33
56
  end
34
57
 
58
+
35
59
  def current_path
36
60
  raw_session.current_path
37
61
  end
@@ -1,124 +1,172 @@
1
- describe PageMagic::Session do
2
- let(:page) do
3
- Class.new do
4
- include PageMagic
5
- url '/page1'
1
+ module PageMagic
2
+ describe Session do
3
+ let(:page) do
4
+ Class.new do
5
+ include PageMagic
6
+ url '/page1'
7
+ end
6
8
  end
7
- end
8
9
 
9
- subject { PageMagic::Session.new(browser) }
10
+ subject { described_class.new(browser) }
10
11
 
11
- let(:browser) { double('browser', current_url: 'url', visit: nil, current_path: :current_path) }
12
+ let(:browser) { double('browser', current_url: 'url', visit: nil, current_path: :current_path) }
12
13
 
13
- describe '#current_url' do
14
- it "returns the browser's current url" do
15
- expect(subject.current_url).to eq(browser.current_url)
14
+ describe '#current_url' do
15
+ it "returns the browser's current url" do
16
+ expect(subject.current_url).to eq(browser.current_url)
17
+ end
16
18
  end
17
- end
18
19
 
19
- describe '#current_path' do
20
- it "returns the browser's current path" do
21
- expect(subject.current_path).to eq(browser.current_path)
20
+ describe '#current_path' do
21
+ it "returns the browser's current path" do
22
+ expect(subject.current_path).to eq(browser.current_path)
23
+ end
22
24
  end
23
- end
24
25
 
25
- describe '#current_page' do
26
- let(:another_page_class) do
27
- Class.new do
28
- include PageMagic
29
- url '/another_page1'
26
+ describe '#current_page' do
27
+ let(:another_page_class) do
28
+ Class.new do
29
+ include PageMagic
30
+ url '/another_page1'
31
+ end
30
32
  end
31
- end
32
33
 
33
- before do
34
- subject.define_page_mappings another_page_class.url => another_page_class
35
- subject.visit(page)
36
- end
34
+ before do
35
+ subject.define_page_mappings another_page_class.url => another_page_class
36
+ subject.visit(page)
37
+ end
37
38
 
38
- context 'page url has not changed' do
39
- it 'returns the original page' do
40
- allow(browser).to receive(:current_path).and_return(page.url)
41
- expect(subject.current_page).to be_an_instance_of(page)
39
+ context 'page url has not changed' do
40
+ it 'returns the original page' do
41
+ allow(browser).to receive(:current_path).and_return(page.url)
42
+ expect(subject.current_page).to be_an_instance_of(page)
43
+ end
42
44
  end
43
- end
44
45
 
45
- context 'page url has changed' do
46
- it 'returns the mapped page object' do
47
- allow(browser).to receive(:current_path).and_return(another_page_class.url)
48
- expect(subject.current_page).to be_an_instance_of(another_page_class)
46
+ context 'page url has changed' do
47
+ it 'returns the mapped page object' do
48
+ allow(browser).to receive(:current_path).and_return(another_page_class.url)
49
+ expect(subject.current_page).to be_an_instance_of(another_page_class)
50
+ end
49
51
  end
50
52
  end
51
- end
52
53
 
53
- describe '#find_mapped_page' do
54
- subject do
55
- described_class.new(nil).tap do |session|
56
- session.define_page_mappings '/page' => :mapped_page_using_string, /page\d/ => :mapped_page_using_regex
54
+ describe '#find_mapped_page' do
55
+ subject do
56
+ described_class.new(nil).tap do |session|
57
+ session.define_page_mappings '/page' => :mapped_page_using_string, /page\d/ => :mapped_page_using_regex
58
+ end
57
59
  end
58
- end
59
60
 
60
- context 'mapping is string' do
61
- it 'returns the page class' do
62
- expect(subject.find_mapped_page('/page')).to be(:mapped_page_using_string)
61
+ context 'mapping is string' do
62
+ it 'returns the page class' do
63
+ expect(subject.find_mapped_page('/page')).to be(:mapped_page_using_string)
64
+ end
63
65
  end
64
- end
65
- context 'mapping is regex' do
66
- it 'returns the page class' do
67
- expect(subject.find_mapped_page('/page2')).to be(:mapped_page_using_regex)
66
+ context 'mapping is regex' do
67
+ it 'returns the page class' do
68
+ expect(subject.find_mapped_page('/page2')).to be(:mapped_page_using_regex)
69
+ end
68
70
  end
69
- end
70
71
 
71
- context 'mapping is not found' do
72
- it 'returns nil' do
73
- expect(subject.find_mapped_page('/fake_page')).to be(nil)
72
+ context 'mapping is not found' do
73
+ it 'returns nil' do
74
+ expect(subject.find_mapped_page('/fake_page')).to be(nil)
75
+ end
74
76
  end
75
77
  end
76
- end
77
78
 
78
- describe '#visit' do
79
- context 'url supplied' do
80
- it 'uses this url instead of the one defined on the page class' do
81
- expect(browser).to receive(:visit).with(:custom_url)
82
- session = PageMagic::Session.new(browser).visit(page, url: :custom_url)
83
- expect(session.current_page).to be_a(page)
79
+ describe '#visit' do
80
+
81
+ let(:session) do
82
+ PageMagic::Session.new(browser)
84
83
  end
85
- end
86
84
 
87
- it 'visits the url on defined on the page class' do
88
- browser.should_receive(:visit).with(page.url)
89
- session = PageMagic::Session.new(browser).visit(page)
90
- expect(session.current_page).to be_a(page)
91
- end
92
- end
85
+ context 'page supplied' do
86
+ context 'url supplied' do
87
+ it 'uses this url instead of the one defined on the page class' do
88
+ expect(browser).to receive(:visit).with(:custom_url)
89
+ session.visit(page, url: :custom_url)
90
+ expect(session.current_page).to be_a(page)
91
+ end
92
+ end
93
+
94
+ context 'url defined on page_class' do
95
+ it 'visits the url on defined on the page class' do
96
+ browser.should_receive(:visit).with(page.url)
97
+ session.visit(page)
98
+ expect(session.current_page).to be_a(page)
99
+ end
100
+ end
101
+
102
+ context 'url not specified' do
103
+ context 'url not specified on page class' do
104
+ before do
105
+ page.instance_variable_set(:@url, nil)
106
+ end
107
+ it 'uses the current url and the path in the page mappings' do
108
+ session.define_page_mappings '/page' => page
109
+ expect(browser).to receive(:visit).with("#{browser.current_url}/page")
110
+ session.visit(page)
111
+ end
112
+
113
+ context 'no mappings found' do
114
+ it 'raises an error' do
115
+ expect { session.visit(page) }.to raise_exception InvalidURLException, described_class::URL_MISSING_MSG
116
+ end
117
+ end
118
+
119
+ context 'mapping is a regular expression' do
120
+ it 'raises an error' do
121
+ session.define_page_mappings %r{mapping} => page
122
+ expect { session.visit(page) }.to raise_exception InvalidURLException, described_class::REGEXP_MAPPING_MSG
123
+ end
124
+ end
125
+ end
126
+ end
93
127
 
94
- context '#method_missing' do
95
- it 'should delegate to current page' do
96
- page.class_eval do
97
- def my_method
98
- :called
128
+ context 'url supplied' do
129
+ it 'visits that url' do
130
+ expected_url = 'http://url.com'
131
+ expect(browser).to receive(:visit).with(expected_url)
132
+ session.visit(expected_url)
133
+ end
99
134
  end
100
135
  end
101
136
 
102
- session = PageMagic::Session.new(browser).visit(page)
103
- session.my_method.should be(:called)
137
+
104
138
  end
105
- end
106
139
 
107
- context '#respond_to?' do
108
- subject do
109
- PageMagic::Session.new(browser).tap do |s|
110
- s.current_page = page.new
140
+ context '#method_missing' do
141
+ it 'should delegate to current page' do
142
+ page.class_eval do
143
+ def my_method
144
+ :called
145
+ end
146
+ end
147
+
148
+ session = PageMagic::Session.new(browser).visit(page)
149
+ session.my_method.should be(:called)
111
150
  end
112
151
  end
113
- it 'checks self' do
114
- expect(subject.respond_to?(:current_url)).to eq(true)
115
- end
116
152
 
117
- it 'checks the current page' do
118
- page.class_eval do
119
- def my_method; end
153
+ context '#respond_to?' do
154
+ subject do
155
+ PageMagic::Session.new(browser).tap do |s|
156
+ s.current_page = page.new
157
+ end
158
+ end
159
+ it 'checks self' do
160
+ expect(subject.respond_to?(:current_url)).to eq(true)
161
+ end
162
+
163
+ it 'checks the current page' do
164
+ page.class_eval do
165
+ def my_method;
166
+ end
167
+ end
168
+ expect(subject.respond_to?(:my_method)).to eq(true)
120
169
  end
121
- expect(subject.respond_to?(:my_method)).to eq(true)
122
170
  end
123
171
  end
124
- end
172
+ end
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: 1.0.0.alpha9
4
+ version: 1.0.0.alpha10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Davis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-28 00:00:00.000000000 Z
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara