Skip to content

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

Add Playwright utilities (#1530)

Olga Bulat01831a58dfd1
diff --git a/src/components/VTranslationStatusBanner.vue b/src/components/VTranslationStatusBanner.vue
index feefde096..615085993 100644
--- a/src/components/VTranslationStatusBanner.vue
+++ b/src/components/VTranslationStatusBanner.vue
@@ -3,6 +3,7 @@
:id="bannerKey"
:enabled="needsTranslationBanner"
variant="informational"
+ data-testid="banner-translation"
>
{{
// eslint-disable-next-line @intlify/vue-i18n/no-raw-text
diff --git a/src/locales/po-files/openverse.pot b/src/locales/po-files/openverse.pot
index 4d6d44a76..453cc8fdc 100644
--- a/src/locales/po-files/openverse.pot
+++ b/src/locales/po-files/openverse.pot
@@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Openverse \n"
"Report-Msgid-Bugs-To: https://github.com/wordpress/openverse/issues \n"
-"POT-Creation-Date: 2022-06-22T23:37:38+00:00\n"
+"POT-Creation-Date: 2022-06-27T05:00:35+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@@ -1978,12 +1978,12 @@ msgid "search button"
msgstr ""
#. Do not translate words between ### ###.
-#: src/components/VTranslationStatusBanner.vue:11
+#: src/components/VTranslationStatusBanner.vue:12
msgctxt "notification.translation.text"
msgid "The translation for ###locale### locale is incomplete. Help us get to 100 percent by ###link###."
msgstr ""
-#: src/components/VTranslationStatusBanner.vue:14
+#: src/components/VTranslationStatusBanner.vue:15
msgctxt "notification.translation.link"
msgid "contributing a translation"
msgstr ""
diff --git a/test/playwright/e2e/filters.spec.ts b/test/playwright/e2e/filters.spec.ts
index 0c7c9f451..1caf2b8be 100644
--- a/test/playwright/e2e/filters.spec.ts
+++ b/test/playwright/e2e/filters.spec.ts
@@ -4,6 +4,7 @@ import {
assertCheckboxStatus,
openFilters,
changeContentType,
+ goToSearchTerm,
} from '~~/test/playwright/utils/navigation'
import { mockProviderApis } from '~~/test/playwright/utils/route'
@@ -43,9 +44,7 @@ for (const searchType of supportedSearchTypes) {
test(`correct total number of filters is displayed for ${searchType}`, async ({
page,
}) => {
- const searchTypePath = searchType === ALL_MEDIA ? '' : searchType
- const searchUrl = `/search/${searchTypePath}?q=cat`
- await page.goto(searchUrl)
+ await goToSearchTerm(page, 'cat', { searchType })
await openFilters(page)
diff --git a/test/playwright/e2e/homepage.spec.ts b/test/playwright/e2e/homepage.spec.ts
index 4556863f5..990799586 100644
--- a/test/playwright/e2e/homepage.spec.ts
+++ b/test/playwright/e2e/homepage.spec.ts
@@ -1,45 +1,24 @@
import { test, expect } from '@playwright/test'
import { mockProviderApis } from '~~/test/playwright/utils/route'
+import {
+ goToSearchTerm,
+ searchTypePath,
+} from '~~/test/playwright/utils/navigation'
+
+import { supportedSearchTypes } from '~/constants/media'
test.beforeEach(async ({ context }) => {
await mockProviderApis(context)
})
-test('can change type and search for audio from homepage', async ({ page }) => {
- // Go to http://localhost:8444/
- await page.goto('/')
- await page.click('[aria-label="All content"]')
- await page.click('button[role="radio"]:has-text("Audio")')
-
- const searchInput = page.locator('main input[type="search"]')
- await searchInput.type('cat')
- await page.click('[aria-label="Search"]')
- const expectedUrl = '/search/audio?q=cat'
- await expect(page).toHaveURL(expectedUrl)
-})
-
-test('can search for all results from homepage', async ({ page }) => {
- await page.goto('/')
-
- const searchInput = page.locator('main input[type="search"]')
- await searchInput.type('cat')
- await page.click('[aria-label="Search"]')
-
- await expect(page).toHaveURL('search/?q=cat')
+for (const searchType of supportedSearchTypes) {
+ test(`can change type and search for ${searchType} from homepage`, async ({
+ page,
+ }) => {
+ await goToSearchTerm(page, 'cat', { searchType, mode: 'CSR' })
- await expect(page.locator('img')).toHaveCount(20)
-})
-test('can search for images from homepage', async ({ page }) => {
- await page.goto('/')
- await page.click('[aria-label="All content"]')
- await page.click('button[role="radio"]:has-text("Images")')
-
- const searchInput = page.locator('main input[type="search"]')
- await searchInput.type('cat')
- await page.click('[aria-label="Search"]')
-
- await expect(page).toHaveURL('search/image?q=cat')
-
- await expect(page.locator('img')).toHaveCount(20)
-})
+ const expectedUrl = `/search/${searchTypePath(searchType)}?q=cat`
+ await expect(page).toHaveURL(expectedUrl)
+ })
+}
diff --git a/test/playwright/e2e/mobile-menu.spec.ts b/test/playwright/e2e/mobile-menu.spec.ts
index b679bcb06..88b86eb69 100644
--- a/test/playwright/e2e/mobile-menu.spec.ts
+++ b/test/playwright/e2e/mobile-menu.spec.ts
@@ -2,6 +2,7 @@ import { test, expect } from '@playwright/test'
import {
closeMobileMenu,
+ goToSearchTerm,
openFilters,
openMobileMenu,
} from '~~/test/playwright/utils/navigation'
@@ -33,7 +34,7 @@ test('Can open filters menu on mobile at least twice', async ({ page }) => {
})
test('Can open mobile menu at least twice', async ({ page }) => {
- await page.goto('/search/?q=cat')
+ await goToSearchTerm(page, 'cat')
await openMobileMenu(page)
await expect(page.locator('button', { hasText: 'Close' })).toBeVisible()
await closeMobileMenu(page)
diff --git a/test/playwright/e2e/report-media.spec.ts b/test/playwright/e2e/report-media.spec.ts
index aad5e0a77..55587a567 100644
--- a/test/playwright/e2e/report-media.spec.ts
+++ b/test/playwright/e2e/report-media.spec.ts
@@ -1,6 +1,12 @@
import { test, expect, Page, BrowserContext } from '@playwright/test'
import { mockProviderApis } from '~~/test/playwright/utils/route'
+import {
+ goToSearchTerm,
+ searchTypeNames,
+} from '~~/test/playwright/utils/navigation'
+
+import { supportedMediaTypes } from '~/constants/media'
/**
* Some helpers for repeated actions.
@@ -78,7 +84,6 @@ test.beforeEach(async ({ context }) => {
await mockProviderApis(context)
})
-const mediaTypes = ['Images', 'Audio']
const reports = {
dmca: submitDmcaReport,
mature: submitMatureContentReport,
@@ -89,18 +94,12 @@ const reports = {
* Iterate through all the media types and supported reports
* to make sure every permutation works correctly.
*/
-mediaTypes.forEach((mediaType) => {
+supportedMediaTypes.forEach((mediaType) => {
Object.entries(reports).forEach(([reportName, reportAssertion]) => {
- test(`Files ${reportName} report for ${mediaType.toLowerCase()}`, async ({
- page,
- context,
- }) => {
- await page.goto('/')
- await page.click(`[aria-label="All content"]`)
- await page.click(`button[role="radio"]:has-text("${mediaType}")`)
- const searchInput = page.locator('main input[type="search"]')
- await searchInput.type('cat')
- await page.click('[aria-label="Search"]')
+ test(`Files ${reportName} report for ${searchTypeNames[
+ mediaType
+ ].toLowerCase()}`, async ({ page, context }) => {
+ await goToSearchTerm(page, 'cat', { searchType: mediaType })
await visitFirstResult(page)
await openReportModal(page)
await reportAssertion(page, context)
diff --git a/test/playwright/e2e/search-navigation.spec.ts b/test/playwright/e2e/search-navigation.spec.ts
index b689bf51d..a96d1abdd 100644
--- a/test/playwright/e2e/search-navigation.spec.ts
+++ b/test/playwright/e2e/search-navigation.spec.ts
@@ -1,24 +1,27 @@
import { expect, test } from '@playwright/test'
-import { openFilters } from '~~/test/playwright/utils/navigation'
+import {
+ goToSearchTerm,
+ openFilters,
+} from '~~/test/playwright/utils/navigation'
import { mockProviderApis } from '~~/test/playwright/utils/route'
test.beforeEach(async ({ context }) => {
- mockProviderApis(context)
+ await mockProviderApis(context)
})
test.describe('search history navigation', () => {
test('should update search results when back navigation changes filters', async ({
page,
}) => {
- await page.goto('/search/?q=galah')
+ await goToSearchTerm(page, 'galah')
// Open filter sidebar
await openFilters(page)
// Apply a filter
await page.click('#modification')
- // Verify the filter is appled to the URL and the checkbox is checked
+ // Verify the filter is applied to the URL and the checkbox is checked
// Note: Need to add that a search was actually executed with the new
// filters and that the page results have been updated for the new filters
// @todo(sarayourfriend): ^?
@@ -36,7 +39,7 @@ test.describe('search history navigation', () => {
test('should update search results when back button updates search type', async ({
page,
}) => {
- await page.goto('/search?q=galah')
+ await goToSearchTerm(page, 'galah')
await page.click('text=See all images')
await page.waitForSelector('text=See all images', { state: 'hidden' })
expect(page.url()).toContain('/search/image')
@@ -48,7 +51,7 @@ test.describe('search history navigation', () => {
})
test('navigates to the image detail page correctly', async ({ page }) => {
- await page.goto('/search/image?q=honey')
+ await goToSearchTerm(page, 'honey')
const figure = page.locator('figure').first()
const imgTitle = await figure.locator('img').getAttribute('alt')
diff --git a/test/playwright/e2e/search-query-client.spec.ts b/test/playwright/e2e/search-query-client.spec.ts
index 84c2ca369..8bd4d031e 100644
--- a/test/playwright/e2e/search-query-client.spec.ts
+++ b/test/playwright/e2e/search-query-client.spec.ts
@@ -1,8 +1,13 @@
-import { test, expect } from '@playwright/test'
+import { expect, test } from '@playwright/test'
-import { changeContentType } from '~~/test/playwright/utils/navigation'
+import {
+ changeContentType,
+ goToSearchTerm,
+} from '~~/test/playwright/utils/navigation'
import { mockProviderApis } from '~~/test/playwright/utils/route'
+import { AUDIO, IMAGE } from '~/constants/media'
+
/**
* When navigating to the search page on the client side:
* 1. `q` parameter is set as the search input value and url parameter.
@@ -20,11 +25,7 @@ test.beforeEach(async ({ context }) => {
})
test('q query parameter is set as the search term', async ({ page }) => {
- await page.goto('/')
-
- const searchInput = page.locator('main input[type="search"]')
- await searchInput.type('cat')
- await page.click('[aria-label="Search"]')
+ await goToSearchTerm(page, 'cat', { mode: 'CSR' })
await expect(page.locator('header input[type="search"]')).toHaveValue('cat')
await expect(page).toHaveURL('search/?q=cat')
@@ -33,12 +34,7 @@ test('q query parameter is set as the search term', async ({ page }) => {
test('selecting `audio` on homepage, you can search for audio', async ({
page,
}) => {
- await page.goto('/')
- await page.click('[aria-label="All content"]')
- await page.click('button[role="radio"]:has-text("Audio")')
-
- await page.type('main input[type="search"]', 'cat')
- await page.click('[aria-label="Search"]')
+ await goToSearchTerm(page, 'cat', { searchType: AUDIO, mode: 'CSR' })
await expect(page.locator('header input[type="search"]')).toHaveValue('cat')
@@ -48,7 +44,10 @@ test('selecting `audio` on homepage, you can search for audio', async ({
test('url filter parameters not used by current mediaType are discarded', async ({
page,
}) => {
- await page.goto('/search/image?q=cat&category=photograph')
+ await goToSearchTerm(page, 'cat', {
+ searchType: IMAGE,
+ query: 'category=photograph',
+ })
await changeContentType(page, 'Audio')
await expect(page).toHaveURL('/search/audio?q=cat')
@@ -57,21 +56,27 @@ test('url filter parameters not used by current mediaType are discarded', async
test('url filter types not used by current mediaType are discarded', async ({
page,
}) => {
- await page.goto('/search/image?q=cat&aspect_ratio=tall')
+ await goToSearchTerm(page, 'cat', {
+ searchType: IMAGE,
+ query: 'aspect_ratio=tall',
+ })
await changeContentType(page, 'Audio')
await expect(page).toHaveURL('/search/audio?q=cat')
})
test('can search for a different term', async ({ page }) => {
- await page.goto('/search/image?q=cat')
+ await goToSearchTerm(page, 'cat', { searchType: IMAGE })
await page.fill('header input[type="search"]', 'dog')
await page.keyboard.press('Enter')
await expect(page).toHaveURL('/search/image?q=dog')
})
test('search for a different term keeps query parameters', async ({ page }) => {
- await page.goto('/search/image?q=cat&license=by&extension=jpg')
+ await goToSearchTerm(page, 'cat', {
+ searchType: IMAGE,
+ query: 'license=by&extension=jpg',
+ })
await page.fill('header input[type="search"]', 'dog')
await page.keyboard.press('Enter')
await expect(page).toHaveURL('/search/image?q=dog&license=by&extension=jpg')
diff --git a/test/playwright/e2e/search-query-server.spec.ts b/test/playwright/e2e/search-query-server.spec.ts
index 970074f95..1ef20c3fb 100644
--- a/test/playwright/e2e/search-query-server.spec.ts
+++ b/test/playwright/e2e/search-query-server.spec.ts
@@ -1,12 +1,15 @@
import { test, expect } from '@playwright/test'
import {
- openFilters,
- currentContentType,
assertCheckboxStatus,
+ currentContentType,
+ goToSearchTerm,
+ openFilters,
} from '~~/test/playwright/utils/navigation'
import { mockProviderApis } from '~~/test/playwright/utils/route'
+import { AUDIO, IMAGE } from '~/constants/media'
+
/**
* URL is correctly converted into search state:
* 1. `q` parameter is set as the search input value
@@ -24,9 +27,9 @@ test.beforeEach(async ({ context }) => {
})
test('q query parameter is set as the search term', async ({ page }) => {
- await page.goto(
- '/search/?q=cat&license=cc0&license_type=commercial&searchBy=creator'
- )
+ await goToSearchTerm(page, 'cat', {
+ query: 'license=cc0&license_type=commercial&searchBy=creator',
+ })
const searchInput = page.locator('input[type="search"]')
await expect(searchInput).toHaveValue('cat')
@@ -46,8 +49,7 @@ test('url path /search/ is used to select `all` search tab', async ({
test('url path /search/audio is used to select `audio` search tab', async ({
page,
}) => {
- const audioSearchUrl = '/search/audio?q=cat'
- await page.goto(audioSearchUrl)
+ await goToSearchTerm(page, 'cat', { searchType: AUDIO })
const contentType = await currentContentType(page)
expect(contentType?.trim()).toEqual('Audio')
@@ -56,9 +58,9 @@ test('url path /search/audio is used to select `audio` search tab', async ({
test('url query to filter, all tab, one parameter per filter type', async ({
page,
}) => {
- await page.goto(
- '/search/?q=cat&license=cc0&license_type=commercial&searchBy=creator'
- )
+ await goToSearchTerm(page, 'cat', {
+ query: 'license=cc0&license_type=commercial&searchBy=creator',
+ })
await openFilters(page)
for (const checkbox of ['cc0', 'commercial', 'creator']) {
@@ -69,9 +71,10 @@ test('url query to filter, all tab, one parameter per filter type', async ({
test('url query to filter, image tab, several filters for one filter type selected', async ({
page,
}) => {
- await page.goto(
- '/search/image?q=cat&searchBy=creator&extension=jpg,png,gif,svg'
- )
+ await goToSearchTerm(page, 'cat', {
+ searchType: IMAGE,
+ query: 'searchBy=creator&extension=jpg,png,gif,svg',
+ })
await openFilters(page)
const checkboxes = ['jpeg', 'png', 'gif', 'svgs']
for (const checkbox of checkboxes) {
@@ -82,7 +85,7 @@ test('url query to filter, image tab, several filters for one filter type select
test.skip('url mature query is set, and can be unchecked using the Safer Browsing popup', async ({
page,
}) => {
- await page.goto('/search/image?q=cat&mature=true')
+ await goToSearchTerm(page, 'cat', { searchType: IMAGE, query: 'mature=true' })
await page.click('button:has-text("Safer Browsing")')
diff --git a/test/playwright/e2e/search-types.spec.ts b/test/playwright/e2e/search-types.spec.ts
index a9ef2923b..9c8637554 100644
--- a/test/playwright/e2e/search-types.spec.ts
+++ b/test/playwright/e2e/search-types.spec.ts
@@ -1,6 +1,11 @@
import { test, expect, Page } from '@playwright/test'
-import { changeContentType } from '~~/test/playwright/utils/navigation'
+import {
+ changeContentType,
+ goToSearchTerm,
+ searchTypePath,
+} from '~~/test/playwright/utils/navigation'
+import { mockProviderApis } from '~~/test/playwright/utils/route'
/**
* Using SSR:
@@ -19,8 +24,7 @@ import { changeContentType } from '~~/test/playwright/utils/navigation'
*/
test.beforeEach(async ({ context }) => {
- // Block any audio (jamendo.com) requests for each test in this file.
- await context.route('**.jamendo.com**', (route) => route.abort())
+ await mockProviderApis(context)
})
const allContentConfig = {
@@ -81,7 +85,7 @@ async function checkSearchMetadata(page: Page, searchType: SearchTypeConfig) {
}
async function checkPageMeta(page: Page, searchType: SearchTypeConfig) {
- const urlParam = searchType.id === 'all' ? '' : searchType.id
+ const urlParam = searchTypePath(searchType.id)
const expectedTitle = `birds | Openverse`
const expectedURL = `/search/${urlParam}?q=birds`
@@ -98,7 +102,7 @@ async function checkSearchResult(page: Page, searchType: SearchTypeConfig) {
for (const searchType of searchTypes) {
test(`Can open ${searchType.name} search page on SSR`, async ({ page }) => {
- await page.goto(searchType.url)
+ await goToSearchTerm(page, 'birds', { searchType: searchType.id })
await checkSearchResult(page, searchType)
})
diff --git a/test/playwright/e2e/search.spec.ts b/test/playwright/e2e/search.spec.ts
index cba9bdaa5..50b7cd849 100644
--- a/test/playwright/e2e/search.spec.ts
+++ b/test/playwright/e2e/search.spec.ts
@@ -9,6 +9,11 @@
import { expect, test } from '@playwright/test'
import { mockProviderApis } from '~~/test/playwright/utils/route'
+import {
+ goToSearchTerm,
+ scrollToBottom,
+ searchFromHeader,
+} from '~~/test/playwright/utils/navigation'
test.beforeEach(async ({ context }) => {
await mockProviderApis(context)
@@ -20,15 +25,14 @@ test('shows no results page when no results', async ({ page }) => {
})
test('scroll to top on new search term submitted', async ({ page }) => {
- await page.goto('/search/?q=galah')
- await page.keyboard.press('PageDown')
+ await goToSearchTerm(page, 'galah')
+ await scrollToBottom(page)
let scrollY = await page.evaluate(() => window.scrollY)
expect(scrollY).not.toBe(0)
- await page.fill('id=search-bar', 'honey')
- await page.locator('button[type="submit"]').click()
+ await searchFromHeader(page, 'honey')
scrollY = await page.evaluate(() => window.scrollY)
- await expect(scrollY).toBe(0)
+ expect(scrollY).toBe(0)
})
diff --git a/test/playwright/e2e/translation-banner.spec.ts b/test/playwright/e2e/translation-banner.spec.ts
index aeb1cc429..be7cbefc4 100644
--- a/test/playwright/e2e/translation-banner.spec.ts
+++ b/test/playwright/e2e/translation-banner.spec.ts
@@ -24,10 +24,9 @@ test.describe('translation banner', () => {
test('Can close the translation banner', async ({ page }) => {
await page.goto(russianSearchPath)
- await page.click(
- '[data-testid="banner-translation-ru"] [aria-label="Закрыть"]:visible',
- { timeout: 500 }
- )
+ await page.click('[data-testid="banner-translation"] button:visible', {
+ timeout: 500,
+ })
const banner = page.locator('.span:has-text("Help us get to 100 percent")')
await expect(banner).not.toBeVisible({ timeout: 500 })
diff --git a/test/playwright/utils/navigation.ts b/test/playwright/utils/navigation.ts
index a287511db..b5ff8a634 100644
--- a/test/playwright/utils/navigation.ts
+++ b/test/playwright/utils/navigation.ts
@@ -1,10 +1,41 @@
import { expect, Page } from '@playwright/test'
+import {
+ ALL_MEDIA,
+ AUDIO,
+ IMAGE,
+ MediaType,
+ MODEL_3D,
+ SupportedSearchType,
+ VIDEO,
+} from '~/constants/media'
+import { SCREEN_SIZES } from '~/constants/screens'
+
+import messages from '~/locales/en.json'
+
+const smWidth = SCREEN_SIZES.get('sm') as number
+
const buttonSelectors = {
filter: '[aria-controls="filters"]',
contentSwitcher: '[aria-controls="content-switcher-modal"]',
}
+export function sleep(ms: number) {
+ return new Promise<void>((resolve) => setTimeout(resolve, ms))
+}
+
+export type RenderMode = 'SSR' | 'CSR'
+export const searchTypePath = (searchType: SupportedSearchType) =>
+ searchType === 'all' ? '' : `${searchType}`
+
+export const searchTypeNames = {
+ [IMAGE]: messages['search-type'][IMAGE],
+ [AUDIO]: messages['search-type'][AUDIO],
+ [VIDEO]: messages['search-type'][VIDEO],
+ [MODEL_3D]: messages['search-type'][MODEL_3D],
+ [ALL_MEDIA]: messages['search-type'][ALL_MEDIA],
+}
+
const isButtonPressed = async (page: Page, buttonSelector: string) => {
const viewportSize = page.viewportSize()
if (!viewportSize) {
@@ -89,8 +120,151 @@ export const currentContentType = async (page: Page) => {
return contentSwitcherButton.textContent()
}
+/**
+ * Dismisses the translation banner if it is visible. It does not wait for the banner to become visible,
+ * so the page should finish rendering before calling `dismissTranslationBanner`.
+ */
export const dismissTranslationBanner = async (page: Page) => {
- await page
- .locator('[data-testid="banner-translation-ar"] [aria-label="Close"]')
- .click()
+ const bannerCloseButton = page.locator(
+ '[data-testid="banner-translation"] button'
+ )
+ if (await bannerCloseButton.isVisible()) {
+ await bannerCloseButton.click()
+ }
+}
+
+export const selectHomepageSearchType = async (
+ page: Page,
+ searchType: SupportedSearchType
+) => {
+ const pageWidth = page.viewportSize()?.width
+ if (pageWidth && pageWidth > smWidth) {
+ await page.click('[aria-label="All content"]')
+ await page.click(
+ `button[role="radio"]:has-text("${searchTypeNames[searchType]}")`
+ )
+ } else {
+ await page.click(`button:has-text("${searchTypeNames[searchType]}")`)
+ }
+}
+
+export const goToSearchTerm = async (
+ page: Page,
+ term: string,
+ options: {
+ searchType?: SupportedSearchType
+ mode?: RenderMode
+ dir?: 'ltr' | 'rtl'
+ query?: string // Only for SSR mode
+ } = {}
+) => {
+ const searchType = options.searchType || ALL_MEDIA
+ const dir = options.dir || 'ltr'
+ const mode = options.mode ?? 'SSR'
+ const query = options.query ? `&${options.query}` : ''
+
+ if (mode === 'SSR') {
+ const path = `search/${searchTypePath(searchType)}?q=${term}${query}`
+ await page.goto(pathWithDir(path, dir))
+ await dismissTranslationBanner(page)
+ } else {
+ await page.goto(pathWithDir('/', dir))
+ await dismissTranslationBanner(page)
+ // Select the search type
+ if (searchType !== 'all') {
+ await selectHomepageSearchType(page, searchType)
+ }
+ // Type search term
+ const searchInput = page.locator('main input[type="search"]')
+ await searchInput.type(term)
+ // Click search button
+ // Wait for navigation
+ await Promise.all([
+ page.waitForNavigation(),
+ page.click('[aria-label="Search"]'),
+ ])
+ await page.waitForLoadState('networkidle')
+ }
+ await scrollDownAndUp(page)
+ const pageWidth = page.viewportSize()?.width
+ if (pageWidth && pageWidth > smWidth) {
+ await page.waitForSelector('[aria-label="menu"]')
+ }
+}
+
+/**
+ * Fills the search input in the page header, clicks on submit
+ * and waits for navigation.
+ */
+export const searchFromHeader = async (page: Page, term: string) => {
+ await page.fill('id=search-bar', term)
+ await Promise.all([
+ page.waitForNavigation(),
+ page.locator('button[type="submit"]').click(),
+ ])
+}
+
+/**
+ * Click on the first <mediaType> result: a link that contains
+ * /<mediaType>/ in its URL. We cannot use the 'startsWith' `^` matcher
+ * because localized routes start with the locale prefix (e.g. /ar/image/).
+ * Scroll down and up to load all lazy-loaded content.
+ */
+export const openFirstResult = async (page: Page, mediaType: MediaType) => {
+ await page.locator(`a[href*="/${mediaType}/"]`).first().click()
+ await scrollDownAndUp(page)
+}
+
+/**
+ * Click on the first <mediaType> related result: a link that contains
+ * /<mediaType>/ in its URL in the 'aside' element for related media.
+ * We cannot use the 'startsWith' `^` matcher because localized routes
+ * start with the locale prefix (e.g. /ar/image/).
+ * Scroll down and up to load all lazy-loaded content.
+ */
+export const openFirstRelatedResult = async (
+ page: Page,
+ mediaType: MediaType
+) => {
+ await page.locator(`aside a[href*="/${mediaType}/"]`).first().click()
+ await scrollDownAndUp(page)
+}
+
+export const scrollToBottom = async (page: Page) => {
+ await page.evaluate(() => {
+ window.scrollTo(0, document.body.scrollHeight)
+ })
+}
+
+export const scrollToTop = async (page: Page) => {
+ await page.evaluate(() => {
+ window.scrollTo(0, 0)
+ })
+ await sleep(200)
+}
+
+/**
+ * Used to load all lazy-loaded images in the page.
+ */
+export const scrollDownAndUp = async (page: Page) => {
+ await scrollToBottom(page)
+ await page.waitForLoadState('networkidle')
+ await scrollToTop(page)
+}
+
+export const languageDirections = ['ltr', 'rtl'] as const
+
+export const renderingContexts = [
+ ['SSR', 'ltr'],
+ ['SSR', 'rtl'],
+ ['CSR', 'ltr'],
+ ['CSR', 'rtl'],
+] as const
+
+/**
+ * Adds '/ar' prefix to a rtl route. The path should start with '/'
+ */
+export const pathWithDir = (rawPath: string, dir: string) => {
+ const path = rawPath.startsWith('/') ? rawPath : `/${rawPath}`
+ return dir === 'rtl' ? `/ar${path}` : path
}
diff --git a/test/playwright/visual-regression/components/header.spec.ts b/test/playwright/visual-regression/components/header.spec.ts
index 8c177dee4..efb221e57 100644
--- a/test/playwright/visual-regression/components/header.spec.ts
+++ b/test/playwright/visual-regression/components/header.spec.ts
@@ -2,65 +2,49 @@ import { test } from '@playwright/test'
import breakpoints from '~~/test/playwright/utils/breakpoints'
import { hideInputCursors } from '~~/test/playwright/utils/page'
+import {
+ goToSearchTerm,
+ languageDirections,
+ scrollToBottom,
+ sleep,
+} from '~~/test/playwright/utils/navigation'
const headerSelector = '.main-header'
const loadMoreSelector = 'button:has-text("Load more")'
test.describe('header snapshots', () => {
- test.describe('ltr', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/search/?q=birds')
- })
-
- test.describe('header', () => {
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('resting', async ({ page }) => {
- await expectSnapshot('resting-ltr', page.locator(headerSelector))
- })
-
- test('scrolled', async ({ page }) => {
- await page.locator(loadMoreSelector).focus()
- await expectSnapshot('scrolled-ltr', page.locator(headerSelector))
- })
-
- test('searchbar hovered', async ({ page }) => {
- await page.hover('input')
- await hideInputCursors(page)
- await expectSnapshot(
- 'searchbar-hovered-ltr',
- page.locator(headerSelector)
- )
- })
+ for (const dir of languageDirections) {
+ test.describe(dir, () => {
+ test.beforeEach(async ({ page }) => {
+ await goToSearchTerm(page, 'birds', { dir: dir })
})
- })
- })
-
- test.describe('rtl', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/ar/search/?q=birds')
- })
-
- test.describe('header', () => {
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('resting', async ({ page }) => {
- await expectSnapshot('resting-rtl', page.locator(headerSelector))
- })
-
- test('scrolled', async ({ page }) => {
- await page.locator(loadMoreSelector).focus()
- await page.mouse.wheel(10, 0)
- await expectSnapshot('scrolled-rtl', page.locator(headerSelector))
- })
- test('searchbar hovered', async ({ page }) => {
- await page.hover('input')
- await hideInputCursors(page)
- await expectSnapshot(
- 'searchbar-hovered-rtl',
- page.locator(headerSelector)
- )
+ test.describe('header', () => {
+ breakpoints.describeEvery(({ expectSnapshot }) => {
+ test('resting', async ({ page }) => {
+ await expectSnapshot(`resting-${dir}`, page.locator(headerSelector))
+ })
+
+ test('scrolled', async ({ page }) => {
+ await page.locator(loadMoreSelector).focus()
+ await scrollToBottom(page)
+ await sleep(200)
+ await expectSnapshot(
+ `scrolled-${dir}`,
+ page.locator(headerSelector)
+ )
+ })
+
+ test('searchbar hovered', async ({ page }) => {
+ await page.hover('input')
+ await hideInputCursors(page)
+ await expectSnapshot(
+ `searchbar-hovered-${dir}`,
+ page.locator(headerSelector)
+ )
+ })
})
})
})
- })
+ }
})
diff --git a/test/playwright/visual-regression/components/media-reuse.spec.ts b/test/playwright/visual-regression/components/media-reuse.spec.ts
index f56e9e2e3..82eab6beb 100644
--- a/test/playwright/visual-regression/components/media-reuse.spec.ts
+++ b/test/playwright/visual-regression/components/media-reuse.spec.ts
@@ -1,7 +1,11 @@
import { test } from '@playwright/test'
import breakpoints from '~~/test/playwright/utils/breakpoints'
-import { dismissTranslationBanner } from '~~/test/playwright/utils/navigation'
+import {
+ dismissTranslationBanner,
+ pathWithDir,
+ languageDirections,
+} from '~~/test/playwright/utils/navigation'
const tabs = [
{ id: 'rich', name: 'Rich Text' },
@@ -9,47 +13,26 @@ const tabs = [
{ id: 'plain', name: 'Plain text' },
]
test.describe('media-reuse', () => {
- test.describe('ltr', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/image/f9384235-b72e-4f1e-9b05-e1b116262a29')
- })
- for (const tab of tabs) {
+ for (const tab of tabs) {
+ for (const dir of languageDirections) {
breakpoints.describeEvery(({ expectSnapshot }) => {
- test(`Should render media reuse section with "${tab.name}" tab open`, async ({
+ test(`Should render a ${dir} media reuse section with "${tab.name}" tab open`, async ({
page,
}) => {
- await page.locator(`text=${tab.name}`).click()
- // Make sure the tab is not focused and doesn't have a pink ring
- await page.locator('h3:has-text("Reuse content")').click()
- await expectSnapshot(
- `media-reuse-ltr-${tab.id}-tab`,
- page.locator('.media-reuse')
+ await page.goto(
+ pathWithDir('/image/f9384235-b72e-4f1e-9b05-e1b116262a29', dir)
)
- })
- })
- }
- })
-
- test.describe('rtl', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/ar/image/f9384235-b72e-4f1e-9b05-e1b116262a29')
- await dismissTranslationBanner(page)
- })
+ await dismissTranslationBanner(page)
- for (const tab of tabs) {
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test(`Should render media reuse section with "${tab.name}" tab open`, async ({
- page,
- }) => {
await page.locator(`text=${tab.name}`).click()
// Make sure the tab is not focused and doesn't have a pink ring
await page.locator('h3:has-text("Reuse content")').click()
await expectSnapshot(
- `media-reuse-rtl-${tab.id}-tab`,
+ `media-reuse-${dir}-${tab.id}-tab`,
page.locator('.media-reuse')
)
})
})
}
- })
+ }
})
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts b/test/playwright/visual-regression/pages/homepage.spec.ts
index 926bb4416..c927c7e97 100644
--- a/test/playwright/visual-regression/pages/homepage.spec.ts
+++ b/test/playwright/visual-regression/pages/homepage.spec.ts
@@ -2,56 +2,32 @@ import { test, Page } from '@playwright/test'
import breakpoints from '~~/test/playwright/utils/breakpoints'
import { hideInputCursors } from '~~/test/playwright/utils/page'
-
+import {
+ dismissTranslationBanner,
+ pathWithDir,
+ languageDirections,
+} from '~~/test/playwright/utils/navigation'
+
+/**
+ * Remove the randomly-selected images from the homepage .
+ */
const deleteImageCarousel = async (page: Page) => {
const element = await page.$('[data-testid="image-carousel"]')
await element?.evaluate((node) => node.remove())
element?.dispose()
}
-
-test.describe('homepage snapshots', () => {
- test.describe('ltr', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/')
- })
-
- breakpoints.describeEvery(({ expectSnapshot }) =>
- test('full page', async ({ page }) => {
- await deleteImageCarousel(page)
- await expectSnapshot('index-ltr', page)
- })
- )
-
- test.describe('search input', () => {
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('unfocused', async ({ page }) => {
- await expectSnapshot(
- 'unfocused-search-ltr',
- page.locator('form:has(input)')
- )
- })
-
- test('focused', async ({ page }) => {
- await page.focus('input')
- await hideInputCursors(page)
- await expectSnapshot(
- 'focused-search-ltr',
- page.locator('form:has(input)')
- )
- })
- })
- })
- })
-
- test.describe('rtl', () => {
+for (const dir of languageDirections) {
+ test.describe(`${dir} homepage snapshots`, () => {
+ const path = pathWithDir('/', dir)
test.beforeEach(async ({ page }) => {
- await page.goto('/ar')
+ await page.goto(path)
+ await dismissTranslationBanner(page)
})
breakpoints.describeEvery(({ expectSnapshot }) =>
- test('full page', async ({ page }) => {
+ test(`${dir} full page`, async ({ page }) => {
await deleteImageCarousel(page)
- await expectSnapshot('index-rtl', page)
+ await expectSnapshot(`index-${dir}`, page)
})
)
@@ -59,7 +35,7 @@ test.describe('homepage snapshots', () => {
breakpoints.describeEvery(({ expectSnapshot }) => {
test('unfocused', async ({ page }) => {
await expectSnapshot(
- 'unfocused-search-rtl',
+ `unfocused-search-${dir}`,
page.locator('form:has(input)')
)
})
@@ -68,11 +44,11 @@ test.describe('homepage snapshots', () => {
await page.focus('input')
await hideInputCursors(page)
await expectSnapshot(
- 'focused-search-rtl',
+ `focused-search-${dir}`,
page.locator('form:has(input)')
)
})
})
})
})
-})
+}
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-2xl-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-2xl-linux.png
index b655c3fce..988e0433c 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-2xl-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-2xl-linux.png differ
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-lg-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-lg-linux.png
index 73edbe9a1..cb5fd948c 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-lg-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-lg-linux.png differ
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-md-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-md-linux.png
index b3d785ca7..daa3540bf 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-md-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-md-linux.png differ
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-sm-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-sm-linux.png
index 99b8633ed..f950bd38b 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-sm-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-sm-linux.png differ
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xl-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xl-linux.png
index d06a221b8..6b1c984fe 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xl-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xl-linux.png differ
diff --git a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xs-linux.png b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xs-linux.png
index 1a52fed69..c63e314f8 100644
Binary files a/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xs-linux.png and b/test/playwright/visual-regression/pages/homepage.spec.ts-snapshots/index-rtl-xs-linux.png differ
diff --git a/test/playwright/visual-regression/pages/pages.spec.ts b/test/playwright/visual-regression/pages/pages.spec.ts
index 04654f1f2..a1098a64b 100644
--- a/test/playwright/visual-regression/pages/pages.spec.ts
+++ b/test/playwright/visual-regression/pages/pages.spec.ts
@@ -2,7 +2,11 @@ import { test } from '@playwright/test'
import breakpoints from '~~/test/playwright/utils/breakpoints'
import { removeHiddenOverflow } from '~~/test/playwright/utils/page'
-import { dismissTranslationBanner } from '~~/test/playwright/utils/navigation'
+import {
+ dismissTranslationBanner,
+ pathWithDir,
+ languageDirections,
+} from '~~/test/playwright/utils/navigation'
const contentPages = [
'extension',
@@ -12,32 +16,21 @@ const contentPages = [
'non-existent',
]
for (const contentPage of contentPages) {
- test.describe(`${contentPage} page snapshots`, () => {
- test.describe('ltr', () => {
+ for (const dir of languageDirections) {
+ test.describe(`${contentPage} ${dir} page snapshots`, () => {
test.beforeEach(async ({ page }) => {
- await page.goto(`/${contentPage}`)
- })
-
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('full page', async ({ page }) => {
- await removeHiddenOverflow(page)
- await expectSnapshot(`${contentPage}-ltr`, page, { fullPage: true })
- })
- })
- })
-
- test.describe('rtl', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto(`/ar/${contentPage}`)
+ await page.goto(pathWithDir(contentPage, dir))
await dismissTranslationBanner(page)
})
breakpoints.describeEvery(({ expectSnapshot }) => {
test('full page', async ({ page }) => {
await removeHiddenOverflow(page)
- await expectSnapshot(`${contentPage}-rtl`, page, { fullPage: true })
+ await expectSnapshot(`${contentPage}-${dir}`, page, {
+ fullPage: true,
+ })
})
})
})
- })
+ }
}
diff --git a/test/playwright/visual-regression/pages/sources.spec.ts b/test/playwright/visual-regression/pages/sources.spec.ts
index d30c853e2..f6c0d58ef 100644
--- a/test/playwright/visual-regression/pages/sources.spec.ts
+++ b/test/playwright/visual-regression/pages/sources.spec.ts
@@ -2,31 +2,23 @@ import { test } from '@playwright/test'
import breakpoints from '~~/test/playwright/utils/breakpoints'
import { removeHiddenOverflow } from '~~/test/playwright/utils/page'
+import {
+ pathWithDir,
+ languageDirections,
+} from '~~/test/playwright/utils/navigation'
test.describe('sources page snapshots', () => {
- test.describe('ltr', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/sources')
- })
-
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('top', async ({ page }) => {
- await removeHiddenOverflow(page)
- await expectSnapshot('sources-ltr', page, { fullPage: true })
+ for (const dir of languageDirections) {
+ test.describe(dir, () => {
+ test.beforeEach(async ({ page }) => {
+ await page.goto(pathWithDir('sources', dir))
})
- })
- })
-
- test.describe('rtl', () => {
- test.beforeEach(async ({ page }) => {
- await page.goto('/ar/sources')
- })
-
- breakpoints.describeEvery(({ expectSnapshot }) => {
- test('top', async ({ page }) => {
- await removeHiddenOverflow(page)
- await expectSnapshot('sources-rtl', page, { fullPage: true })
+ breakpoints.describeEvery(({ expectSnapshot }) => {
+ test('top', async ({ page }) => {
+ await removeHiddenOverflow(page)
+ await expectSnapshot(`sources-${dir}`, page, { fullPage: true })
+ })
})
})
- })
+ }
})