Skip to content
Draft
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
23 changes: 19 additions & 4 deletions lib/capybara_accessibility_audit/axe_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AxeAuditor
class_attribute :source, instance_accessor: false, default: Axe::Configuration.instance.jslib

def initialize(page, reporter)
@page = page
@page_proc = page.is_a?(Proc) ? page : -> { page }
@reporter = reporter
end

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

private

def page
@page_proc.call
end

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

@page.evaluate_async_script <<~JS, context.to_h, options.to_h
# Convert to JSON-compatible hashes (all symbols become strings)
# Playwright driver can’t serialize Ruby symbols, e.g.
#
# accessibility_audit_options.according_to = [
# :wcag2a,
# :wcag2aa
# ]
#
context_hash = context.to_h.as_json
options_hash = options.to_h.as_json

page.evaluate_async_script <<~JS, context_hash, options_hash
const [ context, options, callback ] = arguments

axe.run(context, options).then(callback)
Expand All @@ -47,11 +62,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(-> { page }, 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(-> { page }, reporter_class.new(self))
end

config.before(type: :system, &configure)
Expand Down
Loading