arrow-left Back to topicQuizMock browser APIsCheck yourself1.Why must page.addInitScript() be called BEFORE page.goto()?It's just convention — the order doesn't actually matterThe page may call the browser API early during load — the mock must be in place before any page JS runspage.addInitScript() doesn't work after navigation2.You want to test a feature that checks navigator.cookieEnabled. But navigator.cookieEnabled = false has no effect. Why, and what's the fix?Use page.evaluate(() => navigator.cookieEnabled = false) — direct assignment only works from inside the browser.navigator.cookieEnabled is read-only — use Object.defineProperty(Object.getPrototypeOf(navigator), 'cookieEnabled', { get: () => false, configurable: true }) inside addInitScript().Set cookiesEnabled: false in the browser launch options.Use context.clearCookies() to simulate a state where cookies are blocked.3.How do you test that a battery warning banner appears when the battery level drops below 20% mid-session (not just on initial load)?Reload the page with a different mock battery level for each test.Expose the mock battery object on window via addInitScript(), then trigger level changes via page.evaluate(() => window.__batteryMock._setLevel(0.15)).Use context.setOffline(true) — it triggers battery-related events.Call navigator.getBattery() directly from Node.js to change the mock state.4.What does page.exposeFunction('logCall', handler) do?It exposes a browser-side function to Node.js for inspection.It makes window.logCall available in the browser — when called, it invokes the handler function in Node.js.It adds a console.log wrapper around all browser API calls.It creates a shared memory space between Node.js and the browser.5.Your app checks navigator.getBattery() on page load. You write a mock in page.evaluate() after page.goto(). The mock doesn't take effect. Why?You must use page.route() to intercept the Battery API.The page already called getBattery() during initialization — page.evaluate() runs after the page loads, so the mock is too late.The Battery API cannot be mocked at all in Playwright.You need to use page.on('getBattery', handler) to intercept the call.6.Which scenario is a good use case for mocking browser APIs with addInitScript(), rather than using page.route()?Intercepting HTTP API calls to your backend.Faking device hardware APIs like battery level, geolocation (beyond context config), or matchMedia that don't involve HTTP requests.Blocking third-party analytics scripts.Mocking REST API responses with specific status codes.7.You want to verify that the app correctly subscribes to chargingchange and levelchange battery events. How do you capture which events the app actually listened for?Use page.on('customevent', handler) to observe DOM custom events.Use page.exposeFunction('logCall', msg => calls.push(msg)), then in the mock's addEventListener, call window.logCall('addEventListener:' + eventName).Use page.evaluate(() => window.__batteryListeners) after goto to read the listener list.Use page.waitForEvent('chargingchange') — Playwright tracks battery events natively.8.The Battery API mock returns level: 0.15 on page load. Your test asserts the warning banner is visible. But the test is flaky — sometimes the banner doesn't appear. What's the most likely cause?The level: 0.15 value is too close to the 0.20 threshold — use a lower value.The addInitScript() call is placed after page.goto() — the page occasionally reads the real battery state before the mock is registered.The Battery API is asynchronous — the test should await a longer timeout for the banner.The mock object needs a dispose() call to activate it.Submit answersarrow-left PreviousComponents (experimental)Other locatorsNext arrow-right