Class: AxeCuprite::RSpec::BeAxeClean

Inherits:
Object
  • Object
show all
Defined in:
lib/axe/cuprite/rspec/matchers.rb

Overview

The matcher behind ‘be_axe_clean` / `be_accessible`.

expect(page).to be_axe_clean
expect(page).to be_axe_clean.checking_only(:color_contrast).within("#main")
expect(page).to be_axe_clean.according_to(:wcag2aa).excluding(".third-party")
expect(page).to be_axe_clean.skipping(:region)

All chainers return self so they compose in any order.

Constant Summary collapse

MAX_NODES =

Cap how many offending elements we print per rule, to keep failure messages readable on noisy pages.

5
HTML_SNIPPET =

Cap HTML snippet length per node.

200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeAxeClean

Returns a new instance of BeAxeClean.



23
24
25
26
27
28
29
30
31
# File 'lib/axe/cuprite/rspec/matchers.rb', line 23

def initialize
  @includes   = []
  @excludes   = []
  @only_rules = []
  @skip_rules = []
  @tags       = []
  @run_options = {}
  @timeout    = nil
end

Instance Attribute Details

#resultsObject (readonly)

Exposed so callers/tests can inspect the full result after matching.



111
112
113
# File 'lib/axe/cuprite/rspec/matchers.rb', line 111

def results
  @results
end

#violationsObject (readonly)

Exposed so callers/tests can inspect the full result after matching.



111
112
113
# File 'lib/axe/cuprite/rspec/matchers.rb', line 111

def violations
  @violations
end

Instance Method Details

#according_to(*tags) ⇒ Object

Restrict the run to axe tags, e.g. :wcag2aa, :wcag21aa, :best_practice.



60
61
62
63
# File 'lib/axe/cuprite/rspec/matchers.rb', line 60

def according_to(*tags)
  @tags.concat(tags.flatten)
  self
end

#checking_only(*rules) ⇒ Object

Run ONLY these rules. Accepts symbols or axe ids; underscores -> hyphens.



48
49
50
51
# File 'lib/axe/cuprite/rspec/matchers.rb', line 48

def checking_only(*rules)
  @only_rules.concat(rules.flatten)
  self
end

#descriptionObject



106
107
108
# File 'lib/axe/cuprite/rspec/matchers.rb', line 106

def description
  "be free of axe-core accessibility violations"
end

#does_not_match?(page) ⇒ Boolean

Negation ignores report_only: ‘to_not be_axe_clean` passes iff there really are violations.

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/axe/cuprite/rspec/matchers.rb', line 92

def does_not_match?(page)
  run!(page)
  !@violations.empty?
end

#excluding(*selectors) ⇒ Object

Exclude one or more selectors from the run (axe context “exclude”).



42
43
44
45
# File 'lib/axe/cuprite/rspec/matchers.rb', line 42

def excluding(*selectors)
  @excludes.concat(selectors.flatten)
  self
end

#failure_messageObject



97
98
99
# File 'lib/axe/cuprite/rspec/matchers.rb', line 97

def failure_message
  "#{summary_line}\n\n#{details}"
end

#failure_message_when_negatedObject



101
102
103
104
# File 'lib/axe/cuprite/rspec/matchers.rb', line 101

def failure_message_when_negated
  "expected page to have axe-core accessibility violations, but it was clean " \
    "(0 violations for the selected rules/scope)."
end

#matches?(page) ⇒ Boolean

— RSpec protocol —————————————————

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
# File 'lib/axe/cuprite/rspec/matchers.rb', line 79

def matches?(page)
  run!(page)

  if config.report_only && !@violations.empty?
    config.logger.warn("report_only: #{summary_line}\n#{details}")
    return true
  end

  @violations.empty?
end

#skipping(*rules) ⇒ Object

Disable these rules for this run (merged with the global skip-list).



54
55
56
57
# File 'lib/axe/cuprite/rspec/matchers.rb', line 54

def skipping(*rules)
  @skip_rules.concat(rules.flatten)
  self
end

#with_options(options) ⇒ Object

Escape hatch: merge raw axe run options (deep-merged, wins on conflict).



66
67
68
69
# File 'lib/axe/cuprite/rspec/matchers.rb', line 66

def with_options(options)
  @run_options = deep_merge(@run_options, options)
  self
end

#with_timeout(seconds) ⇒ Object

Override the axe timeout (seconds) for this assertion only.



72
73
74
75
# File 'lib/axe/cuprite/rspec/matchers.rb', line 72

def with_timeout(seconds)
  @timeout = seconds
  self
end

#within(*selectors) ⇒ Object

Scope the run to one or more selectors (axe context “include”).



36
37
38
39
# File 'lib/axe/cuprite/rspec/matchers.rb', line 36

def within(*selectors)
  @includes.concat(selectors.flatten)
  self
end