Regole personalizzate

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

La funzionalità delle regole personalizzate consente di creare nuove regole da testare e di modificare i comportamenti chiave all'interno delle regole esistenti.

Creiamo una regola personalizzata che controlla la proprietà nome di una vista. I criteri di risultato saranno i seguenti:

  • Non applicabile: il nome è nullo
  • Fallimento: il nome non è nullo ma è vuoto
  • Successo: il nome non è nullo e ha una certa lunghezza

Creazione della regola

  1. Imposta un nuovo oggetto di classe che eredita da AxeRule. All'interno dell'inizializzatore è possibile impostare la configurazione della regola per specificare quanto segue:

    • Con quale standard stai effettuando il test
    • L'impatto
    • Un riassunto della regola
    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. Successivamente, dovrai raccogliere le proprietà della vista da testare. Per il nostro esempio, dobbiamo accedere alla proprietà name.

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

        return axeProps
   }
  1. Infine, specificheremo nella funzione su cosa stiamo eseguendo il test, per determinare se la regola ha avuto successo, ha fallito o è considerata inapplicabile. 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
        }
    }

Aggiungi a axe DevTools

Dopo aver scritto la logica per la tua regola personalizzata, aggiungila all'elenco delle regole note axe DevTools modificando la configurazione. È necessario impostare le configurazioni prima di eseguire qualsiasi test e dopo l'autenticazione.

axe?.configuration.customRules.insert(CustomRule())

Esempio completo

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
        }
    }
}