賽普拉斯 12864_如何使用賽普拉斯自動化輔助功能測試

賽普拉斯 12864

In my previous post, I covered how to add screenshot testing in Cypress to ensure components don't unintentionally change over time.

在上一篇文章中 ,我介紹了如何在賽普拉斯中添加屏幕截圖測試,以確保組件不會隨時間變化。

Now, I will share how to automate accessibility tests with Cypress.

現在,我將分享如何與賽普拉斯自動執行輔助功能測試。

我們為什么要關心可訪問性? (Why should we care about accessibility?)

Short answer: because it is the right thing to do.

簡短答案:因為這是正確的做法。

Long answer: an accessible web can help people with disabilities improve their lives.

長答案:可訪問的網絡可以幫助殘疾人改善生活。

There are different kinds of disabilities, including auditory, cognitive, neurological, physical, speech and visual. And our goal as product creators, engineers, and designers is to create experiences that include all people.

有多種類型的殘疾,包括聽覺,認知,神經,身體,語言和視覺。 我們作為產品創造者,工程師和設計師的目標是創造包括所有人的體驗。

It is also important to mention that web accessibility also benefits people without disabilities.

同樣重要的是要提的是網頁易讀也有利于殘疾人。

For example, accessible sites help people with changing abilities due to ageing or people with slow Internet connections or those using devices with small screens.

例如,可訪問的站點可以幫助因年齡增長或互聯網連接速度緩慢或使用小屏幕設備的人而改變能力。

And a disability can also be temporary. For example, someone with a broken arm can't type and use a mouse at the same time.

殘疾也可以是暫時的。 例如,手臂骨折的人不能同時鍵入和使用鼠標。

If you want to educate yourself about the topic, I can recommend the W3C Web Accessibility Initiative (W3C WAI) and The A11Y Project.

如果您想對這個主題進行自我教育,我可以推薦W3C Web Accessibility Initiative(W3C WAI)和A11Y Project 。

輔助功能測試技術 (Accessibility testing techniques)

There are different ways to test if your website/app is accessible. The W3C WAI has a list of 140+ tools to help you determine if your website/app meets accessibility guidelines.

有多種測試網站/應用程序是否可訪問的方法。 W3C WAI 列出了140多種工具 ,可幫助您確定您的網站/應用是否符合輔助功能準則。

You can also add in your strategy:

您還可以添加策略:

  • Real user testing: companies like Fable connect you and people with disabilities so that your research and user testing can help you meet your compliance goals.

    真實的用戶測試:像Fable這樣的公司將您與殘疾人聯系起來,以便您的研究和用戶測試可以幫助您實現合規性目標。

  • Browser extensions: axe is a recommended extension for Chrome, Firefox, and Edge that helps identify and resolve common accessibility issues.

    瀏覽器擴展: ax是Chrome,Firefox和Edge的推薦擴展,可幫助識別和解決常見的輔助功能問題。

The accessibility engine of axe is open-source and it can be used in different ways, as this post will show.

正如本文將顯示的那樣,ax的可訪問性引擎是開源的 ,可以以不同的方式使用它。

開始之前 (Before we start)

I created a sample website to mimic a Component Library. It is a very simple website created with Tailwind CSS and hosted in Vercel and it documents 2 components: badge and button.

我創建了一個示例網站來模仿組件庫。 這是一個使用Tailwind CSS創建的非常簡單的網站,并托管在Vercel中,它記錄了2個組件: badge和button 。

You can check out the source code on GitHub. The website is static and it is inside the public folder. You can see the website locally by running npm run serve and checking in the browser http://localhost:8000.

您可以在GitHub上查看源代碼 。 該網站是靜態的,并且位于public文件夾中。 您可以通過運行npm run serve并在瀏覽器中查看http:// localhost:8000來在本地查看該網站。

Sample website

添加柏樹和柏樹軸 (Adding Cypress and cypress-axe)

