xpath-specs 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- checksums.yaml.gz.sig +2 -2
- data.tar.gz.sig +0 -0
- data/.rspec +1 -0
- data/.travis.yml +6 -0
- data/Guardfile +9 -0
- data/README.md +78 -1
- data/Rakefile +6 -0
- data/lib/xpath/specs/contain_a_matcher.rb +3 -0
- data/lib/xpath/specs/page_part.rb +23 -4
- data/lib/xpath/specs/version.rb +1 -1
- data/spec/lib/xpath/specs/contain_a_matcher_spec.rb +102 -0
- data/spec/spec_helper.rb +3 -0
- data/xpath-specs.gemspec +3 -1
- metadata +28 -7
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWNkNzJkN2RlODc2OTYyMDc2NmJkN2ZlMDg2MTU1NjdkYzNjOTljNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzhkZTliYWUxNDBiOWZjYjQ3NTEwNjZkZDBkN2M2NGUxNDQyMWU0Yg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWUwMTIxNTVlY2U1OWMyODI0MjI4YzExYTYxNzIwZTk1NmM5MDhmOGYxNDk3
|
10
|
+
NWYxNjQwZjRmNjBiNGQ0YzI4MjMwNjQ2MjhiMzg2MjE3MDI3ZDFlZWI4MmMx
|
11
|
+
YzZmYzQ3ZGY4MmE3ZjliZmYxYTkwYTExN2EyM2RhNDJmNzNjYjQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGFiN2RmNmJlNGY0MmQxY2M0N2MwNTVhNTg3NTRkNzQ5NmQ0N2EwN2M4YzMx
|
14
|
+
MmZkNTVhOWQxOGFjOThjNDRlNmQyZjBmNjk1OTYyNTM1N2M2MjIwZGViMjE0
|
15
|
+
MzIyNDQxYjAzMmIyOGMwNzJjZDMyYzY5MzExMDk3MGY3NjQ4MzY=
|
checksums.yaml.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
-���
|
2
|
+
���9�H��^�\�&x]�s���po����S����
|
data.tar.gz.sig
CHANGED
Binary file
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -18,7 +18,84 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
As an example, suppose you have website with dishes and receipes. You want to test your views (either in RSpec or Cucumber) to make sure that they contain parts you want them to.
|
22
|
+
|
23
|
+
### Declare your page parts
|
24
|
+
|
25
|
+
In order to encourage reuse, create one or many custom page part definition files
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# spec/support/knows_page_parts.rb
|
29
|
+
|
30
|
+
module KnowsPageParts
|
31
|
+
def dish_panel
|
32
|
+
Xpath::Specs::PagePart.new("the dish panel", "//table[@id='dish-panel']")
|
33
|
+
end
|
34
|
+
|
35
|
+
def dish
|
36
|
+
# match a sub element of dish_panel
|
37
|
+
dish_panel.with("a dish", "//tr")
|
38
|
+
end
|
39
|
+
|
40
|
+
def dish_with_name(name)
|
41
|
+
# match a special dish
|
42
|
+
dish.that("is named #{name}", "[td[contains(.,'#{name}')]]")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
```
|
47
|
+
|
48
|
+
### Use them in your specs
|
49
|
+
|
50
|
+
With these page parts definitions in place, you can now use them to test your views. Suppose this is the html for your view :
|
51
|
+
|
52
|
+
```html
|
53
|
+
<html>
|
54
|
+
<head></head>
|
55
|
+
<body>
|
56
|
+
<div>
|
57
|
+
<table id="dish-panel">
|
58
|
+
<tr><td>Pizza</td>...</tr>
|
59
|
+
<tr><td>Cheese Burger</td>...</tr>
|
60
|
+
...
|
61
|
+
</table>
|
62
|
+
</div>
|
63
|
+
</body>
|
64
|
+
</html>
|
65
|
+
```
|
66
|
+
|
67
|
+
* You can test that your view contains the dish panel :
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
expect(html).to contain_a(dish_panel)
|
71
|
+
```
|
72
|
+
|
73
|
+
* You can test that it contains at least one dish
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
expect(html).to contain_a(dish)
|
77
|
+
```
|
78
|
+
|
79
|
+
* You can also test that it contains a pizza
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
expect(html).to contain_a(dish_with_name("Pizza")
|
83
|
+
```
|
84
|
+
|
85
|
+
* Eventually, if you try to search for a dish that is not there :
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
expect(html).to contain_a(dish_with_name("Grilled Lobster")
|
89
|
+
```
|
90
|
+
|
91
|
+
You'll get a nice error message :
|
92
|
+
|
93
|
+
```
|
94
|
+
expected the page to contain a dish that is named Grilled Lobster (//table[@id='dish-panel']//tr[td[contains(.,'#{name}')]])
|
95
|
+
it found a dish (//table[@id='dish-panel']//tr) :
|
96
|
+
<tr><td>Pizza</td>...</tr>
|
97
|
+
but not a dish that is named Grilled Lobster (//table[@id='dish-panel']//tr[td[contains(.,'#{name}')]])
|
98
|
+
```
|
22
99
|
|
23
100
|
## Contributing
|
24
101
|
|
data/Rakefile
CHANGED
@@ -3,7 +3,14 @@
|
|
3
3
|
module Xpath
|
4
4
|
module Specs
|
5
5
|
|
6
|
+
# XPath wrapper providing a user friendly description.
|
7
|
+
# Should be used with the contain_a rspec matcher.
|
6
8
|
class PagePart
|
9
|
+
|
10
|
+
# description: user friendly text used in assertions error messages
|
11
|
+
# xpath: the actual xpath to search for
|
12
|
+
# parent: an optional parent page part, used to provide better assertion
|
13
|
+
# failure diagnostic
|
7
14
|
def initialize(description, xpath, parent = nil)
|
8
15
|
@xpath = xpath
|
9
16
|
@description = description
|
@@ -16,20 +23,32 @@ module Xpath
|
|
16
23
|
!parent.nil?
|
17
24
|
end
|
18
25
|
|
26
|
+
# A full description containing the xpath
|
19
27
|
def long_description
|
20
28
|
"#{description} (#{xpath})"
|
21
29
|
end
|
22
30
|
|
31
|
+
# Creates another PagePart instance to match elements relatively to self
|
32
|
+
# Uses a completely new description. It's the prefered way to mach sub
|
33
|
+
# elements, xpath should usually start with '/'
|
23
34
|
def with(description, xpath)
|
24
35
|
PagePart.new(description, self.xpath+xpath, self)
|
25
36
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
37
|
+
|
38
|
+
# Creates another PagePart instance to match elements relatively to self
|
39
|
+
# Concatenates the descriptions. It's the prefered way to add constraints
|
40
|
+
# to the currently matched elements, xpath should usually start with '['
|
29
41
|
def that(description, xpath)
|
30
|
-
PagePart.new("#{self.description} that #{description}",
|
42
|
+
PagePart.new("#{self.description} that #{description}",
|
43
|
+
self.xpath+xpath,
|
44
|
+
self)
|
31
45
|
end
|
32
46
|
|
47
|
+
# Creates another PagePart instance like self, but relatively to another
|
48
|
+
# existing page part.
|
49
|
+
def within_a(outer_page_part)
|
50
|
+
outer_page_part.with(self.description, self.xpath)
|
51
|
+
end
|
33
52
|
end
|
34
53
|
|
35
54
|
end
|
data/lib/xpath/specs/version.rb
CHANGED
@@ -0,0 +1,102 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Xpath
|
6
|
+
module Specs
|
7
|
+
|
8
|
+
describe ContainA do
|
9
|
+
|
10
|
+
context 'direct matching' do
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
@html_with_link = '<html>
|
14
|
+
<head></head>
|
15
|
+
<body>
|
16
|
+
<a href="#">Click Me !</a>
|
17
|
+
</body>
|
18
|
+
</html>'
|
19
|
+
|
20
|
+
@blank_html = '<html>
|
21
|
+
<head></head>
|
22
|
+
<body>
|
23
|
+
</body>
|
24
|
+
</html>'
|
25
|
+
|
26
|
+
@link = PagePart.new("<my link>", '//a')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'matches an existing xpath' do
|
30
|
+
expect(@html_with_link).to contain_a(@link)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'has better error messages for missing xpath' do
|
34
|
+
expect do
|
35
|
+
expect(@blank_html).to contain_a(@link)
|
36
|
+
end.to raise_error(Regexp.new("expected the page to contain <my link> \\(\\/\\/a\\)"+
|
37
|
+
"\\s+but could not find <my link> \\(\\/\\/a\\)"))
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'has clear error messages for unexpected xpath' do
|
41
|
+
expect do
|
42
|
+
expect(@html_with_link).not_to contain_a(@link)
|
43
|
+
end.to raise_error(/expected the page not to contain <my link> \(\/\/a\)/)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'automaticaly works on body attributes' do
|
47
|
+
expect(double(:body => @html_with_link)).to contain_a(@link)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'nesting xpath' do
|
53
|
+
|
54
|
+
before :each do
|
55
|
+
@html_with_link = '<html>
|
56
|
+
<head></head>
|
57
|
+
<body>
|
58
|
+
<div>
|
59
|
+
<a href="#">Click Me !</a>
|
60
|
+
</div>
|
61
|
+
</body>
|
62
|
+
</html>'
|
63
|
+
|
64
|
+
@html_without_link = '<html>
|
65
|
+
<head></head>
|
66
|
+
<body>
|
67
|
+
<div></div>
|
68
|
+
</body>
|
69
|
+
</html>'
|
70
|
+
|
71
|
+
@nested_link = PagePart.new("<my div>", '//div').with('<my nested link>', '/a')
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'matches a nested xpath' do
|
75
|
+
expect(@html_with_link).to contain_a(@nested_link)
|
76
|
+
end
|
77
|
+
it 'has even better error messages for missing nested xpath' do
|
78
|
+
expect do
|
79
|
+
expect(@html_without_link).to contain_a(@nested_link)
|
80
|
+
end.to raise_error(Regexp.new("expected the page to contain <my nested link> \\(\\/\\/div\\/a\\)"+
|
81
|
+
"\\s+it found <my div> \\(\\/\\/div\\) :"+
|
82
|
+
"\\s+<div><\\/div>"+
|
83
|
+
"\\s+but not <my nested link> \\(\\/\\/div\\/a\\)"))
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'supports nesting xpath the other way round' do
|
87
|
+
link_within = PagePart.new('link', '//a').within_a(PagePart.new('div', '//div'))
|
88
|
+
|
89
|
+
expect(link_within.xpath).to eq('//div//a')
|
90
|
+
end
|
91
|
+
it 'supports specializing xpath' do
|
92
|
+
red_link = PagePart.new('link', '//a').that('is red', "[@class='red']")
|
93
|
+
|
94
|
+
expect(red_link.xpath).to eq("//a[@class='red']")
|
95
|
+
expect(red_link.description).to eq('link that is red')
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/xpath-specs.gemspec
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "nokogiri"
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "bundler"
|
24
24
|
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "guard-rspec"
|
26
|
+
|
25
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xpath-specs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Philou
|
@@ -38,7 +38,7 @@ cert_chain:
|
|
38
38
|
NU5zd3dIY0llUVpCL05JRDdiZXJJZjlhd3gzCnlMY2wxY21tNUFMdEovK0Jr
|
39
39
|
a21wMGk0YW1YZVRETXZxOXI4UEJzVnNRd3hZT1lKQlArVW14ejNQWDZIakZI
|
40
40
|
clEKWGRrWHgzb1oKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
|
41
|
-
date: 2014-05-
|
41
|
+
date: 2014-05-18 00:00:00.000000000 Z
|
42
42
|
dependencies:
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: nokogiri
|
@@ -58,16 +58,16 @@ dependencies:
|
|
58
58
|
name: bundler
|
59
59
|
requirement: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- -
|
61
|
+
- - ! '>='
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: '
|
63
|
+
version: '0'
|
64
64
|
type: :development
|
65
65
|
prerelease: false
|
66
66
|
version_requirements: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
|
-
- -
|
68
|
+
- - ! '>='
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version: '
|
70
|
+
version: '0'
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: rake
|
73
73
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,6 +82,20 @@ dependencies:
|
|
82
82
|
- - ! '>='
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: guard-rspec
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
85
99
|
description: A gem providing recursive and named wrappers around xpaths, in order
|
86
100
|
to improve error messages when testing xpath presence in html files
|
87
101
|
email:
|
@@ -91,7 +105,10 @@ extensions: []
|
|
91
105
|
extra_rdoc_files: []
|
92
106
|
files:
|
93
107
|
- .gitignore
|
108
|
+
- .rspec
|
109
|
+
- .travis.yml
|
94
110
|
- Gemfile
|
111
|
+
- Guardfile
|
95
112
|
- LICENSE.txt
|
96
113
|
- README.md
|
97
114
|
- Rakefile
|
@@ -99,6 +116,8 @@ files:
|
|
99
116
|
- lib/xpath/specs/contain_a_matcher.rb
|
100
117
|
- lib/xpath/specs/page_part.rb
|
101
118
|
- lib/xpath/specs/version.rb
|
119
|
+
- spec/lib/xpath/specs/contain_a_matcher_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
102
121
|
- xpath-specs.gemspec
|
103
122
|
homepage: https://github.com/philou/xpath-specs
|
104
123
|
licenses:
|
@@ -124,4 +143,6 @@ rubygems_version: 2.0.3
|
|
124
143
|
signing_key:
|
125
144
|
specification_version: 4
|
126
145
|
summary: An RSpec library to get better messages when matching XPaths
|
127
|
-
test_files:
|
146
|
+
test_files:
|
147
|
+
- spec/lib/xpath/specs/contain_a_matcher_spec.rb
|
148
|
+
- spec/spec_helper.rb
|
metadata.gz.sig
CHANGED
Binary file
|