Skip to content
FrontDepth, Home

Back to the topic

Practice: End-to-end tests with Playwright

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    This is a plain-JS simulation with a virtual clock, not Playwright: isVisible() reads the state once, like locator.isVisible(), and expectToBeVisible() re-checks every 100 ms until a timeout, like a web-first assertion. (The 100 ms step belongs to the simulation; Playwright chooses its own intervals.) The toast appears at 250 ms, then in a slower run at 8000 ms. What does it log?

    JavaScript
    // A plain-JS simulation with a virtual clock, not Playwright itself.
    let now = 0;
    const toast = { appearsAt: 250 };
    const isVisible = () => now >= toast.appearsAt;
    
    function expectToBeVisible({ timeout = 5000 } = {}) {
      const start = now;
      let checks = 0;
      while (true) {
        checks += 1;
        if (isVisible()) return `passed at ${now} ms after ${checks} checks`;
        if (now - start >= timeout) return `timed out after ${timeout} ms`;
        now += 100; // the simulation re-checks every 100 ms
      }
    }
    
    console.log(isVisible());
    console.log(expectToBeVisible());
    
    now = 0;
    toast.appearsAt = 8000;
    console.log(expectToBeVisible());
    
    now = 0;
    console.log(expectToBeVisible({ timeout: 10000 }));
  2. Predict the outputJunior

    This is a plain-JS simulation of how getByRole matches names and how strict locators behave, not Playwright itself: by default the name matches as a case-insensitive substring, exact: true asks for a whole, case-sensitive match, and an action on a locator that matches more than one element throws. What does it log?

    JavaScript
    // A plain-JS simulation of getByRole's name matching and strictness.
    const page = [
      { role: "button", name: "Save" },
      { role: "button", name: "Save draft" },
      { role: "link", name: "Saved views" },
    ];
    
    function getByRole(role, { name, exact = false }) {
      const matches = page.filter(
        (el) =>
          el.role === role &&
          (exact ? el.name === name : el.name.toLowerCase().includes(name.toLowerCase())),
      );
      return {
        count: () => matches.length,
        click() {
          if (matches.length !== 1) {
            throw new Error(`strict mode violation: ${matches.length} elements`);
          }
          return "clicked " + matches[0].name;
        },
      };
    }
    
    console.log(getByRole("button", { name: "save" }).count());
    try {
      console.log(getByRole("button", { name: "Save" }).click());
    } catch (error) {
      console.log(error.message);
    }
    console.log(getByRole("button", { name: "Save", exact: true }).click());
    console.log(getByRole("link", { name: "save" }).click());
    console.log(getByRole("button", { name: "save", exact: true }).count());
  3. Open questionJunior

    What does an end-to-end test prove that a component test in jsdom does not, and what does it cost?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  4. Predict the outputMiddle

    This is a plain-JS simulation of how page.route handlers are chosen, not Playwright itself: the most recently registered matching route runs first, fallback() passes the request to the next matching route, continue() sends it to the network, fulfill() and abort() end it. What does it log?

    JavaScript
    // A plain-JS simulation of how page.route handlers are chosen, not Playwright itself.
    const routes = [];
    function route(pattern, handler) {
      routes.unshift({ pattern, handler }); // the newest route is tried first
    }
    
    function request(url) {
      for (const { pattern, handler } of routes) {
        if (!pattern.test(url)) continue;
        let outcome = "fallback";
        handler({
          fulfill: ({ status }) => (outcome = "mocked " + status),
          abort: () => (outcome = "aborted"),
          continue: () => (outcome = "network"),
          fallback: () => (outcome = "fallback"),
        });
        if (outcome !== "fallback") return url + " -> " + outcome;
      }
      return url + " -> network";
    }
    
    route(/\/api\//, (r) => r.fulfill({ status: 200 }));
    route(/\/api\/tasks/, (r) => r.fallback());
    route(/\/api\/profile/, (r) => r.continue());
    route(/\/api\/billing/, (r) => r.abort());
    
    console.log(request("/api/tasks"));
    console.log(request("/api/profile"));
    console.log(request("/api/billing"));
    console.log(request("/api/teams"));
    console.log(request("/assets/app.js"));
  5. Open questionMiddle

    How do you handle authentication in a Playwright suite without logging in through the form in every test?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Open questionMiddle

    When would you mock the network in a Playwright test, and how do you do it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    A Playwright test fails on CI about once in twenty runs and passes on retry. How do you investigate and fix it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.