Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Resolve the Capybara session when the audit runs, so that a suite that calls
`driven_by` from its own `before` hook audits through the selected driver
- Send the audit options to the browser as JSON values, so that the
`:playwright` driver accepts `according_to` and `checking_only`
- Add support for `:cuprite` and `:playwright` system test drivers
Expand Down
14 changes: 9 additions & 5 deletions lib/capybara_accessibility_audit/axe_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module CapybaraAccessibilityAudit
class AxeAuditor
class_attribute :source, instance_accessor: false, default: Axe::Configuration.instance.jslib

def initialize(page, reporter)
@page = page
def initialize(test, reporter)
@test = test
@reporter = reporter
end

Expand All @@ -22,10 +22,14 @@ def audit(**options)

private

def page
@test.page
end

def run(config)
context, options = split(config)

@page.evaluate_async_script <<~JS, context.as_json, options.as_json
page.evaluate_async_script <<~JS, context.as_json, options.as_json
const [ context, options, callback ] = arguments

axe.run(context, options).then(callback)
Expand All @@ -47,11 +51,11 @@ def split(config)
end

def install
@page.execute_script(self.class.source) unless installed?
page.execute_script(self.class.source) unless installed?
end

def installed?
@page.evaluate_script <<~JS
page.evaluate_script <<~JS
"axe" in window && typeof axe.run === "function"
JS
end
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara_accessibility_audit/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Engine < ::Rails::Engine
auditor_class = app.config.capybara_accessibility_audit.auditor
reporter_class = CapybaraAccessibilityAudit.reporter_class(accessibility_audit_reporter)

self.accessibility_audit_auditor = auditor_class.new(page, reporter_class.new(self))
self.accessibility_audit_auditor = auditor_class.new(self, reporter_class.new(self))
end
end
end
Expand All @@ -46,7 +46,7 @@ class Engine < ::Rails::Engine
auditor_class = app.config.capybara_accessibility_audit.auditor
reporter_class = CapybaraAccessibilityAudit.reporter_class(accessibility_audit_reporter)

self.accessibility_audit_auditor = auditor_class.new(page, reporter_class.new(self))
self.accessibility_audit_auditor = auditor_class.new(self, reporter_class.new(self))
end

config.before(type: :system, &configure)
Expand Down
11 changes: 11 additions & 0 deletions spec/system/driven_by_in_a_before_hook_spec.rb

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this coverage possible to achieve from a Minitest System Test? The presence of the RSpec suite is solely for the purpose of exercising the RSpec-facing public interface of the gem.

The rest of the internal test suite is written in Minitest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a minitest test, but kept this one to also test the RSpec hook ordering thing. LMK if you think this is unnecessary.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

RSpec.describe "Audit assertions", type: :system do
before { driven_by Capybara.javascript_driver }

it "audits with the session that is current when the audit runs" do
assert_rule_violation "label: Form elements must have labels" do
visit violations_path(rules: %w[label])
end
end
end
6 changes: 6 additions & 0 deletions test/system/audit_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class AuditAssertionsTest < ApplicationSystemTestCase
end
end

test "flunks on violations detected within #using_session" do
assert_rule_violation "label: Form elements must have labels" do
using_session(:other) { visit violations_path(rules: %w[label]) }
end
end

test "ignores violations within a matching skip_accessibility_violations block" do
skip_accessibility_violations("label") { visit violations_path(rules: %w[label]) }
skip_accessibility_violations(%w[label]) { visit violations_path(rules: %w[label]) }
Expand Down
Loading