カスタムルール
Not for use with personal data
カスタムルール機能を利用すると、新しいルールを作成し、それに対してテストを行い、既存のルールの主要な動作を変更することができます。
ビューの名前プロパティをチェックするカスタムルールを作成しましょう。結果基準は以下の通りです:
- 適用不可: 名前がnullである
- 失敗: 名前がnullでないが空である
- 成功: 名前がnullでなく、かつ何らかの長さがある
ルールの作成
-
から継承した新しいクラスオブジェクトを設定します。 初期化子内で、次の事項を指定するためにルール設定を行うことができます:
AxeRule.- どの基準に対してテストを行っているか
- 影響
- ルールの概要
class CustomRule: AxeRule { public init() { let ruleConfiguration = RuleConf(standard: AxeStandard.BEST_PRACTICE, impact: AxeImpact.MODERATE, summary: "Asserts that when name is present it is not empty.") super.init(ruleConf: ruleConfiguration) } } -
次に、テストを行うためにビューのプロパティを収集する必要があります。私たちの例では、名前プロパティにアクセスする必要があります。
override func collectProps(for node: AxeView) -> AxeProps {
let axeProps = AxeProps()
axeProps.add([
.name: node.name
])
return axeProps
}
- 最後に、
runRule関数で、テストを行う基準を指定し、ルールが成功したか失敗したか、または適用不能とみなされるかを決定します。
override func runRule(_ props: AxeProps) throws -> AxeStatus {
guard let name = props.get(.name, withType: String.self) else { return .INAPPLICABLE }
if !name.isEmpty {
return AxeStatus.PASS
} else {
return AxeStatus.FAIL
}
}axe DevTools への追加
カスタムルールのロジックを書いたら、それをaxe DevToolsが認識するルールリストに追加します。設定は、テストを実行する前および認証後に行う必要があります。
axe?.configuration.customRules.insert(CustomRule())完全な例
class CustomRule: AxeRule {
public init() {
let ruleConfiguration = RuleConf(standard: AxeStandard.BEST_PRACTICE,
impact: AxeImpact.MODERATE,
summary: "Asserts that when name is present it is not empty.")
super.init(ruleConf: ruleConfiguration)
}
override func collectProps(for node: AxeView) -> AxeProps {
let axeProps = AxeProps()
axeProps.add([
.name: node.name
])
return axeProps
}
override func runRule(_ props: AxeProps) throws -> AxeStatus {
guard let name = props.get(.name, withType: String.self) else { return .INAPPLICABLE }
if !name.isEmpty {
return AxeStatus.PASS
} else {
return AxeStatus.FAIL
}
}
}