Start by cloning the example repository. Next, create a new branch and install cypress-axe, the package responsible for tying the axe engine to Cypress.

首先克隆示例存儲庫 。 接下來,創建一個新分支,并安裝cypress-axe ,該軟件包負責將斧頭引擎綁定到Cypress。

git checkout -b add-cypress
npm install -D cypress cypress-axe

Next, create a cypress/support/index.js file containing:

接下來,創建一個包含以下內容的cypress/support/index.js文件:

import 'cypress-axe'

This import will inject all the functions we need for our tests.

此導入將注入我們測試所需的所有功能。

創建可訪問性測試 (Creating the accessibility test)

Time to create the accessibility test. Here is the plan:

是時候創建可訪問性測試了。 這是計劃:

  1. Cypress will visit each page (badge and button) of the project.

    賽普拉斯將訪問該項目的每個頁面(徽章和按鈕)。
  2. Cypress will test each example in the page. The Badge page has 2 examples (Default and Pill), while the Button page has 3 examples (Default, Pill and Outline).

    賽普拉斯將測試頁面中的每個示例。 徽章頁面有2個示例(默認和藥丸),而按鈕頁面有3個示例(默認,藥丸和輪廓)。

All these examples are inside an <div> element with a cypress-wrapper. This class was added with the only intention to identify what needs to be tested.

所有這些示例都在帶有cypress-wrapper<div>元素內。 添加該類的唯一目的是確定需要測試的內容。

The first step is creating the Cypress configuration file (cypress.json):

第一步是創建賽普拉斯配置文件( cypress.json ):

{"baseUrl": "http://localhost:8000/","video": false
}

The baseUrl is the website running locally. As I mentioned before, npm run serve will serve the content of the public folder. The second option, video disables Cypress video recording, which won't be used in the project.

baseUrl是在本地運行的網站。 正如我前面提到的, npm run serve將服務內容public文件夾。 第二個選項, video禁用賽普拉斯的視頻記錄,該記錄將不會在項目中使用。

Time to create the test. In cypress/integration/accessibility.spec.js, add:

是時候創建測試了。 在cypress/integration/accessibility.spec.js ,添加:

const routes = ['badge.html', 'button.html'];describe('Component accessibility test', () => {routes.forEach((route) => {const componentName = route.replace('.html', '');const testName = `${componentName} has no detectable accessibility violations on load`;it(testName, () => {cy.visit(route);cy.injectAxe();cy.get('.cypress-wrapper').each((element, index) => {cy.checkA11y('.cypress-wrapper',{runOnly: {type: 'tag',values: ['wcag2a'],},});});});});
});

In the code above, I am creating dynamic tests based in the routes array. The test will check each .cypress-wrapper element against WCAG 2.0 Level A rules.

在上面的代碼中,我正在基于routes數組創建動態測試。 該測試將對照WCAG 2.0 A級規則檢查每個.cypress-wrapper元素。

There are different standards / purposes, as the table below shows:

下表顯示了不同的標準/目的:

Tag NameAccessibility Standard / Purpose
wcag2aWCAG 2.0 Level A
wcag2aaWCAG 2.0 Level AA
wcag21aWCAG 2.1 Level A
wcag21aaWCAG 2.1 Level AA
best-practiceCommon accessibility best practices
wcag***WCAG success criterion e.g. wcag111 maps to SC 1.1.1
ACTW3C approved Accessibility Conformance Testing rules
section508Old Section 508 rules
section508.*.*Requirement in old Section 508
標簽名 無障礙標準/目的
wcag2a WCAG 2.0 A級
wcag2aa WCAG 2.0 AA級
wcag21a WCAG 2.1 A級
wcag21aa WCAG 2.1 AA級
best-practice 通用無障礙最佳做法
wcag*** WCAG成功標準,例如wcag111映射到SC 1.1.1
ACT W3C批準的可訪問性一致性測試規則
section508 舊版508條規定
section508.*.* 舊版508節的要求

You can find more information about it in the axe-core docs.

