rspec-html 0.3.0 → 0.3.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: 0cfd83e0a3a67f3f946b6ce6cb841e1cc32b591a590c84f85152ebc15c0c9930
4
- data.tar.gz: a75ffeb0933fa3fa30d80fa89a4eff6da2211420fb04ec2fddf6ea287573fcb2
3
+ metadata.gz: e4eb7179d1b8e0ae007c863c0ff33ee3b7599e5f9d8d105fc5be06afd273f7a8
4
+ data.tar.gz: 391b0849d222beac92d0e98975da2f0eaf66d5bc1fce3285ba26d9ad36007250
5
5
  SHA512:
6
- metadata.gz: 54886d022c1df32ce95b291769248387ef8d3044cda07f7ff9d314e91118c99dfacbe246b878e35f5bb7f8e1d4ddca88e5ebe387143d0eff3b7c446364c4d33c
7
- data.tar.gz: 2437216d2611a1751e86f01c98d19442dbe33d6b21eee654711200dae3528a5000b23832a6b3b30cb91865a697d69605732143f56ba7cb6cb88949d8f27ef202
6
+ metadata.gz: 37f3419b01b9d60979a51e7f3866e2a3b42b87b1988141fe5819de6a76f387c844ebd1f79f44aecdf7e675d13c89fb92681fd21fbf63d78e02737e03ae9b2827
7
+ data.tar.gz: '08ffe7d4519cda519b2dc02bb9fcc3321fdf92ab76480c1a4f0d82b9c9e922aae8103d121b2f3c7649e9926484c9d27030e646d689763db1dc8385ed24abc706'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-html (0.3.0)
4
+ rspec-html (0.3.1)
5
5
  nokogiri (~> 1.10)
6
6
  rspec (~> 3.0)
7
7
 
@@ -21,8 +21,10 @@ GEM
21
21
  i18n (1.13.0)
22
22
  concurrent-ruby (~> 1.0)
23
23
  json (2.6.3)
24
+ mini_portile2 (2.8.2)
24
25
  minitest (5.18.0)
25
- nokogiri (1.13.10-x86_64-linux)
26
+ nokogiri (1.13.10)
27
+ mini_portile2 (~> 2.8.0)
26
28
  racc (~> 1.4)
27
29
  paint (2.3.0)
28
30
  parallel (1.23.0)
data/README.md CHANGED
@@ -27,6 +27,19 @@ require 'rspec/html'
27
27
 
