page-object-pal 0.1.0

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Johnson Denen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # PageObjectPal
2
+
3
+ PageObjectPal keeps an eye on your [page-object](https://github.com/cheezy/page-object) classes, alerting you to outdated element information. Unit test your page classes with PageObjectPal, and don't wait for your acceptance tests to fail!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'page-object-pal'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install page-object-pal
18
+
19
+ ## Usage
20
+
21
+ PageObjectPal is a self-extending module and needs only one method call. `PageObjectPal#examine` takes three arguments: a class, a filepath, and a URL. It returns ```true``` if your page objects pass, and raises a `PageObjectInvalid` error if they fail.
22
+
23
+ Call `PageObjectPal#examine` from your specs:
24
+ ```ruby
25
+ require 'page-object-pal'
26
+ describe Page do
27
+ it "should reference valid elements in its methods" do
28
+ PageObjectPal.examine(Page, '/path/to/Page', 'http://www.page.com').should be_true
29
+ end
30
+ end
31
+ ```
32
+
33
+ Or wrap `PageObjectPal#examine` in a class method to be called in your specs:
34
+ ```ruby
35
+ require 'page-object'
36
+ require 'page-object-pal'
37
+ class Page
38
+ include 'PageObject'
39
+ def self.examine
40
+ PageObjectPal.examine(self, __FILE__, 'http://www.page.com')
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Write tests for your feature
50
+ 4. Code until your tests pass (`rake`)
51
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 6. Push to the branch (`git push origin my-new-feature`)
53
+ 7. Create new Pull Request
54
+
55
+ ## Testing
56
+
57
+ I'm partial to the rspec-given syntax and use it in [my unit tests](/spec). Please do the same. If you're unfamiliar with [rspec-given](https://github.com/jimweirich/rspec-given), you should check it out!
58
+
59
+ ## Issues/Comments/Questions?
60
+ Write something up in the [Issues Tracker](https://github.com/jdenen/page-object-pal/issues). Or contact me via twitter, [@jpdenen](http://twitter.com/jpdenen)!
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ desc "Execute specs"
6
+ task :test do
7
+ sh "bundle exec rspec"
8
+ end
@@ -0,0 +1,70 @@
1
+ require "page-object-pal/version"
2
+ require "page-object-pal/elements"
3
+ require "page-object-pal/diff"
4
+ require "nokogiri"
5
+ require "open-uri"
6
+ #
7
+ # Module that will compare element identifying information used to define
8
+ # instance methods in PageObject classes to the HTML source code of a URL.
9
+ # Use to keep page objects from becoming stale/unit testing your classes.
10
+ #
11
+ # Can be used independent of class to unit test page object.
12
+ # @example Independent test
13
+ # describe Page do
14
+ # it "calls upon valid elements in its instance methods" do
15
+ # PageObjectPal.examine(Page, 'path/to/page/file', 'http://www.page.com').should be_true
16
+ # end
17
+ # end
18
+ #
19
+ # Can wrapped in a page object class method for validation.
20
+ # @example Class method
21
+ # class Page
22
+ # include PageObject
23
+ #
24
+ # def self.elements_still_valid
25
+ # PageObjectPal.examine(self, __FILE__, 'http://www.page.com')
26
+ # end
27
+ # end
28
+ #
29
+ module PageObjectPal
30
+ include PageObjectPal::Elements
31
+ include PageObjectPal::Diff
32
+ extend self
33
+
34
+ #
35
+ # Compare elements constructed by the PageObject::DSL to the elements in the
36
+ # URL's source code.
37
+ #
38
+ # @param [Class] Page object class to be tested
39
+ # @param [String] Path to the file housing the class being tested
40
+ # @param [String] Page URL
41
+ #
42
+ # @return [Boolean] True if no error is raised
43
+ #
44
+ def examine(klass, path, url)
45
+ diff_page(Nokogiri::HTML(open(url)), parse_class(klass, path))
46
+ success!
47
+ end
48
+
49
+ private
50
+ def success!
51
+ true
52
+ end
53
+
54
+ #
55
+ # Parse elements from the page object class methods.
56
+ #
57
+ def parse_class(klass, path)
58
+ methods = find_class_methods(klass)
59
+ elements = parse_methods(methods, path)
60
+ end
61
+
62
+ #
63
+ # Grab instance methods defined in the page object class and not its super classes.
64
+ #
65
+ def find_class_methods(klass)
66
+ methods = []
67
+ klass.instance_methods(false).each { |m| methods << m }
68
+ end
69
+
70
+ end
@@ -0,0 +1,51 @@
1
+ require 'page-object-pal/exceptions'
2
+ require 'page-object-pal/elements'
3
+
4
+ module PageObjectPal
5
+ module Diff
6
+ include PageObjectPal::Elements
7
+
8
+ #
9
+ # Find page object defined methods that do not match code
10
+ # in the source HTML.
11
+ #
12
+ def diff_page(source, elements)
13
+ elements.each do |e_hash|
14
+ e_hash.each do |elem, identifier|
15
+ scrub_source(elem, identifier, source)
16
+ end
17
+ end
18
+ end
19
+
20
+ #
21
+ # Look for code in the source HTML matching the identifying
22
+ # code defined in the page object class.
23
+ #
24
+ def scrub_source(tag, identifier, source)
25
+ identifier.each do |prop, prop_val|
26
+ match = case prop
27
+ when :class
28
+ source.css("#{tag.to_s}.#{prop_val}")
29
+ when :id
30
+ source.css("#{tag.to_s}##{prop_val}")
31
+ when :index
32
+ source.css("#{tag.to_s}")[prop_val.to_i]
33
+ when :text
34
+ source.search("[text()*='#{prop_val}']")
35
+ when :xpath
36
+ source.xpath(prop_val)
37
+ else
38
+ raise SupportError, "PageObjectPal doesn't support elements identified by '#{prop}'... yet. Consider ",
39
+ "forking the project at http://github.com/jdenen/page-object-pal."
40
+ end
41
+
42
+ raise PageObjectOutdated, "Could not identify '#{html_to_dsl(tag)}' where :#{prop} == '#{prop_val}'" if failure? match
43
+ end
44
+ end
45
+
46
+ def failure?(match)
47
+ match.to_a.empty?
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,89 @@
1
+ module PageObjectPal
2
+ module Elements
3
+
4
+ #
5
+ # Find lines in the page object class file that define the non-super
6
+ # instance methods.
7
+ #
8
+ def parse_methods(methods, path)
9
+ elements = []
10
+ methods.each do |meth|
11
+ File.open(path).read.each_line do |line|
12
+ next unless line.include? meth.to_s
13
+ next unless line.lstrip.start_with? "div", "element", "image", "link", "list_item", "ordered_list", "paragraph", "radio_button", "span", "text_area", "text_field", "unordered_list"
14
+ elements << parse_element(line.lstrip)
15
+ end
16
+ end
17
+ elements
18
+ end
19
+
20
+ #
21
+ # Convert method defining string to hash containing anchor_tag,
22
+ # identifying_property, and property_value.
23
+ #
24
+ def parse_element(string)
25
+ string.gsub!(/(,\s+)/, ",")
26
+ tag = dsl_to_html(string[/^(\w+)/])
27
+
28
+ case tag
29
+ when :element then element_tag(string)
30
+ else standard_tag(tag, string)
31
+ end
32
+ end
33
+
34
+ #
35
+ # Convert method defining string for PageObject::Accessors#element method.
36
+ #
37
+ def element_tag(string)
38
+ tag = string[/,:(\w+)/].gsub(",:","")
39
+ sym = string[/:class|:id|:index|:text|:xpath/].gsub(":","").to_sym
40
+ (sym == :index) ? val = string[/\d+/] : val = string[/"(.+)"/].gsub("\"","")
41
+ { tag => { sym => val } }
42
+ end
43
+
44
+ #
45
+ # Convert method defining string for standard PageObject::Accessors methods.
46
+ #
47
+ def standard_tag(tag, string)
48
+ sym = string[/:class|:id|:index|:text|:xpath/].gsub(":","").to_sym
49
+ (sym == :index) ? val = string[/\d+/] : val = string[/"(.+)"/].gsub("\"","")
50
+ { tag => { sym => val } }
51
+ end
52
+
53
+ #
54
+ # Convert non-HTML anchor_tags from the PageObject::DSL to HTML.
55
+ #
56
+ def dsl_to_html(anchor)
57
+ case anchor
58
+ when "image" then :img
59
+ when "link" then :a
60
+ when "list_item" then :li
61
+ when "ordered_list" then :ol
62
+ when "paragraph" then :p
63
+ when "radio_button" then :radio
64
+ when "text_area" then :input
65
+ when "text_field" then :input
66
+ when "unordered_list" then :ul
67
+ else anchor.to_sym
68
+ end
69
+ end
70
+
71
+ #
72
+ # Convert HTML to PageObject::DSL where the two are not identical.
73
+ #
74
+ def html_to_dsl(anchor)
75
+ case anchor
76
+ when :img then "image"
77
+ when :a then "link"
78
+ when :li then "list_item"
79
+ when :ol then "ordered_list"
80
+ when :p then "paragraph"
81
+ when :radio then "radio_button"
82
+ when :input then "text_field/text_area"
83
+ when :ul then "unordered_list"
84
+ else anchor.to_s
85
+ end
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,6 @@
1
+ module PageObjectPal
2
+ class PageObjectOutdated < StandardError; end
3
+ class AnchorError < StandardError; end
4
+ class SupportError < StandardError; end
5
+ class CannotFindClass < StandardError; end
6
+ end
@@ -0,0 +1,3 @@
1
+ module PageObjectPal
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'page-object-pal/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "page-object-pal"
8
+ spec.version = PageObjectPal::VERSION
9
+ spec.authors = ["Johnson Denen"]
10
+ spec.email = ["jdenen@manta.com"]
11
+ spec.description = %q{Did the test suite find a bug? Or are the page objects outdated by new code? PageObjectPal will keep an eye on your page objects and let you know when they need some TLC.}
12
+ spec.summary = %q{Page object maintenance made easier}
13
+ spec.homepage = "http://github.com/jdenen/page-object-pal"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "nokogiri", "~> 1.6.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", ">= 2.12.0"
26
+ spec.add_development_dependency "rspec-given", "~> 3.1.1"
27
+ spec.add_development_dependency "page-object", "~> 0.9.2"
28
+ end
data/spec/Page.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'page-object'
2
+
3
+ class Page
4
+ include PageObject
5
+ link(:by_class, :class => "coolLight")
6
+ link(:by_id, :id => "signup-nav-header")
7
+ link(:by_index, :index => 0)
8
+ link(:by_text, :text => "Advertise With Us")
9
+ link(:by_xpath, :xpath => "(//a)[1]")
10
+ element(:by_element, :a, :xpath => "(//a)[1]")
11
+
12
+ def self.healthy?
13
+ PageObjectPal.examine(self, __FILE__, "http://www.manta.com")
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'rspec'
2
+
3
+ describe PageObjectPal do
4
+ context "Converting to-and-from PageObject DSL" do
5
+ context "Converting most tags" do
6
+ ['div','image','link','list_item','ordered_list','paragraph','radio_button','unordered_list'].each do |anchor_tag|
7
+ Then { PageObjectPal.html_to_dsl(PageObjectPal.dsl_to_html(anchor_tag)).should == anchor_tag }
8
+ end
9
+ end
10
+
11
+ context "Converting text_area and text_field" do
12
+ Then { PageObjectPal.html_to_dsl(PageObjectPal.dsl_to_html("text_area")).should == "text_field/text_area" }
13
+ And { PageObjectPal.html_to_dsl(PageObjectPal.dsl_to_html("text_field")).should == "text_field/text_area" }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+
3
+ describe PageObjectPal do
4
+ describe "#parse_element" do
5
+ context "passed uncommented line" do
6
+ Then { PageObjectPal.parse_element('link(:stuff, :xpath => "//a")').should be_a_kind_of(Hash) }
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec'
2
+
3
+ describe PageObjectPal, :private => true do
4
+ Given(:source) { Nokogiri::HTML(open("http://www.manta.com")) }
5
+
6
+ context "using unsupported identifying property" do
7
+ Then { expect { PageObjectPal.scrub_source('a', {:numero => 'uno'}, source) }.to raise_error(PageObjectPal::SupportError) }
8
+ end
9
+
10
+ context "using invalid identification" do
11
+ context "HTML selectors" do
12
+ Then { expect { PageObjectPal.scrub_source('a', {:class => 'invalid-class'}, source) }.to raise_error(PageObjectPal::PageObjectOutdated) }
13
+ Then { expect { PageObjectPal.scrub_source('a', {:id => 'invalid-id'}, source) }.to raise_error(PageObjectPal::PageObjectOutdated) }
14
+ Then { expect { PageObjectPal.scrub_source('a', {:index => 9000}, source) }.to raise_error(PageObjectPal::PageObjectOutdated) }
15
+ Then { expect { PageObjectPal.scrub_source('a', {:text => 'xxcvxddvndxm'}, source) }.to raise_error(PageObjectPal::PageObjectOutdated) }
16
+ end
17
+
18
+ context "Xpath selector" do
19
+ Then { expect { PageObjectPal.scrub_source('a', {:xpath => "//a[@id='invalid-id']"}, source) }.to raise_error(PageObjectPal::PageObjectOutdated) }
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'rspec'
2
+
3
+ describe PageObjectPal do
4
+ Given(:page) { require_relative 'Page'; Page }
5
+ Given(:path) { File.join(File.dirname(__FILE__), "Page.rb") }
6
+ Given(:src) { "http://www.manta.com" }
7
+
8
+ context "Evaluate from outside PageObject" do
9
+ Then { PageObjectPal.examine(page, path, src).should be_true }
10
+ end
11
+
12
+ context "Evaluate from PageObject method" do
13
+ Then { page.healthy?.should be_true }
14
+ end
15
+
16
+ end
@@ -0,0 +1,10 @@
1
+ require 'rspec-given'
2
+ require 'page-object-pal'
3
+
4
+ RSpec.configure do |config|
5
+ config.before :each, :private => true do
6
+ PageObjectPal.class_eval do
7
+ public *private_instance_methods
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page-object-pal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Johnson Denen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.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: 1.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.12.0
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: 2.12.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-given
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 3.1.1
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 3.1.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: page-object
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.9.2
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.9.2
110
+ description: Did the test suite find a bug? Or are the page objects outdated by new
111
+ code? PageObjectPal will keep an eye on your page objects and let you know when
112
+ they need some TLC.
113
+ email:
114
+ - jdenen@manta.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/page-object-pal.rb
126
+ - lib/page-object-pal/diff.rb
127
+ - lib/page-object-pal/elements.rb
128
+ - lib/page-object-pal/exceptions.rb
129
+ - lib/page-object-pal/version.rb
130
+ - page-object-pal.gemspec
131
+ - spec/Page.rb
132
+ - spec/dsl_conversion_spec.rb
133
+ - spec/elements_spec.rb
134
+ - spec/exceptions_spec.rb
135
+ - spec/identifiers_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: http://github.com/jdenen/page-object-pal
138
+ licenses:
139
+ - MIT
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ segments:
151
+ - 0
152
+ hash: -577390764071290171
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ segments:
160
+ - 0
161
+ hash: -577390764071290171
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 1.8.25
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Page object maintenance made easier
168
+ test_files:
169
+ - spec/Page.rb
170
+ - spec/dsl_conversion_spec.rb
171
+ - spec/elements_spec.rb
172
+ - spec/exceptions_spec.rb
173
+ - spec/identifiers_spec.rb
174
+ - spec/spec_helper.rb