您可以在axe-core docs中找到有關它的更多信息。

Last, let's create inside the package.json command to trigger the tests:

最后,讓我們在package.json命令內部創建以觸發測試:

{"test": "cypress"
}

From here, there are 2 options: run Cypress in headless mode with npm run cypress run or use the Cypress Test Runner with npm run cypress open.

從這里開始,有2個選項:使用npm run cypress run在無頭模式下運行Cypress或在npm run cypress open使用Cypress Test Runner。

無頭選項 (Headless option)

Using npm run test run, the output should be similar to the next image:

使用npm run test run ,輸出應該類似于下一個圖像:

Output of first test

The tests will pass since the components have no accessibility issues.

由于組件沒有可訪問性問題,因此測試將通過。

測試運行器選項 (Test Runner option)

Using npm run test open, Cypress Test Runner will be opened and you can follow step by step the tests.

使用npm run test open ,賽普拉斯Test Runner將被打開,您可以逐步進行測試。

Cypress Test Runner screenshot

Our first milestone is done. Let's merge this branch to master. If you want to see the work done so far, jump in my Pull Request.

我們的第一個里程碑已經完成。 讓我們將該分支合并到master。 如果您想查看到目前為止已完成的工作,請進入我的請求請求 。

現實生活中的測試 (Testing in real life)

Imagine we are updating the website and we want to identify the buttons with ids. The id property must be unique in the document, as we can't have 2 elements with the same one. But we forgot about that and 3 buttons have the same id.

想象一下,我們正在更新網站,并且想要標識具有ID的按鈕。 id屬性在文檔中必須是唯一的,因為我們不能有2個元素具有相同的元素。 但是我們忘記了這一點,并且3個按鈕具有相同的ID。

Cypress will fail and the output will look something like this:

賽普拉斯將失敗,并且輸出將類似于以下內容:

Output of failed test

Let's improve the output of our tests by showing a table of found issues. First, let's create a new branch:

通過顯示已發現問題的表格,讓我們改善測試的輸出。 首先,讓我們創建一個新分支:

git checkout -b improve-cypress-tests

Next, create the cypress/plugins/index.js file with the following content:

接下來,使用以下內容創建cypress/plugins/index.js文件:

module.exports = (on, config) => {on('task', {log(message) {console.log(message)return null},table(message) {console.table(message)return null}})
}

This will execute code in Node via the task plugin event of Cypress. Next, let's go back to the accessibility.spec.js file and add the following function in the top of the file:

這將通過賽普拉斯的task插件事件在Node中執行代碼。 接下來,讓我們回到accessibility.spec.js文件,并在文件頂部添加以下功能:

const terminalLog = (violations) => {cy.task('log',`${violations.length} accessibility violation${violations.length === 1 ? '' : 's'} ${violations.length === 1 ? 'was' : 'were'} detected`)// pluck specific keys to keep the table readableconst violationData = violations.map(({ id, impact, description, nodes }) => ({id,impact,description,nodes: nodes.length}))cy.task('table', violationData)
}

The violations array contains all information about the failing elements. You can tweak this code to include, for instance, the element source code in the test output.

violations數組包含有關失敗元素的所有信息。 您可以調整此代碼以在測試輸出中包括例如元素源代碼。

Last, let's call the function inside the tests. Modify the checkA11y function to:

最后,讓我們在測試中調用該函數。 將checkA11y函數修改為:

cy.checkA11y('.cypress-wrapper',{runOnly: {type: 'tag',values: ['wcag2a'],},},terminalLog,
);

When you run the test again, you'll get a table containing the issues reported by axe:

再次運行測試時,您將獲得一個包含斧頭報告的問題的表:

Output of failed test with a nice table

If you have any questions, please check the Pull request in Github.

如有任何疑問,請檢查Github中的Pull請求 。

下一步 (Next steps)

From here, you can continue your journey toward making products more accessible. As some next steps, I would recommend:

從這里開始,您可以繼續努力,使產品更易于使用。 作為下一步,我建議:

  • Adding these tests in your CI solution - I wrote about integrating Cypress inside Semaphore

    在您的CI解決方案中添加這些測試-我寫過有關將Cypress集成到Semaphore中的信息

  • Finding the best way to customize the output of tests to improve troubleshooting issues

    尋找自定義測試輸出以改善故障排除問題的最佳方法
  • Learning more about how axe works.

    了解有關斧頭工作原理的更多信息。

I hope that you have learned that accessibility testing is not difficult and it doesn't require much to start. Automation powered by axe can definitely help us to create better user experiences for all people.

我希望您了解到可訪問性測試并不困難,并且不需要太多的開始。 由斧頭驅動的自動化絕對可以幫助我們為所有人創造更好的用戶體驗。

--

-

Questions? Comments? This post is also in my blog. If you like this content, follow me on Twitter and GitHub.

有什么問題嗎 注釋? 這篇文章也在我的博客中 。 如果您喜歡此內容,請在Twitter和GitHub上關注我。

翻譯自: https://www.freecodecamp.org/news/automating-accessibility-tests-with-cypress/

賽普拉斯 12864

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390170.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390170.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390170.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

anaconda在win下和在mac下的安裝區別

1. 在win下安裝anaconda后會提示你選擇環境變量&#xff0c;但是建議使用默認。 于是CMD進入終端和使用navigator進入終端不一樣&#xff0c;前者會提示無此命令&#xff0c;只能通過navigator進入終端 即使在系統變量變量Path里添加了路徑&#xff0c;使用CMD還是不能使用pyth…

fcn從頭開始_如何使用Go從頭開始構建區塊鏈

fcn從頭開始介紹 (Introduction) With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?隨著Web 3.0和區塊鏈每天變得越來越主流&#xff0c;您知道什么是區塊鏈嗎&#x…

java實現無序數組結構

一、數組的2種定義方式 數據類型 [] 數組名稱 new 數據類型[數組長度]; 這里 [] 可以放在數組名稱的前面&#xff0c;也可以放在數組名稱的后面&#xff0c;一般放在名稱的前面 數據類型 [] 數組名稱 {數組元素1&#xff0c;數組元素2&#xff0c;......} 這種方式聲明數組的…

Android App 的主角:Activity

Android App 程序主要由4種類型組成&#xff1a; 1.Activity&#xff08;活動&#xff09;&#xff1a;主要負責屏幕顯示畫面&#xff0c;并處理與用戶的互動。每個Android App至少都會有一個Activity&#xff0c;在程序一啟動時顯示主畫面供用戶操作。 2.Service&#xff08;后…

通過構建Paint App學習React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 據知情人士稱&#xff0c;React Hooks很熱&…

正則表達式 匹配常用手機號 (13、15\17\18開頭的十一位手機號)

原文:正則表達式 匹配常用手機號 &#xff08;13、15\17\18開頭的十一位手機號&#xff09;^1[3578]\d{9}$ ^1表示以1開頭&#xff0c;[3578]表示第二位的數字為3578中的任意一個&#xff0c;\d{9}表示0~9范圍內的數字匹配九次,$表示結束&#xff0c;12位以上的數字不匹配。

Npoi導出excel整理(附源碼)

前些日子做了一個簡單的winform程序&#xff0c;需要導出的功能&#xff0c;剛開始省事直接使用微軟的組件&#xff0c;但是導出之后發現效率極其低下&#xff0c;絕對像web那樣使用npoi組件&#xff0c;因此簡單的進行了整理&#xff0c;包括直接根據DataTable導出excel及Data…

44. 通配符匹配

44. 通配符匹配 給定一個字符串 (s) 和一個字符模式 &#xff0c;實現一個支持 ‘?’ 和 ‘*’ 的通配符匹配。 ? 可以匹配任何單個字符。 * 可以匹配任意字符串&#xff08;包括空字符串&#xff09;。 兩個字符串完全匹配才算匹配成功。說明: s 可能為空&#xff0c;且…

