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, test } from '@playwright/test'
import {
adminRequest,
FIXTURE_BULK_CHANNEL_NAME,
gotoIndex,
login,
saveCustomFieldInSheet,
withCustomFieldDefinition,
} from './helpers'
const DELIVERY_PROFILES_PATH = (storeId: string) => `/${storeId}/settings/delivery-profiles`
const PROFILE_CTA = /add profile/i
/** The select inside a method-form field, found by the field's visible label. */
function fieldSelect(scope: Locator | Page, label: string): Locator {
const page = 'page' in scope ? scope.page() : scope
return scope
.locator('[data-slot="field"]')
.filter({ has: page.getByText(label, { exact: true }) })
.getByRole('combobox')
}
async function createProfile(page: Page, name: string, kind?: RegExp) {
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(name)
if (kind) {
await fieldSelect(page.getByRole('dialog'), 'Kind').click()
await page.getByRole('option', { name: kind }).click()
}
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name })).toBeVisible({ timeout: 15_000 })
}
test.describe('delivery profiles', () => {
test('lists delivery profiles including the store default', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
// Every store is seeded with a default profile, so the list is never empty.
await expect(page.getByText(/^default$/i).first()).toBeVisible({ timeout: 15_000 })
})
// Filled by the probes in src/testing/slot-probes.tsx.
test('renders the plugin slots on a profile page', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
await createProfile(page, `E2E Slots ${Date.now()}`)
await expect(page.getByText('Slot probe: delivery_profile.form_main')).toBeVisible()
await expect(page.getByText('Slot probe: delivery_profile.form_sidebar')).toBeVisible()
})
test('creates a profile, a zone inside it, and a method inside the zone', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const profileName = `E2E Profile ${stamp}`
const zoneName = `E2E Zone ${stamp}`
const methodName = `E2E Method ${stamp}`
// ---- profile -----------------------------------------------------------
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
// Creating a profile lands on its detail page, where zones and methods live.
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
// ---- zone --------------------------------------------------------------
await page.getByRole('button', { name: /add delivery zone/i }).click()
await expect(page.getByRole('heading', { name: /new delivery zone/i })).toBeVisible()
await page.locator('#name').fill(zoneName)
// Coverage is picked from the store's market countries: search narrows the
// list, then the country's own checkbox selects the whole country.
const zoneSheet = page.getByRole('dialog')
await zoneSheet.getByPlaceholder(/search countries and regions/i).fill('United States')
await zoneSheet.getByRole('checkbox', { name: /^united states$/i }).check()
// The sheets' plugin slots, filled by src/testing/slot-probes.tsx.
await expect(zoneSheet.getByText('Slot probe: delivery_zone.form_fields (new)')).toBeVisible()
await page.getByRole('button', { name: /create delivery zone/i }).click()
await expect(page.getByText(zoneName)).toBeVisible({ timeout: 15_000 })
// ---- method ------------------------------------------------------------
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
// The method form is a sheet over the profile, so the profile stays put
// behind it rather than being navigated away from. The modal sheet marks
// the page behind as aria-hidden, which removes it from role queries —
// assert through a CSS locator instead.
await expect(page.locator('h1', { hasText: profileName })).toBeVisible()
// Started from inside a zone, so the zone is already answered — asking
// again could silently file the method under a different one.
await expect(page.getByRole('dialog').getByText(/^zone$/i)).toHaveCount(0)
await page.getByRole('dialog').locator('#name').fill(methodName)
await expect(
page.getByRole('dialog').getByText('Slot probe: delivery_method.form_fields (new)'),
).toBeVisible()
await page.getByRole('button', { name: /create delivery method/i }).click()
// Saving closes the sheet and the method appears under its zone.
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await expect(page.getByText(methodName)).toBeVisible({ timeout: 15_000 })
})
// The calculator is named on the wire by its `api_type` shorthand
// (`flat_rate`), and that same value selects the per-currency amount editor.
// A mismatch between the two would leave the picker blank and quietly create
// the method free, so drive both through the UI.
test('prices a method with a flat rate, in the store currency', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const profileName = `E2E Priced ${stamp}`
const methodName = `E2E Flat ${stamp}`
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
const sheet = page.getByRole('dialog')
await sheet.locator('#name').fill(methodName)
// The picker is built from `GET /delivery_methods/calculators`, so a
// readable "Flat rate" option proves the catalog resolved.
const calculator = sheet.locator('#calculator-type')
await expect(calculator).toBeEnabled({ timeout: 15_000 })
await calculator.click()
await page.getByRole('option', { name: /flat rate/i }).click()
await expect(calculator).toContainText(/flat rate/i)
// Flat rate is amount-based, so the per-currency editor replaces the
// generic preference rendering.
const amount = sheet.locator('[id^="calculator-amount-"]').first()
await expect(amount).toBeVisible({ timeout: 10_000 })
await amount.fill('7.50')
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await expect(page.getByText(methodName)).toBeVisible({ timeout: 15_000 })
// Reopening hydrates from the persisted row: the calculator and its amount
// both round-tripped rather than saving as a free method.
await page.getByText(methodName).click()
const reopened = page.getByRole('dialog')
await expect(reopened.locator('#calculator-type')).toContainText(/flat rate/i, {
timeout: 15_000,
})
await expect(reopened.locator('[id^="calculator-amount-"]').first()).toHaveValue(/7\.50|7\.5/)
})
// A method's settings are typed: each currency keeps its own amount, the
// eligibility rules keep theirs, and switching the calculator starts over
// from the new one's defaults instead of carrying the old one's keys.
test('keeps per-currency amounts and conditions, and resets settings when the calculator changes', async ({
page,
}) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const methodName = `E2E Typed ${stamp}`
await createProfile(page, `E2E Typed Profile ${stamp}`)
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
const sheet = page.getByRole('dialog')
await sheet.locator('#name').fill(methodName)
const calculator = sheet.locator('#calculator-type')
await expect(calculator).toBeEnabled({ timeout: 15_000 })
await calculator.click()
await page.getByRole('option', { name: /^flat rate$/i }).click()
await sheet.locator('#calculator-amount-USD').fill('5.00')
await sheet.locator('#calculator-amount-EUR').fill('4.50')
await sheet.getByRole('button', { name: /^add condition$/i }).click()
await page.getByRole('menuitem', { name: /^channel$/i }).click()
await sheet.getByPlaceholder(/search sales channels/i).fill(FIXTURE_BULK_CHANNEL_NAME)
await page.getByRole('option', { name: FIXTURE_BULK_CHANNEL_NAME }).click()
await sheet.getByRole('button', { name: /^add condition$/i }).click()
await page.getByRole('menuitem', { name: /^item total$/i }).click()
await sheet.locator('#preference-minimum_amount').fill('25')
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
// Both conditions and both amounts come back as saved.
await page.getByText(methodName).click()
await expect(sheet.locator('#calculator-amount-USD')).toHaveValue(/^5(\.0+)?$/, {
timeout: 15_000,
})
await expect(sheet.locator('#calculator-amount-EUR')).toHaveValue(/^4\.50?$/)
await expect(sheet.getByText(FIXTURE_BULK_CHANNEL_NAME)).toBeVisible()
await expect(sheet.locator('#preference-minimum_amount')).toHaveValue(/^25(\.0+)?$/)
// Changing one currency leaves the other alone.
await sheet.locator('#calculator-amount-USD').fill('6.25')
await sheet.getByRole('button', { name: /^save$/i }).click()
await expect(sheet).toHaveCount(0, { timeout: 15_000 })
await page.getByText(methodName).click()
await expect(sheet.locator('#calculator-amount-USD')).toHaveValue('6.25', { timeout: 15_000 })
await expect(sheet.locator('#calculator-amount-EUR')).toHaveValue(/^4\.50?$/)
// A different calculator starts from its own defaults.
await calculator.click()
await page.getByRole('option', { name: /^flexi rate$/i }).click()
await expect(sheet.locator('#calculator-amount-USD')).toHaveCount(0)
await expect(sheet.locator('#preference-first_item')).toHaveValue(/^0(\.0+)?$/)
await sheet.locator('#preference-first_item').fill('3.00')
await sheet.getByRole('button', { name: /^save$/i }).click()
await expect(sheet).toHaveCount(0, { timeout: 15_000 })
await page.getByText(methodName).click()
await expect(calculator).toContainText(/flexi rate/i, { timeout: 15_000 })
await expect(sheet.locator('#preference-first_item')).toHaveValue(/^3(\.0+)?$/)
await expect(sheet.getByText(FIXTURE_BULK_CHANNEL_NAME)).toBeVisible()
})
// A currency left blank is not offered, the store's default currency
// included, and a method with no amount in any currency is refused rather
// than saved as one nobody can be quoted.
test('leaves a blank currency out, the store default included, and asks for at least one amount', async ({
page,
}) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const methodName = `E2E Euro Only ${stamp}`
await createProfile(page, `E2E Euro Only Profile ${stamp}`)
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
const sheet = page.getByRole('dialog')
await sheet.locator('#name').fill(methodName)
const calculator = sheet.locator('#calculator-type')
await expect(calculator).toBeEnabled({ timeout: 15_000 })
await calculator.click()
await page.getByRole('option', { name: /^flat rate$/i }).click()
await expect(sheet.locator('#calculator-amount-USD')).toHaveValue('')
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(sheet.getByText(/enter an amount in at least one currency/i)).toBeVisible()
await sheet.locator('#calculator-amount-EUR').fill('4.50')
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await page.getByText(methodName).click()
await expect(sheet.locator('#calculator-amount-EUR')).toHaveValue(/^4\.50?$/, {
timeout: 15_000,
})
await expect(sheet.locator('#calculator-amount-USD')).toHaveValue('')
})
// Opening a method is a search-param change, so the sheet must survive a
// reload and a direct link — that is what keeps bookmarked methods working.
test('opens the method sheet straight from a URL', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const profileName = `E2E Deeplink Profile ${Date.now()}`
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
await page.getByRole('button', { name: /offer pickup/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
// Reloading the URL the sheet put in the address bar reopens it, over a
// profile page that rendered underneath.
await page.reload()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
// Behind a modal sheet the page is aria-hidden, so role queries can't see
// the heading — a CSS locator proves it stayed rendered.
await expect(page.locator('h1', { hasText: profileName })).toBeVisible()
// Cancelling leaves the profile behind, with no sheet left open.
await page.getByRole('button', { name: /^cancel$/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0)
await expect(page.getByRole('heading', { name: profileName })).toBeVisible()
// The sheet replaces its history entry rather than pushing one, so going
// back does not reopen what the merchant just dismissed.
await page.goBack()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0)
})
// Pickup is a separate card because it has no destination: the merchant
// reaches the method form through it, and that form must drop the zone.
test('offers pickup from a fresh profile, on a form with no zone', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const profileName = `E2E Pickup Profile ${Date.now()}`
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
// A brand-new profile collects nowhere yet, so the pickup card offers the
// way in rather than listing methods.
await expect(page.getByText(/customers cannot collect orders/i)).toBeVisible({
timeout: 15_000,
})
await page.getByRole('button', { name: /offer pickup/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
// Pickup preselected: collection counters replace the zone entirely.
const sheet = page.getByRole('dialog')
await expect(sheet.getByText(/collection locations/i)).toBeVisible({ timeout: 15_000 })
await expect(sheet.getByText(/^zone$/i)).toHaveCount(0)
})
// A profile with no zones still needs a way in: a method offered wherever
// the profile reaches, which is what a carrier quoting worldwide wants.
test('adds a method to a profile that has no zones', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const profileName = `E2E Zoneless Profile ${Date.now()}`
const methodName = `E2E Zoneless Method ${Date.now()}`
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
await page.getByRole('dialog').locator('#name').fill(methodName)
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await expect(page.getByText(methodName)).toBeVisible({ timeout: 15_000 })
// Opening and closing the sheet must not leave the header's back arrow
// walking through dismissed sheets — it goes to the profile list.
await page.getByRole('button', { name: /^back$/i }).click()
await expect(page.getByRole('button', { name: PROFILE_CTA })).toBeVisible({ timeout: 15_000 })
})
// The origin-group layer stays invisible until a profile is split, so this
// covers both directions: splitting reveals the per-group sections, and
// deleting the second group returns the page to its flat rendering.
test('splits shipping origins into a second group, then collapses back', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const profileName = `E2E Origins Profile ${Date.now()}`
const groupName = 'EU warehouse'
await page.getByRole('button', { name: PROFILE_CTA }).click()
await expect(page.getByRole('heading', { name: /new delivery profile/i })).toBeVisible()
await page.locator('#name').fill(profileName)
await page.getByRole('button', { name: /create profile/i }).click()
await expect(page.getByRole('heading', { name: profileName })).toBeVisible({ timeout: 15_000 })
// A fresh profile has one nameless group, so no group header renders yet
// ("Ships from" itself also appears in the Locations card — assert on the
// group name, which cannot exist before the split).
await expect(page.getByText(groupName)).toHaveCount(0)
await page.getByRole('button', { name: /split shipping origins/i }).click()
await expect(page.getByRole('heading', { name: /split shipping origins/i })).toBeVisible()
await page.locator('#origin-group-name').fill(groupName)
await page
.getByRole('dialog')
.getByRole('button', { name: /^split shipping origins$/i })
.click()
// Two groups now: the nameless default renders as "All locations", the new
// one under its own name.
await expect(page.getByText(groupName)).toBeVisible({ timeout: 15_000 })
await expect(page.getByText(/ships from all locations/i)).toHaveCount(2)
// Deleting the empty second group takes the profile back to one group,
// and the group framing disappears with it.
// The last actions menu on the page belongs to the group just created,
// which renders below the default one.
await page
.getByRole('button', { name: /open actions/i })
.last()
.click()
await page.getByRole('menuitem', { name: /delete/i }).click()
await page
.getByRole('dialog')
.getByRole('button', { name: /^delete$/i })
.click()
await expect(page.getByText(groupName)).toHaveCount(0, { timeout: 15_000 })
})
// Profile kinds and fulfillment providers are both named by short values on
// the wire; the digital kind must offer, preselect and persist the digital
// provider, and reopening must find it in the provider catalog again.
test('creates a digital profile with a digital method that reopens as digital', async ({
page,
}) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const methodName = `E2E Download ${stamp}`
await createProfile(page, `E2E Digital Profile ${stamp}`, /^digital$/i)
await page.getByRole('button', { name: /add digital method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
const sheet = page.getByRole('dialog')
await expect(fieldSelect(sheet, 'Fulfillment provider')).toContainText(/^digital$/i, {
timeout: 15_000,
})
await sheet.locator('#name').fill(methodName)
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await page.getByText(methodName).click()
await expect(fieldSelect(page.getByRole('dialog'), 'Fulfillment provider')).toContainText(
/^digital$/i,
{ timeout: 15_000 },
)
})
// A method left on the default would reopen as Internal whether or not the
// stored provider round-tripped, so pick the other built-in one.
test('prices a method through the freight rate provider with a markup, and it reopens that way', async ({
page,
}) => {
const creds = await login(page)
await gotoIndex(page, DELIVERY_PROFILES_PATH(creds.store_id), PROFILE_CTA)
const stamp = Date.now()
const methodName = `E2E Freight ${stamp}`
await createProfile(page, `E2E Freight Profile ${stamp}`)
await page
.getByRole('button', { name: /add method/i })
.first()
.click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toBeVisible({
timeout: 15_000,
})
const sheet = page.getByRole('dialog')
await sheet.locator('#name').fill(methodName)
// A flat rate left without amounts must not block a method whose pricing fields the provider hides.
const calculator = sheet.locator('#calculator-type')
await expect(calculator).toBeEnabled({ timeout: 15_000 })
await calculator.click()
await page.getByRole('option', { name: /^flat rate$/i }).click()
await expect(sheet.locator('#calculator-amount-USD')).toHaveValue('')
const rateProvider = fieldSelect(sheet, 'Rate provider')
await expect(rateProvider).toContainText(/^internal$/i, { timeout: 15_000 })
await rateProvider.click()
await page.getByRole('option', { name: /^freight$/i }).click()
await expect(rateProvider).toContainText(/^freight$/i)
await expect(sheet.locator('#calculator-amount-USD')).toHaveCount(0)
// A provider-priced method carries the store's markup on top of the quote.
await sheet.locator('#markup_percent').fill('7.5')
await sheet.locator('#markup_flat').fill('2.50')
await page.getByRole('button', { name: /create delivery method/i }).click()
await expect(page.getByRole('heading', { name: /new delivery method/i })).toHaveCount(0, {
timeout: 15_000,
})
await page.getByText(methodName).click()
const reopened = page.getByRole('dialog')
await expect(fieldSelect(reopened, 'Rate provider')).toContainText(/^freight$/i, {
timeout: 15_000,
})
await expect(reopened.locator('#markup_percent')).toHaveValue('7.5')
await expect(reopened.locator('#markup_flat')).toHaveValue('2.50')
})
// An existing method: saving only a custom field leaves the method itself
// untouched, so the shared fixture stays as it was.
test('saves a custom field with the delivery method', async ({ page }) => {
const creds = await login(page)
await withCustomFieldDefinition(page, creds, 'delivery_method', async (label) => {
const methods = await adminRequest<{
data: { id: string; delivery_profile_id: string | null }[]
}>(page, creds, 'get', '/delivery_methods?limit=25')
const method = methods.data.find((candidate) => candidate.delivery_profile_id)
if (!method) throw new Error('no delivery method on a profile in the fixtures')
const url = `${DELIVERY_PROFILES_PATH(creds.store_id)}/${method.delivery_profile_id}?method=${method.id}`
await page.goto(url)
await saveCustomFieldInSheet(page, url, label, 'Carrier contract 12')
})
})
})