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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d6b33d4bbde8f992f361359fed2e7ca1fa5aea60031bfeb72fcc719fe61bb4a
4
- data.tar.gz: c8bbd8cae270839cba34034bdbd78a8d42085f01fe29891ef01eed6299805b1b
3
+ metadata.gz: d34bf3daf886cca4a7e1b50de449025cd6255629a1fb6a5f6d319c6353fedb0a
4
+ data.tar.gz: 7f3ae650fa77a60700211ccda0cb3478e034d8b14f22a40375b89ed007ecb843
5
5
  SHA512:
6
- metadata.gz: 810cc82b373e10c3ab4f137d7dc4648972f81654f07478b937b7eb38d43832273c5b0203ad8ebcc7fb6927efea78ac1d048663d9c4a5f95d8afa8459f8bdae97
7
- data.tar.gz: 0a9e83ccf82b023d0f4809935c354ef3d31b5bb6cc95161395bd8b4246ae839fcc71abfc933563d913eea8fcd9ee0935992c535a5455ef230a768e91ca48981d
6
+ metadata.gz: a0ae4675dfe2790a31f953e089ce97157f6b736967abc96281eb50adc04641e1725765085be03770686dbff3c8ccd8d2fa4d567fd366edaabb74c7f2348f5e27
7
+ data.tar.gz: '0384ab293fe9397088d0c6356585d1c0657a443ba7a2be8ed1d6a8cdd05ada3b173adc7138091db29b4eedf0c3a267360231735fe1d9fa53144ee64f7367bf39'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-html (0.2.0)
4
+ rspec-html (0.2.1)
5
5
  nokogiri (~> 1.10)
6
6
  rspec (~> 3.0)
7
7
 
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.0'
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
- To navigating the _DOM_ by a sequence of tag names use chained method calls on the `document` object:
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(class: 'my-class')).to eql 'some text'
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 eql 'my-class'
50
- expect(document.body.div.span['data-content']).to eql 'my content'
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 eql 'my-class'
72
+ expect(document.body.div[1].span[1][:class]).to include 'my-class'
57
73
  ```
58
74
 
59
75
  #### Element Existence
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module HTML
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
@@ -76,18 +76,34 @@ module RSpecHTML
76
76
  end
77
77
 
78
78
  def where(tag, query, all: false)
79
- matched = @element&.xpath("//#{tag}[#{where_conditions(query)}]")
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.0
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-28 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri