An All-Green E2E: Three Criteria and One Evidence Gap in nsbp-shop-e2e
The Scene
First, let's clarify the boundaries of this setup: it is the test suite for nsbp-shop, but the repository does not contain nsbp-shop. The application under test is in a neighboring directory; the test side simply points baseURL there:
baseURL: 'http://localhost:3001',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
This is from playwright.config.ts. The following webServer block is conditionally enabled; it only pulls up the service if the environment variable is set:
// 手动启动服务器后运行测试,或设置 START_SERVER=1 自动启动
webServer: process.env.START_SERVER ? {
command: 'cd ../nsbp-shop && npm run dev',
url: 'http://localhost:3001',
reuseExistingServer: true,
timeout: 180 * 1000,
} : undefined,
playwright.config.ts. So the action of "running E2E" in this repository has two prerequisites: manually starting the neighboring service, or turning on START_SERVER to let it start for you (the cd ../nsbp-shop in playwright.config.ts is that cross-directory dependency).
When I first ran it, I used the default setup and forgot to start the service. The result was a browser full of connection failures—that was not a failure of test logic, but noise from an unready environment. After starting the service and running again, the noise disappeared, and the report returned to all-green. In other words, the environment layer was cleanly excluded. The real problem lies deeper.
TL;DR
- This repository does not contain the application under test;
baseURLis hardcoded inplaywright.config.ts, and thewebServerblock relies onSTART_SERVERto decide whether to automatically start the service (playwright.config.ts). - What actually makes the report green is the semantics of the criteria:
count() > 0,A || B || C, andtoHaveURLall accept "it appeared" without requiring "it was done correctly" (tests/products.spec.ts,tests/home.spec.ts,tests/navigation.spec.ts). - All 13
test.skipinstances are on test cases that require a logged-in state (5 intests/cart.spec.ts, 8 intests/user.spec.ts); they do not show up as red in the report, nor as gaps. reporter: 'html'combined withtrace: 'on-first-retry'andscreenshot: 'only-on-failure'means passing test cases leave no evidence, so when trying to trace back "what made this green valid," there is no material (playwright.config.ts,,:13).
Three Dissections: What Makes the Green Valid
After the all-green result, I did not look at "how many passed," but instead picked a few and read the assertions line by line. By the third one, the pattern was already very clear.
Product List: count() > 0 Observes the Container
The test case starting at tests/products.spec.ts, "should correctly display the product list page":
test('应该正确显示商品列表页面', async ({ page }) => {
// 检查页面是否加载成功
await expect(page).toHaveURL(/products/);
// 等待加载完成
await page.waitForTimeout(2000);
// 检查主要内容区域
const productContent = page.locator('main, section');
const hasContent = await productContent.count() > 0;
expect(hasContent).toBe(true);
The object of judgment is main, section (tests/products.spec.ts). This selector matches "there is a main content area container on the page"; it hits as soon as the page skeleton is rendered; whether the product data has returned is completely unobservable by hasContent. Thus, this test case passes even against an empty list.
In the same file, there is actually a selector closer to the business logic, written in the test case below:
// 检查商品卡片或空状态
const productCards = page.locator('a[href*="/product/"]');
const emptyState = page.locator('text=/暂无商品|没有商品|empty/i');
const hasProducts = await productCards.count() > 0;
const isEmpty = await emptyState.count() > 0;
expect(hasProducts || isEmpty).toBe(true);
tests/products.spec.ts. a[href*="/product/"] is the element that actually changes as products appear; the problem lies in the "or" in the second half: the judgment for isEmpty is text=/暂无商品|没有商品|empty/i (tests/products.spec.ts); as long as any of these three words appears anywhere on the page, the whole condition holds, and the truth value of hasProducts no longer affects the conclusion. The empty state text is treated as "this is a legitimate empty list," while the premise that "the list is indeed empty" is not verified.
Navigation: URL Matching Only Observes That the Address Changed
The test case from tests/navigation.spec.ts going from the homepage to the product list ends with one line:
await page.click('a:has-text("全部商品"), a[href="/products"]');
await waitForPageLoad(page);
await expect(page).toHaveURL(/products/);
tests/navigation.spec.ts. The address becoming /products only indicates that the route jumped; whether the target page rendered products or had a 500 error, this assertion does not check. The 404 test case in the same file is more direct—is404 || isHome (tests/navigation.spec.ts); passing if either "displays 404" or "redirects back to homepage" holds, merging two semantically completely different results into one criterion.
Homepage: Three Choices, Equal to Not Specifying an Observation Target
The test case starting at tests/home.spec.ts, "should display category or product area," lists sections, category text, and product text in parallel:
const sections = page.locator('section, main');
const categorySection = page.locator('text=/分类|Category|热门分类/i');
const productSection = page.locator('text=/商品|热门商品/i');
const hasSections = await sections.count() > 0;
const hasCategory = await categorySection.count() > 0;
const hasProduct = await productSection.count() > 0;
expect(hasSections || hasCategory || hasProduct).toBe(true);
tests/home.spec.ts. Among the three, hasSections returns to the section, main container level, so regardless of the page content, this is basically always true. The benefit of three choices is "not easy to go red," the cost is "if it goes red, it doesn't indicate which part is broken."
Absence: 13 Skips and What They Suppress
Besides broad criteria, there is a quieter gap. The entire repository has 13 test.skip instances, all on test cases requiring a logged-in state: 5 in tests/cart.spec.ts, 8 in tests/user.spec.ts. The shape is the same:
test.skip('应该能修改商品数量', async ({ page }) => {
// 此测试需要登录,暂时跳过
await page.goto('/cart');
await waitForPageLoad(page);
const quantityInput = page.locator('input[type="number"], .quantity-input');
tests/cart.spec.ts. The five behavioral chains of changing quantity, deleting products, checking out, shipping address, and submitting orders in the cart are all under this layer of skip; the eight pages in the user center are the same (tests/user.spec.ts).
The nature of skip is different from broad criteria: it explicitly states in the report "this did not run," not pretending to pass. So I look at them separately—skip is absence, broad criteria are false evidence. False evidence is harder to discover because in the report it looks exactly the same as truly passed test cases; absence at least can be counted. Incidentally, the implementation of waitForPageLoad waits for networkidle (tests/helpers/common.ts), which also explains why hard waits like waitForTimeout(2000) appear everywhere in this repository: after network silence, there is still asynchronous rendering, and the author used time to cover that segment.
Evidence Gap: What the Report Configured
Besides criteria, there is a layer of configuration that determines "whether post-hoc review is possible." playwright.config.ts is reporter: 'html', trace: 'on-first-retry' and screenshot: 'only-on-failure'. The combined effect of the three is: passing test cases leave no trace, no screenshot; only failures leave evidence.
This is a reasonable choice in terms of cost—traces take up a lot of space, and capturing only on retry is a common practice. The cost is that when looking back to verify "which frame of the screen made this green valid," the material is not there. Broad criteria combined with only-on-failure evidence, when combined, a suspicious green has neither criterion constraints nor a scene to check.
There is also a small drift in the same repository: .PHONY in Makefile lists install test test-ui test-headed debug report clean codegen help, but not test-auto; while the target at Makefile does exist:
test:
pnpm test
# 运行测试(自动启动服务)
test-auto:
START_SERVER=1 pnpm test
Makefile. It runs START_SERVER=1 pnpm test, combined with the conditional webServer in playwright.config.ts, this is the path with automatic service pulling. This inconsistency between the command list and the target list does not affect execution normally, but it makes it hard to confirm "which startup setup am I actually using"—and this is exactly the thing that needs to be pinned down before troubleshooting.
Fix: Shift the Criterion from "Appeared" to "Behaved Correctly"
The changes are concentrated at the assertion layer; the configuration and command layers remain untouched for now.
First, for the product list test, replace the observation target with an element that varies with the data: the pattern main, section combined with count() > 0 (tests/products.spec.ts) is no longer used as the criterion. Instead, use the already-existing a[href*="/product/"] selector in the same repository (tests/products.spec.ts). The presence of this selector on the page directly follows the product data; it is zero when the list is empty.
Second, split the "or" into two independent assertions: when the product card count is zero, the empty-state message (tests/products.spec.ts) must hold on its own; if both are zero, this test case fails. This way, "products exist" and "valid empty list" become individually verifiable propositions, no longer covering for each other.
Third, for the navigation test, add a visibility check for the landing point after the URL assertion (after tests/navigation.spec.ts). Navigation is only considered successful if the address changed and there is content on the other side.
The three-way choice in home.spec.ts (tests/home.spec.ts) is retained, because the homepage indeed allows for either a "categories section" or a "products section" layout; however, the hasSections item is extracted from the "or" and listed separately to avoid the entire test passing just because the container exists.
Verification: Before-and-After Comparison with the Same Command Set
The execution method is fixed in this repository. Install dependencies once: make install runs pnpm install and pnpm exec playwright install (Makefile``23). Then run each of the two startup modes once to see if the conclusions are consistent: make test corresponds to pnpm test (Makefile``27), and make test-auto corresponds to START_SERVER=1 pnpm test (Makefile``31). Use make report to view the report, which runs pnpm run report (Makefile``47).
The fact that the conclusions are consistent across both modes is valuable in itself: inconsistency indicates that the assertions still depend on environmental timing, and a green result is just luck. I originally used this to rule out "the service didn't start properly," but now I use it in reverse as a regression check for the fix.
The final step is active sabotage: even if everything is still green after the changes, one more test to force a failure is needed, because the new criteria might also be too broad. Remove both the product cards and the empty-state message (for example, by pointing to a product API that returns empty); the modified product list test case should turn red. Only when it turns red does it prove that the criterion is truly attached to an element that changes with the data. Perform the same trick on navigation: make the target page return only the address without rendering content; toHaveURL will still hold, but the added visibility check should turn red.
Retrospective
After going through this cycle, the most valuable takeaway is not a specific fixed assertion, but a sequence for reading green results.
First, ask about the conditions for truth. When you see a green result, don't celebrate immediately; first ask whether its minimal condition for being "true" is "something appeared" or "something was done correctly." If it's the former, that green result needs to be rewritten.
Second, distinguish absence from false evidence. A skip is absence, honestly shown in the report as not run (tests/cart.spec.ts); a broad criterion is false evidence, shown in the report as run, but actually proving nothing (tests/products.spec.ts). The latter has higher priority.
Third, actively sabotage once. Everything being green after changes does not mean it's fixed. Remove the data that the new criterion depends on to force it to turn red once; only then is it properly attached.
These three points do not depend on specific tools and can be used in other projects.
Source Code Navigation
The body names files and symbols only; line numbers are omitted because they drift with every commit — search by symbol or by the quoted snippet.
playwright.config.tstests/helpers/common.tstests/products.spec.tstests/navigation.spec.tstests/home.spec.tstests/auth.spec.tstests/cart.spec.tstests/user.spec.ts- Makefile (build entry, including install / test / test-auto / report targets)
package.json
Why does this repository encounter a bunch of connection failures when running E2E?
The repository does not contain the application under test, and baseURL points to http://localhost:3001 (playwright.config.ts), so the service needs to be started separately. make test assumes you have already started it manually, while make test-auto triggers the webServer block in the configuration via START_SERVER=1 to automatically start the service (Makefile, playwright.config.ts). Failures when the service is not up are environmental noise and are unrelated to the test logic.
What exactly is the problem with the hasContent assertion?
It observes the count of page.locator('main, section') (tests/products.spec.ts), i.e., whether the page has a main content area container. It remains true when the container is present but the product data has not returned, so this test case passes even against an empty list.
Why is hasProducts || isEmpty considered false evidence?
The criterion for isEmpty is whether text=/暂无商品|没有商品|empty/i matches (tests/products.spec.ts). If any of these three words appears anywhere on the page, the entire assertion holds, and the truth value of hasProducts no longer affects the result; the premise that "the list is indeed empty" is not verified.
Where exactly are the 13 skips distributed?
5 in tests/cart.spec.ts (change quantity, delete product, checkout page, shipping address, submit order), and 8 in tests/user.spec.ts (two groups: user center and order list), all of which are behavior test cases requiring a logged-in state. They are shown as skipped in the report, not as failed.
Why can't evidence of passed test cases be found in the report?
playwright.config.ts uses reporter: 'html', captures the trace only on the first retry, and keeps the screenshot only on failure. Therefore, passed test cases have neither a trace nor a screenshot.
How to confirm that the criteria are truly effective after the changes?
Run both startup modes once (make test / make test-auto), and the conclusions must be consistent; then perform active sabotage once by removing both the product cards and the empty-state message, and the modified list test case should turn red. Being all green itself does not constitute proof.
Comments (0)