mache 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a33c9e84520e543f6693fd7895d9fb6c6835626
4
+ data.tar.gz: 7370d76223bd10fe2444e2d091e9ddd64219181a
5
+ SHA512:
6
+ metadata.gz: 771ff8e66362d6788b046bb94e092a100dacd2f952ee960f0b2530b47eca889a0b8a2a57ba7c4ac40116214663d3d14ca89db988e861f6ed46ed1d52c0bc37a9
7
+ data.tar.gz: 4b203e4da5927d68605208ed06b8aa54a81d7c3c8f69d5c1f19cc13b1019e9c3fd7442cfb857aa11ac244b634d967a4fd1badcdc9e9ae3c28db66115b29b634b
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "bin/**/*"
4
+ - "mache.gemspec"
5
+
6
+ Metrics/BlockLength:
7
+ ExcludedMethods: describe
8
+
9
+ Style/Documentation:
10
+ Exclude:
11
+ - "spec/**/*"
12
+
13
+ Style/StringLiterals:
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/FrozenStringLiteralComment:
17
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.3
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ language: ruby
2
+ before_install: gem install bundler -v 1.14.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mache.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Joshua Bassett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Mache
2
+
3
+ [![Build Status](https://travis-ci.org/nullobject/mache.svg?branch=master)](https://travis-ci.org/nullobject/mache)
4
+
5
+ A [page object](https://martinfowler.com/bliki/PageObject.html) library for writing cleaner acceptance tests with [Capybara](https://github.com/teamcapybara/capybara).
6
+
7
+ ## Getting started
8
+
9
+ Consider the following HTML snippet:
10
+
11
+ ```html
12
+ <html>
13
+ <body>
14
+ <header>
15
+ <h1>Welcome</h1>
16
+ </header>
17
+ <nav>
18
+ <a href="#" class="selected">foo</a>
19
+ <a href="#">bar</a>
20
+ <a href="#">baz</a>
21
+ </nav>
22
+ <main>
23
+ lorem ipsum
24
+ </main>
25
+ </body>
26
+ </html>
27
+ ```
28
+
29
+ To define a page object class to wrap this HTML snippet, we extend the
30
+ `Mache::Page` class. The only method our class needs to provide is `path`, this
31
+ tells Mache where to go when we want to visit the page.
32
+
33
+ ```ruby
34
+ class WelcomePage < Mache::Page
35
+ def path
36
+ "/welcome"
37
+ end
38
+ end
39
+ ```
40
+
41
+ This is how we visit our page object:
42
+
43
+ ```ruby
44
+ page = WelcomePage.visit
45
+ page.current? # true
46
+ ```
47
+
48
+ Any Capybara API methods will be forwarded to the underlying node:
49
+
50
+ ```ruby
51
+ page.find("body > main").text # "lorem ipsum"
52
+ ```
53
+
54
+ ### Elements
55
+
56
+ To make our page object more useful, we can define an element on our page
57
+ object class using the `element` macro. An element is simply a HTML element
58
+ that we expect to find on the page using a CSS selector.
59
+
60
+ Let's define a `main` element:
61
+
62
+ ```ruby
63
+ class WelcomePage < Mache::Page
64
+ element :main, "body > main"
65
+
66
+ def path
67
+ "/welcome"
68
+ end
69
+ end
70
+ ```
71
+
72
+ Now we can query the element on our page object:
73
+
74
+ ```ruby
75
+ page.has_main? # true
76
+ page.main.visible? # true
77
+ page.main.text # "lorem ipsum"
78
+ ```
79
+
80
+ ### Components
81
+
82
+ For elements that can be shared across an number of page object classes, it may
83
+ be useful to define a reusable component by extending the `Mache::Component`
84
+ class. A component class can contain any number of elements or other
85
+ components:
86
+
87
+ ```ruby
88
+ class Header < Mache::Component
89
+ element :title, "h1"
90
+ end
91
+ ```
92
+
93
+ Our page object class can mount our component at a given CSS selector using the
94
+ `component` macro:
95
+
96
+ ```ruby
97
+ class WelcomePage < Mache::Page
98
+ component :header, Header, "header"
99
+ element :main, "body > main"
100
+
101
+ def path
102
+ "/welcome"
103
+ end
104
+ end
105
+ ```
106
+
107
+ Querying a component on our page object is much the same as an element:
108
+
109
+ ```ruby
110
+ page.has_header? # true
111
+ page.header.visible? # true
112
+ page.header.title.text # "Welcome"
113
+ ```
114
+
115
+ ## Example
116
+
117
+ Let's look at a more complete example for our `WelcomePage`:
118
+
119
+ ```ruby
120
+ class Header < Mache::Component
121
+ element :title, "h1"
122
+ end
123
+
124
+ class NavItem < Mache::Component
125
+ def selected?
126
+ node[:class].include?("selected")
127
+ end
128
+ end
129
+
130
+ class Nav < Mache::Component
131
+ components :items, NavItem, "a"
132
+ end
133
+
134
+ class WelcomePage < Mache::Page
135
+ component :header, Header, "header"
136
+ component :nav, Nav, "nav"
137
+ element :main, "main"
138
+
139
+ def path
140
+ "/welcome"
141
+ end
142
+ end
143
+ ```
144
+
145
+ We can use our page objects to write expressive tests:
146
+
147
+ ```ruby
148
+ feature "Home page" do
149
+ let(:home_page) { WelcomePage.visit }
150
+
151
+ scenario "A user visits the home page" do
152
+ expect(home_page).to be_current
153
+
154
+ expect(home_page).to have_header
155
+ expect(home_page.header.title).to eq("Welcome")
156
+
157
+ expect(home_page).to have_nav
158
+ expect(home_page.nav).to have_items
159
+ expect(home_page.nav.items.count).to be(3)
160
+
161
+ expect(home_page.nav.items[0].text).to eq("foo")
162
+ expect(home_page.nav.items[1].text).to eq("bar")
163
+ expect(home_page.nav.items[2].text).to eq("baz")
164
+
165
+ expect(home_page.nav.items[0]).to be_selected
166
+
167
+ expect(home_page.main.text).to eq("lorem ipsum")
168
+ end
169
+ end
170
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: [:spec, :rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,6 @@
1
+ require "mache/node"
2
+
3
+ module Mache
4
+ class Component < Node
5
+ end
6
+ end
data/lib/mache/dsl.rb ADDED
@@ -0,0 +1,58 @@
1
+ module Mache
2
+ # Provides the mache DSL for nodes.
3
+ module DSL
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods #:nodoc:
9
+ def automation(*ids)
10
+ ids.map { |id| %([data-automation="#{id}"]) }.join(" ")
11
+ end
12
+
13
+ def element(name, selector)
14
+ define_method(name.to_s) do
15
+ @node.find(selector)
16
+ end
17
+ define_helper_methods(name, selector)
18
+ end
19
+
20
+ def elements(name, selector)
21
+ define_method(name.to_s) do
22
+ @node.all(selector, minimum: 1)
23
+ end
24
+ define_helper_methods(name, selector)
25
+ end
26
+
27
+ def component(name, type, selector)
28
+ define_method(name.to_s) do
29
+ type.new(@node.find(selector))
30
+ end
31
+ define_helper_methods(name, selector)
32
+ end
33
+
34
+ def components(name, type, selector)
35
+ define_method(name.to_s) do
36
+ @node.all(selector, minimum: 1).map { |element| type.new(element) }
37
+ end
38
+ define_helper_methods(name, selector)
39
+ end
40
+
41
+ private
42
+
43
+ def define_helper_methods(name, selector)
44
+ define_existence_predicates(name, selector)
45
+ end
46
+
47
+ def define_existence_predicates(name, selector)
48
+ define_method("has_#{name}?") do
49
+ @node.has_selector?(selector)
50
+ end
51
+
52
+ define_method("has_no_#{name}?") do
53
+ @node.has_no_selector?(selector)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/mache/node.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "mache/dsl"
2
+
3
+ module Mache
4
+ # An abstract class that wraps a capybara node object and exposes the mache
5
+ # DSL. It also delegates any capybara methods to the underlying node object.
6
+ class Node
7
+ include DSL
8
+
9
+ attr_reader :node
10
+
11
+ def initialize(node)
12
+ @node = node
13
+ end
14
+
15
+ def method_missing(name, *args, &block)
16
+ if @node.respond_to?(name)
17
+ @node.send(name, *args, &block)
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ def respond_to_missing?(name, include_private = false)
24
+ @node.respond_to?(name) || super
25
+ end
26
+ end
27
+ end
data/lib/mache/page.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "capybara"
2
+ require "mache/node"
3
+
4
+ module Mache
5
+ # A page provides a DSL for querying a wrapped capybara node object node. A
6
+ # page can also has a path which can be visited.
7
+ class Page < Node
8
+ attr_reader :path
9
+
10
+ def initialize(node: Capybara.current_session, path: nil)
11
+ @node ||= node
12
+ @path ||= path
13
+ end
14
+
15
+ def visit
16
+ @node.visit(path)
17
+ self
18
+ end
19
+
20
+ def current?
21
+ @node.current_path == path
22
+ end
23
+
24
+ def self.visit
25
+ new.tap(&:visit)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Mache
2
+ VERSION = "1.0.0".freeze
3
+ end
data/lib/mache.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "mache/component"
2
+ require "mache/page"
3
+ require "mache/version"
data/mache.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "mache/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mache"
8
+ spec.version = Mache::VERSION
9
+ spec.authors = ["Joshua Bassett"]
10
+ spec.email = ["josh.bassett@gmail.com"]
11
+ spec.summary = "A page object library for writing cleaner acceptance tests with Capybara."
12
+ spec.description = "Mache provides a DSL for writing page object clases to use in your acceptance tests."
13
+ spec.homepage = "https://github.com/nullobject/mache"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "capybara", "~> 2"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "rake", "~> 12.0"
24
+ spec.add_development_dependency "rack", "~> 2.0"
25
+ spec.add_development_dependency "rspec", "~> 3.5"
26
+ spec.add_development_dependency "rubocop", "~> 0.47"
27
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mache
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Bassett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.47'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.47'
97
+ description: Mache provides a DSL for writing page object clases to use in your acceptance
98
+ tests.
99
+ email:
100
+ - josh.bassett@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".ruby-version"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - LICENSE
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - lib/mache.rb
116
+ - lib/mache/component.rb
117
+ - lib/mache/dsl.rb
118
+ - lib/mache/node.rb
119
+ - lib/mache/page.rb
120
+ - lib/mache/version.rb
121
+ - mache.gemspec
122
+ homepage: https://github.com/nullobject/mache
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.5.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A page object library for writing cleaner acceptance tests with Capybara.
146
+ test_files: []