遞歸javascript_使用freeCodeCamp挑戰解釋了JavaScript中的遞歸

遞歸javascriptIn this article I will touch on a few important ideas to help you understand Recursion in JavaScript. I’m not going to give a full definition here, but you can take a look at what Wikipedia has to say. 在本文中&#xff0c;我將介紹一些重要的想…

入庫成本與目標成本對比報表中我學到的東西

1、SQL方面&#xff1a; &#xff08;1&#xff09;、用DECODE函數解決除數為零的情況 具體語法&#xff1a; DECODE&#xff08;參數&#xff0c;0&#xff0c;1&#xff0c;參數&#xff09; ->DECODE(TAB1.A8&#xff0c;0&#xff0c;1&#xff0c;TAB1.A8) &#xff08…

J - Borg Maze

J - Borg Maze 思路&#xff1a;bfs最小生成樹。#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int fa[MAXN]; struct nond{int x,y,z; }v[MAXN*MAXN]; s…

1095. 山脈數組中查找目標值

1095. 山脈數組中查找目標值 &#xff08;這是一個 交互式問題 &#xff09; 給你一個 山脈數組 mountainArr&#xff0c;請你返回能夠使得 mountainArr.get(index) 等于 target 最小 的下標 index 值。 如果不存在這樣的下標 index&#xff0c;就請返回 -1。 何為山脈數組…

【小摘抄】關于C++11下 string各類用法(持續更新)

http://blog.csdn.net/autocyz/article/details/42391155 提供了最簡單的詳解 下列對本人近期開發中的一些心得體會進行摘抄 1.string按照字符進行截取 示例代碼&#xff1a; string teststring "#12313#kajlkfdsa";//通訊消息示例&#xff0c;結合string的內置函數…

sql綜合練習題

一、表關系 年級表&#xff1a;class_grade create table class_grade(gid int primary key auto_increment,gname varchar(20) not null); insert into class_grade(gname) values(一年級),(二年級),(三年級); 班級表&#xff1a;class create table class(cid int primary ke…

javascript原型_在JavaScript中凍結原型時會發生什么

javascript原型Have you wondered what happens when you freeze the prototype of an object? Lets find out together.您是否想過凍結對象的原型時會發生什么&#xff1f; 讓我們一起找出答案。 對象 (Objects) In JavaScript, objects are dynamic collections of propert…

遲來的2017總結

明天就是年后第一天上班了&#xff08;過年期間請了6天假&#xff09;&#xff0c; 打算今天寫一下2017的總結&#xff0c;本來還想寫2018的愿景的&#xff0c;不過想想還是算了&#xff0c;現在沒什么想法&#xff0c;不想敷衍了事。 先貼一個2017的提升計劃&#xff1a; http…

tomcat啟動卡住

新部署的項目啟動tomcat后一直停在org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.16&#xff0c;卡在了org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/web…

怎樣準備阿里技術面試_如何準備技術面試

怎樣準備阿里技術面試In June 2020 I watched an inspiring talk by Anthony D. Mays, a technical coach and founder at Morgan Latimerco. He came on a Facebook Developer Circles Benin live session and talked about how to prepare for a technical interview. 2020年…

通過一個簡單例子理解 RecyclerView.ItemDecoration

一、前言 RecyclerView 是從5.0推出的 MD 風格的控件。RecyclerView 之前有 ListView、GridView&#xff0c;但是功能很有限&#xff0c;例如 ListView 只能實現垂直方向上的滑動等。但是存在則合理&#xff0c;ListView 卻沒有被官方標記為 Deprecated&#xff0c;有興趣的同學…

Entity Framework Logging and Intercepting Database Operations (EF6 Onwards)

參考官方文檔&#xff1a;https://msdn.microsoft.com/en-us/library/dn469464(vvs.113).aspx轉載于:https://www.cnblogs.com/liandy0906/p/8473110.html