28
28
  Several [matchers](#matchers) are provided to identify text and _HTML_ elements within the _DOM_. These matchers can only be used with the provided [object interface](#object-interface).
29
29
 
30
+ ### Browser Inspection
31
+
32
+ To open the current document in your operating system's default browser, call `document.open`. Use this to inspect _HTML_ content while debugging.
33
+
34
+ ```ruby
35
+ it 'has complex HTML' do
36
+ get '/my/path'
37
+ document.open
38
+ end
39
+ ```
40
+
41
+ Alternatively `document.html_path` writes the current document to a temporary file and returns its path.
42
+
30
43
  ### Object Interface
31
44
  <a name="object-interface"></a>
32
45
 
@@ -47,7 +60,7 @@ This method can also be used for _ActionMailer_ emails:
47
60
  let(:document) { parse_html(ActionMailer::Base.deliveries.last.body.decoded) }
48
61
  ```
49
62
 
50
- **Changed in 0.3.0**: `parse_html` no longer sets the `document` automatically, you must use a `let` block to assign it yourself.
63
+ **Changed in 0.3.0**: `parse_html` no longer assigns `document` automatically, you must use a `let` block to assign it yourself.
51
64
 
52
65
  To navigate the _DOM_ by a sequence of tag names use chained method calls on the `document` object:
53
66
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module HTML
5
- VERSION = '0.3.0'
5
+ VERSION = '0.3.1'
6
6
  end
7
7
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecHTML
4
+ # Convenience utility for loading a provided path in the operating system's default browser.
5
+ # Used for inspecting documents while debugging tests.
6
+ class Browser
7
+ def self.open(path)
8
+ new(path).open
9
+ end
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ end
14
+
15
+ def open
16
+ log_launch_browser_event
17
+ system `#{command} '#{path}'`
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :path
23
+
24
+ def log_launch_browser_event
25
+ warn "\e[35m[rspec-html] Opening document in browser from: \e[32m#{caller[3]}\e[0m"
26
+ end
27
+
28
+ def command
29
+ return 'start' if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
30
+ return 'open' if RUBY_PLATFORM =~ /darwin/
31
+ return 'xdg-open' if RUBY_PLATFORM =~ /linux|bsd/
32
+
33
+ raise ArgumentError, "Unable to detect operating system from #{RUBY_PLATFORM}"
34
+ end
35
+ end
36
+ end
@@ -22,6 +22,16 @@ module RSpecHTML
22
22
  @search = Search.new(@element, @siblings, self)
23
23
  end
24
24
 
25
+ def open
26
+ Browser.open(html_path)
27
+ end
28
+
29
+ def html_path
30
+ @html_path ||= Pathname.new(Dir.mktmpdir('rspec-html')).join('document.html').tap do |path|
31
+ path.write(@element.inner_html)
32
+ end
33
+ end
34
+
25
35
  def present?
26
36
  return true if name == :document
27
37
 
@@ -29,7 +29,11 @@ module RSpecHTML
29
29
  end
30
30
 
31
31
  def regexp_siblings_match?(actual)
32
- actual.siblings.any? { |sibling| @expected.match(sibling&.text || '') }
32
+ matching_siblings = actual.siblings.select { |sibling| @expected.match(sibling&.text || '') }
33
+ @actual_count = matching_siblings.size
34
+ return matching_siblings.size.positive? if @expected_count.nil?
35
+
36
+ matching_siblings.size.positive? && count_match?
33
37
  end
34
38
 
35
39
  def string_match?(actual)
@@ -40,7 +44,11 @@ module RSpecHTML
40
44
  end
41
45
 
42
46
  def string_siblings_match?(actual)
43
- actual.siblings.any? { |sibling| (sibling&.text || '').include?(@expected.to_s) }
47
+ matching_siblings = actual.siblings.select { |sibling| (sibling&.text || '').include?(@expected.to_s) }
48
+ @actual_count = matching_siblings.size
49
+ return matching_siblings.size.positive? if @expected_count.nil?
50
+
51
+ matching_siblings.size.positive? && count_match?
44
52
  end
45
53
 
46
54
  def raise_argument_error
@@ -117,12 +117,12 @@ module RSpecHTML
117
117
 
118
118
  def index(val)
119
119
  zero_index_error if val.zero?
120
- self.class.new(@siblings[val - 1], @element.name, element_wrapper)
120
+ Element.new(@siblings[val - 1], @element.name)
121
121
  end
122
122
 
123
123
  def range(val)
124
124
  zero_index_error if val.first.zero?
125
- self.class.new(@siblings[(val.first - 1)..(val.last - 1)], :range, element_wrapper)
125
+ @siblings[(val.first - 1)..(val.last - 1)].map { |element| Element.new(element, @element.name) }
126
126
  end
127
127
 
128
128
  def zero_index_error
@@ -149,7 +149,10 @@ module RSpecHTML
149
149
 
150
150
  def where_xpath(tag, query)
151
151
  conditions = "[#{where_conditions(query)}]" unless query.compact.empty?
152
- @element&.xpath(".//#{tag}#{conditions}")
152
+ result = @element&.xpath(".//#{tag}#{conditions}")
153
+ return result unless @siblings.is_a?(Nokogiri::XML::NodeSet) && result.empty?
154
+
155
+ @siblings.xpath(".//#{tag}#{conditions}")
153
156
  end
154
157
 
155
158
  def where_conditions(query)
data/lib/rspec_html.rb CHANGED
@@ -4,6 +4,7 @@ require 'nokogiri'
4
4
 
5
5
  require 'pathname'
6
6
  require 'forwardable'
7
+ require 'tempfile'
7
8
 
8
9
  require 'rspec_html/tags'
9
10
  require 'rspec_html/element'
@@ -11,6 +12,7 @@ require 'rspec_html/search'
11
12
  require 'rspec_html/reconstituted_element'
12
13
  require 'rspec_html/countable'
13
14
  require 'rspec_html/matchers'
15
+ require 'rspec_html/browser'
14
16
 
15
17
  # Support module for rspec/html
16
18
  module RSpecHTML
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.3.0
4
+ version: 0.3.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: 2023-05-13 00:00:00.000000000 Z
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -61,6 +61,7 @@ files:
61
61
  - lib/rspec/html.rb
62
62
  - lib/rspec/html/version.rb
63
63
  - lib/rspec_html.rb
64
+ - lib/rspec_html/browser.rb
64
65
  - lib/rspec_html/countable.rb
65
66
  - lib/rspec_html/element.rb
66
67
  - lib/rspec_html/matchers.rb