page_magic 1.0.0.alpha9 → 1.0.0.alpha10
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 +4 -4
- data/VERSION +1 -1
- data/lib/page_magic/session.rb +27 -3
- data/spec/page_magic/session_spec.rb +134 -86
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d169c7cf7a4d49cfe0424e561f0a52cc732ec56
|
4
|
+
data.tar.gz: c8c26faa46d799b27f985e88bbec7ae21839b227
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9c32d2a85aad55ab12396ff2fad7da5b3e8bc319eef54ba34ec27182f24564853ed110b5b450ec8aea3bdeca2a70a2a992336a0e82186a5c9d32d66fa3e478c
|
7
|
+
data.tar.gz: 002e9bc54cc7e99e4c0d05996d1afeed3a68d141264d44bcef6e1ff47231f1083cc611bc0bb3a8445cbe30d96f37ee5d766a08c79e49df356156bd85bc57b7bf
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.0.
|
1
|
+
1.0.0.alpha10
|
data/lib/page_magic/session.rb
CHANGED
@@ -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
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
10
|
+
subject { described_class.new(browser) }
|
10
11
|
|
11
|
-
|
12
|
+
let(:browser) { double('browser', current_url: 'url', visit: nil, current_path: :current_path) }
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
before do
|
35
|
+
subject.define_page_mappings another_page_class.url => another_page_class
|
36
|
+
subject.visit(page)
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
103
|
-
session.my_method.should be(:called)
|
137
|
+
|
104
138
|
end
|
105
|
-
end
|
106
139
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
118
|
-
|
119
|
-
|
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.
|
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-
|
11
|
+
date: 2015-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capybara
|