rspec-html 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +24 -8
- data/lib/rspec/html.rb +5 -1
- data/lib/rspec/html/version.rb +1 -1
- data/lib/rspec_html/searchable.rb +18 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d34bf3daf886cca4a7e1b50de449025cd6255629a1fb6a5f6d319c6353fedb0a
|
4
|
+
data.tar.gz: 7f3ae650fa77a60700211ccda0cb3478e034d8b14f22a40375b89ed007ecb843
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0ae4675dfe2790a31f953e089ce97157f6b736967abc96281eb50adc04641e1725765085be03770686dbff3c8ccd8d2fa4d567fd366edaabb74c7f2348f5e27
|
7
|
+
data.tar.gz: '0384ab293fe9397088d0c6356585d1c0657a443ba7a2be8ed1d6a8cdd05ada3b173adc7138091db29b4eedf0c3a267360231735fe1d9fa53144ee64f7367bf39'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@ _RSpec::HTML_ provides a simple object interface to HTML responses from [_RSpec
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem 'rspec-html', '~> 0.2.
|
8
|
+
gem 'rspec-html', '~> 0.2.1'
|
9
9
|
```
|
10
10
|
|
11
11
|
Bundle
|
@@ -26,11 +26,20 @@ Require the gem in your `spec_helper.rb`:
|
|
26
26
|
require 'rspec/html'
|
27
27
|
```
|
28
28
|
|
29
|
-
In request specs, access the HTML document through the provided `document` object.
|
30
|
-
|
31
29
|
### Object Interface
|
32
30
|
|
33
|
-
|
31
|
+
The top-level object `document` is available in all tests which reflects the current response body (e.g. in request specs).
|
32
|
+
|
33
|
+
If you need to parse _HTML_ manually you can use the provided `parse_html` helper and then access `document` as normal:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
before { parse_html('<html><body>hello</body></html>') }
|
37
|
+
it 'says hello' do
|
38
|
+
expect(document.body).to include 'hello'
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
To navigate the _DOM_ by a sequence of tag names use chained method calls on the `document` object:
|
34
43
|
|
35
44
|
#### Tag Traversal
|
36
45
|
```ruby
|
@@ -40,20 +49,27 @@ expect(document.body.div.span).to include 'some text'
|
|
40
49
|
#### Attribute Matching
|
41
50
|
To select an element matching certain attributes pass a hash to any of the chained methods:
|
42
51
|
```ruby
|
43
|
-
expect(document.body.div(id: 'my-div').span(
|
52
|
+
expect(document.body.div(id: 'my-div').span(align: 'left')).to include 'some text'
|
53
|
+
```
|
54
|
+
|
55
|
+
#### Class Matching
|
56
|
+
CSS classes are treated as a special case: to select an element matching a specific class (or array of classes) pass the `class` parameter:
|
57
|
+
```ruby
|
58
|
+
expect(document.body.div(id: 'my-div').span(class: 'my-class')).to include 'some text'
|
59
|
+
expect(document.body.div(id: 'my-div').span(class: ['my-class', 'my-other-class'])).to include 'some text'
|
44
60
|
```
|
45
61
|
|
46
62
|
#### Attribute Retrieval
|
47
63
|
To select an attribute from an element use the hash-style interface:
|
48
64
|
```ruby
|
49
|
-
expect(document.body.div.span[:class]).to
|
50
|
-
expect(document.body.div.span['data-content']).to
|
65
|
+
expect(document.body.div.span[:class]).to include 'my-class'
|
66
|
+
expect(document.body.div.span['data-content']).to include 'my content'
|
51
67
|
```
|
52
68
|
|
53
69
|
#### Indexing a Matching Set
|
54
70
|
To select an index from a set of matched elements use the array-style interface (the first matching element is `1`, not `0`):
|
55
71
|
```ruby
|
56
|
-
expect(document.body.div[1].span[1][:class]).to
|
72
|
+
expect(document.body.div[1].span[1][:class]).to include 'my-class'
|
57
73
|
```
|
58
74
|
|
59
75
|
#### Element Existence
|
data/lib/rspec/html.rb
CHANGED
@@ -9,7 +9,11 @@ module RSpec
|
|
9
9
|
# Module extension for RSpec::SharedContext
|
10
10
|
module HTML
|
11
11
|
def document
|
12
|
-
RSpecHTML::Element.new(Nokogiri::HTML.parse(response.body), :document)
|
12
|
+
@document || RSpecHTML::Element.new(Nokogiri::HTML.parse(response.body), :document)
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse_html(content)
|
16
|
+
@document = RSpecHTML::Element.new(Nokogiri::HTML.parse(content), :document)
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
data/lib/rspec/html/version.rb
CHANGED
@@ -76,18 +76,34 @@ module RSpecHTML
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def where(tag, query, all: false)
|
79
|
-
matched =
|
79
|
+
matched = if query[:class]
|
80
|
+
where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil))
|
81
|
+
else
|
82
|
+
where_xpath(tag, query)
|
83
|
+
end
|
80
84
|
return matched&.first unless all
|
81
85
|
|
82
86
|
matched
|
83
87
|
end
|
84
88
|
|
89
|
+
def where_xpath(tag, query)
|
90
|
+
conditions = "[#{where_conditions(query)}]" unless query.compact.empty?
|
91
|
+
@element&.xpath("//#{tag}#{conditions}")
|
92
|
+
end
|
93
|
+
|
85
94
|
def where_conditions(query)
|
86
|
-
query.map do |key, value|
|
95
|
+
query.compact.map do |key, value|
|
96
|
+
next if value.nil?
|
97
|
+
|
87
98
|
%(@#{key}="#{value}")
|
88
99
|
end.join ' and '
|
89
100
|
end
|
90
101
|
|
102
|
+
def where_class(tag, class_or_classes)
|
103
|
+
selector = class_or_classes.is_a?(Array) ? class_or_classes.join('.') : class_or_classes
|
104
|
+
@element&.css("#{tag}.#{selector}")
|
105
|
+
end
|
106
|
+
|
91
107
|
def find(tag, all: false)
|
92
108
|
return @element&.css(tag.to_s)&.first unless all
|
93
109
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Farrell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|