Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import { expect, type Locator, type Page } from '@playwright/test'
import { gotoIndex } from './helpers'
/** Storefront-equivalent route paths used by the products specs. */
export const PRODUCTS_PATH = (storeId: string) => `/${storeId}/products`
export const OPTIONS_PATH = (storeId: string) => `/${storeId}/products/options`
/**
* Focus the product form's description field and type `text`. The field is a
* tiptap `RichTextEditor` (contenteditable), not a textarea, so `.fill()` is
* wrong. Click the editable surface and press Enter for each `\n` so each
* line becomes its own paragraph.
*/
export async function typeDescription(page: Page, text: string): Promise<void> {
const editor = page.locator('#product-description')
await editor.click()
const paragraphs = text.split('\n')
for (const [index, paragraph] of paragraphs.entries()) {
if (index > 0) await editor.press('Enter')
if (paragraph.length > 0) await editor.pressSequentially(paragraph)
}
}
/**
* Drive the New Product form and land on the edit page. The minimum input the
* form accepts is a name — pass a `description` when the spec specifically
* verifies how rich-text data round-trips.
*/
export async function createProduct(
page: Page,
storeId: string,
name: string,
description?: string,
): Promise<void> {
await gotoIndex(page, PRODUCTS_PATH(storeId), /add product/i)
await page.getByRole('button', { name: /add product/i }).click()
await expect(page.getByRole('heading', { name: /^new product$/i })).toBeVisible()
await page.getByLabel(/^name$/i).fill(name)
if (description) await typeDescription(page, description)
await page.getByRole('button', { name: /^create product$/i }).click()
// Lands on `/$storeId/products/$productId` (not `/new`).
await expect(page).toHaveURL(new RegExp(`/${storeId}/products/prod_[^/]+$`), { timeout: 15_000 })
}
/**
* Drive the New Option Type sheet to create a fresh option type with the given
* value rows. Returns the human-readable label the test can match in pickers
* (e.g. the Variants card's option-type combobox). Each call generates a
* timestamped suffix so concurrent specs don't collide.
*/
export async function seedOptionType(
page: Page,
storeId: string,
baseName: string,
values: string[],
): Promise<string> {
const suffix = Date.now() + Math.floor(Math.random() * 1000)
const internalName = `${baseName}-${suffix}`
const label = `E2E ${baseName.charAt(0).toUpperCase() + baseName.slice(1)} ${suffix}`
await gotoIndex(page, OPTIONS_PATH(storeId), /add option type/i)
await page.getByRole('button', { name: /add option type/i }).click()
await expect(page.getByRole('heading', { name: /create option type/i })).toBeVisible()
await page.locator('#label').fill(label)
await page.locator('#name').fill(internalName)
for (const [i, value] of values.entries()) {
await page.getByRole('button', { name: /add option value/i }).click()
const row = page
.locator('tbody tr')
.filter({ has: page.getByLabel('Internal name') })
.nth(i)
await row.getByLabel('Internal name').fill(value)
await row.getByLabel('Label').fill(value.charAt(0).toUpperCase() + value.slice(1))
}
await page.getByRole('button', { name: /create option type/i }).click()
await expect(page.getByRole('heading', { name: /create option type/i })).toBeHidden({
timeout: 15_000,
})
return label
}
/**
* Add an option type (already seeded) plus a set of value toggles to the
* currently-open product's Variants card, then confirm. Scoped to the
* Variants card by title so it doesn't fight the page-level "Add" buttons.
*/
export async function addOptionToVariants(
page: Page,
optionTypeLabel: string,
valueLabels: string[],
): Promise<void> {
const card = variantsCard(page)
await card.getByRole('button', { name: /add option/i }).click()
// Base UI Select renders the selected value as the trigger label, so we
// open the listbox and click an item by name rather than typing.
await card.getByRole('combobox').first().click()
await page.getByRole('option', { name: optionTypeLabel, exact: true }).click()
for (const label of valueLabels) {
await card.getByRole('button', { name: label, exact: true }).click()
}
await card.getByRole('button', { name: /^done$/i }).click()
}
/**
* Scope a locator to the product edit page's named card. `<CardTitle>` is a
* `<div>` (not a heading), so we anchor on the `data-slot` shadcn emits.
* Title matches are exact to avoid catching "Categorization" when the caller
* asked for "Categories" or similar prefix collisions.
*/
export function card(page: Page, title: RegExp): Locator {
return page
.locator('[data-slot="card"]')
.filter({ has: page.locator('[data-slot="card-title"]', { hasText: title }) })
}
export const variantsCard = (page: Page) => card(page, /^Variants$/)
export const mediaCard = (page: Page) => card(page, /^Media$/)
/**
* Act on the first media thumbnail: clicking a tile opens its edit sheet, and
* every other action lives in the tile's right-click menu.
*
* Scrolls the Media card clear of the sticky PageHeader first — restoring
* sticky headers (PR #14218) made Playwright's default scroll-into-view land
* the tile under the header chrome.
*/
export async function clickMediaThumbnailAction(
media: Locator,
action: 'edit' | 'delete',
): Promise<void> {
// Target the tile itself, not its image — a video with no poster renders an
// icon instead, and waiting on an <img> would hang.
const thumb = media.locator('[data-slot="media-thumbnail"]').first()
await thumb.scrollIntoViewIfNeeded()
// Keep the card below the sticky PageHeader. Playwright's default click
// scrolls the target back into view, so we force-click after positioning —
// otherwise the header chrome intercepts pointer events.
//
// The header publishes its own measured height into this variable, so read
// it rather than assuming one: it is the whole of the sticky chrome now that
// there is no top bar above it.
//
// Scroll the sheet, not the window. The inset shell is exactly viewport
// height and scrolls inside itself, so the document cannot scroll at all and
// `window.scrollBy` moves nothing — the tile would stay under the header.
await media.evaluate((el) => {
const headerHeight =
Number.parseFloat(
getComputedStyle(document.documentElement).getPropertyValue('--spacing-header-height'),
) || 0
const stickyOffset = headerHeight + 24
const top = el.getBoundingClientRect().top
if (top >= stickyOffset) return
const delta = top - stickyOffset
// Nearest scrollable ancestor: the shell's sheet in the new layout, and
// the document in any context that still scrolls that way.
let node: HTMLElement | null = el.parentElement
while (node) {
const style = getComputedStyle(node)
const scrolls = /auto|scroll/.test(style.overflowY)
if (scrolls && node.scrollHeight > node.clientHeight) {
node.scrollTop += delta
return
}
node = node.parentElement
}
window.scrollBy(0, delta)
})
if (action === 'edit') {
await thumb.click({ force: true })
return
}
await thumb.click({ button: 'right', force: true })
const item = media.page().getByRole('menuitem', { name: /^delete$/i })
await expect(item).toBeVisible()
await item.click()
}
export const customFieldsCard = (page: Page) => card(page, /^Custom fields$/)
export const inventoryCard = (page: Page) => card(page, /^Inventory$/)
export const pricesCard = (page: Page) => card(page, /^Prices$/)
export const publishingCard = (page: Page) => card(page, /^Publishing$/)
export const categorizationCard = (page: Page) => card(page, /^Categorization$/)
function productCategoriesCombobox(page: Page) {
return categorizationCard(page)
.getByRole('group')
.filter({ has: page.getByText('Categories', { exact: true }) })
.getByRole('combobox')
}
/** Add a category chip on the product form's Categorization card. */
export async function addProductCategory(page: Page, categoryName: string): Promise<void> {
await productCategoriesCombobox(page).fill(categoryName)
await page
.getByRole('option', { name: new RegExp(`^${categoryName}$`, 'i') })
.first()
.click()
}
/** Remove a category chip from the product form's Categorization card. */
export async function removeProductCategory(page: Page, categoryName: string): Promise<void> {
const card = categorizationCard(page)
await card
.locator('[data-slot="combobox-chip"]', { hasText: categoryName })
.locator('[data-slot="combobox-chip-remove"]')
.click()
}