Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Fix flaky audio a11y tests (#1568)

sarayourfriendeda35c38c73a
diff --git a/src/components/VAudioTrack/VAudioTrack.vue b/src/components/VAudioTrack/VAudioTrack.vue
index ca10c7d60..18f5137ba 100644
--- a/src/components/VAudioTrack/VAudioTrack.vue
+++ b/src/components/VAudioTrack/VAudioTrack.vue
@@ -65,6 +65,7 @@ import {
AudioLayout,
AudioSize,
AudioStatus,
+ activeAudioStatus,
layoutMappings,
} from '~/constants/audio'
@@ -192,7 +193,7 @@ export default defineComponent({
const updateTimeLoop = () => {
if (localAudio) {
- if (status.value === 'playing' || status.value === 'loading') {
+ if (activeAudioStatus.includes(status.value)) {
currentTime.value = localAudio.currentTime
window.requestAnimationFrame(updateTimeLoop)
} else {
@@ -334,7 +335,11 @@ export default defineComponent({
/* Interface with VPlayPause */
- const handleToggle = (state?: 'playing' | 'paused') => {
+ /**
+ * This function can safely ignore the `loading` status because
+ * that status is never toggled _to_.
+ */
+ const handleToggle = (state?: 'playing' | 'paused' | 'played') => {
if (!state) {
switch (status.value) {
case 'playing':
@@ -420,7 +425,9 @@ export default defineComponent({
const handleSpace = (event: KeyboardEvent) => {
if (!isBoxed.value) return
event.preventDefault()
- status.value = status.value === 'playing' ? 'paused' : 'playing'
+ status.value = activeAudioStatus.includes(status.value)
+ ? 'paused'
+ : 'playing'
handleToggle(status.value)
}
diff --git a/src/components/VAudioTrack/VPlayPause.vue b/src/components/VAudioTrack/VPlayPause.vue
index 4ff8f8cce..00e2d4b8c 100644
--- a/src/components/VAudioTrack/VPlayPause.vue
+++ b/src/components/VAudioTrack/VPlayPause.vue
@@ -31,7 +31,7 @@
<script lang="ts">
import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
-import type { AudioLayout, AudioStatus } from '~/constants/audio'
+import { AudioLayout, AudioStatus, statusVerbMap } from '~/constants/audio'
import { defineEvent } from '~/types/emits'
import VIconButton from '~/components/VIconButton/VIconButton.vue'
@@ -41,13 +41,6 @@ import playIcon from '~/assets/icons/play.svg'
import pauseIcon from '~/assets/icons/pause.svg'
import replayIcon from '~/assets/icons/replay.svg'
-const statusVerbMap = {
- playing: 'pause',
- paused: 'play',
- played: 'replay',
- loading: 'loading',
-} as const
-
const statusIconMap = {
playing: pauseIcon,
paused: playIcon,
diff --git a/src/constants/audio.ts b/src/constants/audio.ts
index 06764f1e1..43fff5058 100644
--- a/src/constants/audio.ts
+++ b/src/constants/audio.ts
@@ -1,16 +1,51 @@
-export const audioLayouts = ['full', 'box', 'row', 'global'] as const
-export const audioSizes = ['s', 'm', 'l'] as const
-export const audioStatuses = ['playing', 'paused', 'played', 'loading'] as const
-export const audioFeatures = ['timestamps', 'duration', 'seek'] as const
-
export type AudioLayout = typeof audioLayouts[number]
+export const audioLayouts = Object.freeze([
+ 'full',
+ 'box',
+ 'row',
+ 'global',
+] as const)
+
export type AudioSize = typeof audioSizes[number]
+export const audioSizes = Object.freeze(['s', 'm', 'l'] as const)
+
export type AudioStatus = typeof audioStatuses[number]
+export const audioStatuses = Object.freeze([
+ 'playing',
+ 'paused',
+ 'played',
+ 'loading',
+] as const)
+
export type AudioFeature = typeof audioFeatures[number]
+export const audioFeatures = Object.freeze([
+ 'timestamps',
+ 'duration',
+ 'seek',
+] as const)
-export const layoutMappings = {
+export const inactiveAudioStatus: readonly AudioStatus[] = Object.freeze([
+ 'paused',
+ 'played',
+])
+export const activeAudioStatus: readonly AudioStatus[] = Object.freeze([
+ 'playing',
+ 'loading',
+])
+
+export const layoutMappings = Object.freeze({
full: 'VFullLayout',
row: 'VRowLayout',
box: 'VBoxLayout',
global: 'VGlobalLayout',
-}
+})
+
+export const statusVerbMap = Object.freeze({
+ playing: 'pause',
+ paused: 'play',
+ played: 'replay',
+ loading: 'loading',
+} as const)
+
+export const audioStatusVerbs = Object.values(statusVerbMap)
+export type AudioStatusVerb = typeof audioStatusVerbs[number]
diff --git a/test/playwright/e2e/all-results-keyboard.spec.ts b/test/playwright/e2e/all-results-keyboard.spec.ts
index 2b4e81289..501349717 100644
--- a/test/playwright/e2e/all-results-keyboard.spec.ts
+++ b/test/playwright/e2e/all-results-keyboard.spec.ts
@@ -1,13 +1,10 @@
import { test, expect, Page } from '@playwright/test'
-import { keycodes } from '~/constants/key-codes'
+import audio from '~~/test/playwright/utils/audio'
-const walkToType = async (type: 'image' | 'audio', page: Page) => {
- // Go to skip to content button
- await page.keyboard.press(keycodes.Tab)
- // Skip to content
- await page.keyboard.press(keycodes.Enter)
+import { keycodes } from '~/constants/key-codes'
+const walkToNextOfType = async (type: 'image' | 'audio', page: Page) => {
const isActiveElementOfType = () => {
return page.evaluate(
([contextType]) =>
@@ -26,6 +23,15 @@ const walkToType = async (type: 'image' | 'audio', page: Page) => {
}
}
+const walkToType = async (type: 'image' | 'audio', page: Page) => {
+ // Go to skip to content button
+ await page.keyboard.press(keycodes.Tab)
+ // Skip to content
+ await page.keyboard.press(keycodes.Enter)
+
+ await walkToNextOfType(type, page)
+}
+
const locateFocusedResult = async (page: Page) => {
const href = await page.evaluate(
() => (document.activeElement as HTMLAnchorElement | null)?.href
@@ -74,15 +80,13 @@ test.describe('all results grid keyboard accessibility test', () => {
}) => {
await walkToType('audio', page)
const focusedResult = await locateFocusedResult(page)
- const playButton = focusedResult.locator('[aria-label="Play"]')
+ const playButton = await audio.getInactive(focusedResult)
await playButton.click()
-
// should not navigate
expect(page.url()).toMatch(/\/search\?q=birds$/)
- const pauseButton = focusedResult.locator('[aria-label="Pause"]')
- await expect(pauseButton).toBeVisible()
- await pauseButton.click()
+ const pauseButton = await audio.getActive(focusedResult)
+ pauseButton.click()
await expect(playButton).toBeVisible()
})
@@ -92,8 +96,27 @@ test.describe('all results grid keyboard accessibility test', () => {
await walkToType('audio', page)
await page.keyboard.press(keycodes.Spacebar)
const focusedResult = await locateFocusedResult(page)
- await expect(focusedResult.locator('[aria-label="Pause"]')).toBeVisible()
+ await expect(await audio.getActive(focusedResult)).toBeVisible()
await page.keyboard.press(keycodes.Spacebar)
- await expect(focusedResult.locator('[aria-label="Play"]')).toBeVisible()
+ await expect(await audio.getInactive(focusedResult)).toBeVisible()
+ })
+
+ test('should pause audio after playing another', async ({ page }) => {
+ await walkToType('audio', page)
+ const focusedResult = await locateFocusedResult(page)
+ const playButton = await audio.getInactive(focusedResult)
+ await playButton.click()
+ const pauseButton = await audio.getActive(focusedResult)
+
+ await page.keyboard.press(keycodes.Tab)
+ await walkToNextOfType('audio', page)
+
+ const nextFocusedResult = await locateFocusedResult(page)
+ const nextPlayButton = await audio.getInactive(nextFocusedResult)
+ await nextPlayButton.click()
+ await audio.getActive(nextFocusedResult)
+
+ await expect(playButton).toBeVisible()
+ await expect(pauseButton).not.toBeVisible()
})
})
diff --git a/test/playwright/utils/audio.ts b/test/playwright/utils/audio.ts
new file mode 100644
index 000000000..397a89467
--- /dev/null
+++ b/test/playwright/utils/audio.ts
@@ -0,0 +1,72 @@
+import { expect, Locator } from '@playwright/test'
+
+import {
+ activeAudioStatus,
+ inactiveAudioStatus,
+ statusVerbMap,
+ AudioStatusVerb,
+} from '~/constants/audio'
+
+const getPossibleAudioActions = async (
+ context: Locator,
+ options: {
+ state: AudioStatusVerb[]
+ filterVisible?: boolean
+ }
+) => {
+ return (
+ await Promise.all(
+ options.state.map(async (s) => {
+ const locator = context.locator(
+ `[aria-label="${s[0].toUpperCase() + s.slice(1)}"]`
+ )
+ const isVisible = await locator.isVisible().catch(() => false)
+ return [locator, isVisible] as const
+ })
+ )
+ )
+ .filter(options.filterVisible ? ([, visible]) => visible : (x) => x)
+ .map(([l]) => l)
+}
+
+const getAllActive = async (
+ context: Locator,
+ options: { filterVisible?: boolean } = {}
+) => {
+ const locators = await getPossibleAudioActions(context, {
+ ...options,
+ state: activeAudioStatus.map((a) => statusVerbMap[a]),
+ })
+ expect(locators, 'Could not find a pause or loading button.').toHaveLength(
+ options.filterVisible ? 1 : 2
+ )
+ return locators
+}
+
+const getActive = async (context: Locator) =>
+ (await getAllActive(context, { filterVisible: true }))[0]
+
+const getAllInactive = async (
+ context: Locator,
+ options: { filterVisible?: boolean } = {}
+) => {
+ const locators = await getPossibleAudioActions(context, {
+ ...options,
+ state: inactiveAudioStatus.map((a) => statusVerbMap[a]),
+ })
+ expect(locators, 'Could not find a play or replay button.').toHaveLength(
+ options.filterVisible ? 1 : 2
+ )
+ return locators
+}
+
+const getInactive = async (context: Locator) =>
+ (await getAllInactive(context, { filterVisible: true }))[0]
+
+export default {
+ getPossibleAudioActions,
+ getAllActive,
+ getActive,
+ getAllInactive,
+ getInactive,
+}