カスタムルール

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard
Not for use with personal data

カスタムルール機能を利用すると、新しいルールを作成し、それに対してテストを行い、既存のルールの主要な動作を変更することができます。

ビューの名前プロパティをチェックするカスタムルールを作成しましょう。結果基準は以下の通りです:

  • 適用不可: 名前がnullである
  • 失敗: 名前がnullでないが空である
  • 成功: 名前がnullでなく、かつ何らかの長さがある

ルールの作成

  1. 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)
        }
    }
  2. 次に、テストを行うためにビューのプロパティを収集する必要があります。私たちの例では、名前プロパティにアクセスする必要があります。

   override func collectProps(for node: AxeView) -> AxeProps {
        let axeProps = AxeProps()
        axeProps.add([
            .name: node.name
        ])

        return axeProps
   }
  1. 最後に、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
        }
    }
}