Created
July 17, 2017 11:51
-
-
Save elomatreb/0f6673f5fca7f98483cac9c5c4a7e9f5 to your computer and use it in GitHub Desktop.
Duct-tape HTML testing. No capybaras were required and/or harmed during development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "nokogiri" | |
RSpec::Matchers.define :have_elements_matching do |selector| | |
match do |view| | |
Nokogiri::HTML(view).match(selector).any? | |
end | |
end | |
RSpec::Matchers.define :have_one_element_matching do |selector| | |
match do |view| | |
Nokogiri::HTML(view).match(selector).size == 1 | |
end | |
end | |
RSpec::Matchers.define :have_input do |name| | |
match do |view| | |
# Workaround for Nokogiri weirdness since it apparently doesn't understand | |
# escaped CSS selectors | |
Nokogiri::HTML(view).css("input").any? { |e| e["name"] == name } | |
end | |
end | |
# Type-specific matchers lol | |
%w[text email password].each do |type| | |
RSpec::Matchers.define :"have_#{type}_input" do |name = nil| | |
match do |view| | |
# See above | |
if name | |
Nokogiri::HTML(view).css("input").any? { |e| e["name"] == name && e["type"] == type } | |
else | |
Nokogiri::HTML(view).css("input").any? { |e| e["type"] == type } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment