page_magic 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'page magic' do
4
+
5
+ describe 'class level' do
6
+ context 'session' do
7
+ it 'should setup a session using the specified browser' do
8
+ Capybara::Session.should_receive(:new).with(:chrome, nil).and_return(:chrome_session)
9
+
10
+ session = PageMagic.session(:chrome)
11
+ Capybara.drivers[:chrome].call(nil).should == Capybara::Selenium::Driver.new(nil, browser: :chrome)
12
+
13
+ session.browser.should == :chrome_session
14
+ end
15
+
16
+ it 'should use the Capybara default browser if non is specified' do
17
+ Capybara.default_driver = :rack_test
18
+ session = PageMagic.session
19
+ session.browser.mode.should == :rack_test
20
+ end
21
+ end
22
+ end
23
+
24
+ describe 'instances' do
25
+
26
+ include_context :webapp
27
+
28
+ let(:my_page_class) do
29
+ Class.new do
30
+ include PageMagic
31
+ url '/page1'
32
+ link(:next, :text => "next page")
33
+ end
34
+ end
35
+
36
+ let(:another_page_class) do
37
+ Class.new do
38
+ include PageMagic
39
+ url '/another_page1'
40
+ end
41
+ end
42
+
43
+ before :each do
44
+ @page = my_page_class.new
45
+ end
46
+
47
+
48
+ describe 'browser integration' do
49
+ it "should use capybara's default session if a one is not supplied" do
50
+ Capybara.default_driver = :rack_test
51
+ my_page_class.new.browser.mode.should == :rack_test
52
+ end
53
+ end
54
+
55
+
56
+ describe 'parent pages' do
57
+ before :all do
58
+ module ParentPage
59
+ include PageMagic
60
+ link(:next, :text => "next page")
61
+ end
62
+
63
+ class ChildPage
64
+ include ParentPage
65
+ url '/page1'
66
+ end
67
+ end
68
+
69
+ context 'children' do
70
+ it 'override parents url' do
71
+ ChildPage.url.should == '/page1'
72
+ end
73
+
74
+ it 'inherit elements' do
75
+ child_page = ChildPage.new
76
+ child_page.visit
77
+ child_page.element_definitions.should include(:next)
78
+ end
79
+
80
+ it 'are added to PageMagic.pages list' do
81
+ PageMagic.pages.find_all { |page| page == ChildPage }.size.should == 1
82
+ end
83
+ end
84
+
85
+ context 'parent' do
86
+ it 'is not registered as Page' do
87
+ PageMagic.pages.should_not include(ParentPage)
88
+ end
89
+
90
+ it 'cannot be instantiated' do
91
+ expect { ParentPage.new }.to raise_error("You can only instantiate child pages")
92
+ end
93
+ end
94
+ end
95
+
96
+ describe 'visit' do
97
+ it 'should go to the page' do
98
+ @page.visit
99
+ @page.current_path.should == '/page1'
100
+ end
101
+ end
102
+
103
+ describe 'text_on_page' do
104
+ it 'should return true' do
105
+ @page.visit
106
+ @page.text_on_page?('next page').should be_true
107
+ end
108
+
109
+ it 'should return false' do
110
+ @page.visit
111
+ @page.text_on_page?('billy bob').should be_false
112
+ end
113
+
114
+ end
115
+
116
+
117
+ it 'can have fields' do
118
+ @page.element_definitions[:next].call(@page).should == PageMagic::PageElement.new(:next, @page, :button, :text => "next")
119
+ end
120
+
121
+ it 'should copy fields on to element' do
122
+ new_page = my_page_class.new
123
+ @page.element_definitions[:next].call(@page).should_not equal(new_page.element_definitions[:next].call(new_page))
124
+ end
125
+
126
+ it 'gives access to the page text' do
127
+ @page.visit.text.should == 'next page'
128
+ end
129
+
130
+ it 'should access a field' do
131
+ @page.visit
132
+ @page.click_next
133
+ @page.text.should == 'page 2 content'
134
+ end
135
+
136
+ it 'are registered at class level' do
137
+ PageMagic.instance_variable_set(:@pages, nil)
138
+
139
+ page = Class.new { include PageMagic }
140
+ PageMagic.pages.should == [page]
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe PageMagic::PageSection do
4
+
5
+ include_context :webapp
6
+
7
+ before :each do
8
+ @elements_page = elements_page.new
9
+ @elements_page.visit
10
+ end
11
+
12
+ let!(:elements_page) do
13
+
14
+ Class.new do
15
+ include PageMagic
16
+ url '/elements'
17
+ section :form_by_css do
18
+ selector css: '.form'
19
+ link(:link_in_form, text: 'a in a form')
20
+ end
21
+
22
+ section :form_by_id do
23
+ selector id: 'form'
24
+ link(:link_in_form, text: 'a in a form')
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'class level' do
30
+ let(:section) do
31
+ Class.new do
32
+ extend PageMagic::PageSection
33
+ end
34
+ end
35
+
36
+ describe 'selector' do
37
+ before do
38
+ section.parent_browser_element = @elements_page.browser
39
+ end
40
+
41
+ it 'should find by id' do
42
+ section.selector id: 'form'
43
+ section.browser_element[:id].should == 'form'
44
+ end
45
+
46
+ it 'should find by css' do
47
+ section.selector css: '.form'
48
+ section.browser_element[:id].should == 'form'
49
+ end
50
+ end
51
+ end
52
+
53
+ describe 'method_missing' do
54
+ it 'should delegate to capybara' do
55
+ @elements_page.form_by_css.visible?.should be(true)
56
+ end
57
+
58
+ it 'should throw default exception if the method does not exist on the capybara object' do
59
+ expect { @elements_page.form_by_css.bobbins }.to raise_exception NoMethodError
60
+ end
61
+ end
62
+
63
+ it 'can have elements' do
64
+ @elements_page.form_by_css.link_in_form.visible?.should be_true
65
+ end
66
+
67
+
68
+ describe 'construction' do
69
+
70
+ let(:page_section_class) do
71
+ page_section_class = Class.new do
72
+ extend PageMagic::PageSection
73
+ end
74
+ page_section_class.stub(:name).and_return('PageSection')
75
+ page_section_class
76
+ end
77
+
78
+ let(:selector) { {css: '.class_name'} }
79
+
80
+ let!(:browser) { double('browser', find: :browser_element) }
81
+ let!(:parent_page_element){ double('parent_page_element', browser_element: browser)}
82
+
83
+ context 'selector' do
84
+ it 'should use the class defined selector if one is not given to the constructor' do
85
+ page_section_class.selector selector
86
+ page_section_class.new(parent_page_element).selector.should == selector
87
+ end
88
+
89
+ it 'should raise an error if a class selector is not defined and one is not given to the constructor' do
90
+ expect { page_section_class.new(parent_page_element) }.to raise_error(PageMagic::PageSection::UndefinedSelectorException)
91
+ end
92
+ end
93
+
94
+ context 'name' do
95
+ it 'should default to the name of the class if one is not supplied' do
96
+ page_section_class.selector selector
97
+ page_section_class.new(parent_page_element).name.should == :page_section
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe PageMagic::Session do
4
+
5
+ let(:page) do
6
+ Class.new do
7
+ include PageMagic
8
+ url :url
9
+
10
+ def my_method
11
+ :called
12
+ end
13
+ end
14
+ end
15
+
16
+ let(:another_page_class) do
17
+ Class.new do
18
+ include PageMagic
19
+ url '/another_page1'
20
+ end
21
+ end
22
+
23
+ let(:browser) { double('browser') }
24
+
25
+ it 'should visit the given url' do
26
+ browser.should_receive(:visit).with(page.url)
27
+ session = PageMagic::Session.new(browser).visit(page)
28
+ session.current_page.should be_a(page)
29
+ end
30
+
31
+ context 'method_missing' do
32
+ it 'should delegate to current page' do
33
+ browser.stub(:visit)
34
+ session = PageMagic::Session.new(browser).visit(page)
35
+ session.my_method.should be(:called)
36
+ end
37
+ end
38
+
39
+ context 'move_to moves the session object to another page' do
40
+ it 'can take a class' do
41
+ page_magic_session = PageMagic::Session.new(double(:browser, current_url: '/another_page1'))
42
+ page_magic_session.move_to(another_page_class)
43
+ page_magic_session.current_page.should be_a(another_page_class)
44
+ end
45
+
46
+ it 'can take the name of the class as a string' do
47
+ class ThePage
48
+ include PageMagic
49
+ url '/the_page'
50
+ end
51
+
52
+ page_magic_session = PageMagic::Session.new(double(:browser, current_url: '/the_page'))
53
+ page_magic_session.move_to("ThePage")
54
+ page_magic_session.current_page.should be_a(ThePage)
55
+ end
56
+
57
+ it 'should wait until the browser url has changed' do
58
+ mock_browser = double(:browser, current_url: 'a')
59
+ page_magic_session = PageMagic::Session.new(mock_browser)
60
+
61
+ expect { page_magic_session.move_to(another_page_class) }.to raise_error(Wait::ResultInvalid)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,45 @@
1
+ Bundler.require
2
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
3
+ require 'page_magic'
4
+ require 'helpers'
5
+
6
+ RSpec.configure do
7
+
8
+ shared_context :webapp do
9
+ require 'sinatra/base'
10
+
11
+ rack_app = Class.new(Sinatra::Base) do
12
+ get '/page1' do
13
+ "<a href='/page2'>next page</a>"
14
+ end
15
+
16
+ get '/page2' do
17
+ 'page 2 content'
18
+ end
19
+
20
+ get '/elements' do
21
+ <<-ELEMENTS
22
+ <a href='#'>a link</a>
23
+ <input type='submit' value='a button'/>
24
+
25
+ <div id='form' class="form">
26
+ <a href='#'>a in a form</a>
27
+ </form>
28
+ ELEMENTS
29
+
30
+ end
31
+ end
32
+
33
+
34
+ before :each do
35
+ Capybara.app = rack_app
36
+ end
37
+
38
+ after do
39
+ Capybara.reset!
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_magic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Leon Davis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: watir-webdriver
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: capybara
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: wait
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: watir-webdriver
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Framework for modeling and interacting with webpages which wraps capybara
95
+ email: info@lad-tech.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - README.md
100
+ files:
101
+ - .ruby-gemset
102
+ - .ruby-version
103
+ - .travis.yml
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - README.md
107
+ - Rakefile
108
+ - VERSION
109
+ - lib/page_magic.rb
110
+ - lib/page_magic/ajax_support.rb
111
+ - lib/page_magic/browser.rb
112
+ - lib/page_magic/element_context.rb
113
+ - lib/page_magic/page_element.rb
114
+ - lib/page_magic/page_elements.rb
115
+ - lib/page_magic/page_magic.rb
116
+ - lib/page_magic/page_section.rb
117
+ - lib/page_magic/session.rb
118
+ - lib/page_magic/wait.rb
119
+ - page_magic.gemspec
120
+ - spec/browser_spec.rb
121
+ - spec/element_context_spec.rb
122
+ - spec/helpers.rb
123
+ - spec/helpers/capybara.rb
124
+ - spec/member_methods_spec.rb
125
+ - spec/page_element_spec.rb
126
+ - spec/page_elements_spec.rb
127
+ - spec/page_magic_spec.rb
128
+ - spec/page_section_spec.rb
129
+ - spec/session_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/ladtech/page_magic
132
+ licenses:
133
+ - ruby
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: 4130538887016609819
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 1.8.25
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Framework for modeling and interacting with webpages
159
+ test_files: []