arrow-left Back to topicQuizEvaluating JavaScriptCheck yourself1.You need to read a value from window.__APP_STATE__ in a test. Which approach is correct?const state = window.__APP_STATE__ — access it directly in the testconst state = await page.evaluate(() => window.__APP_STATE__)const state = await page.locator('window.__APP_STATE__').textContent()2.You want to replace Math.random() with a fixed value for reproducible tests. When must you inject this replacement?After page.goto() — once the page is loadedBefore page.goto() using page.addInitScript() — before any page JS runsThe order doesn't matter — Math.random is always replaceable3.Inside a page.evaluate() callback, you try to use a variable declared in your test scope. Why does this fail?Playwright doesn't support closures inside evaluate.The callback runs in the browser process (a separate VM) — JavaScript closures don't cross the Node.js/browser boundary.The variable is out of scope because evaluate runs asynchronously.You need to use page.evaluateHandle() instead for variable access.4.How do you read the auth_token value from localStorage in a Playwright test?const token = localStorage.getItem('auth_token')const token = await page.evaluate(() => localStorage.getItem('auth_token'))const token = await page.locator('localStorage').getAttribute('auth_token')const token = await page.storageState().localStorage['auth_token']5.You want to dispatch a custom DOM event 'order:updated' from your test. Which method do you use?page.dispatchEvent('order:updated', { detail: {} })page.evaluate(() => window.dispatchEvent(new CustomEvent('order:updated', { detail: {} })))page.emit('order:updated', { detail: {} })page.trigger('order:updated')6.What does context.addInitScript() do differently from page.addInitScript()?context.addInitScript() runs the script after every page load; page.addInitScript() runs only once.context.addInitScript() applies the script to every new page opened in that context; page.addInitScript() applies only to that specific page.They are identical — context.addInitScript() is just an alias.context.addInitScript() runs the script in Node.js, not in the browser.7.You need to make a fetch request from inside the browser (with the browser's own cookies/session) and get the result back in Node.js. Which approach works?Use page.request.get('/api/orders') — it runs in the browser context.Use page.evaluate(async () => { const r = await fetch('/api/orders'); return r.json() }) — async evaluate runs the fetch in the browser.Use node-fetch in your test — it automatically shares browser cookies.Use page.route('/api/orders', handler) to intercept and return the data.8.What is the return value constraint for page.evaluate()?It can return any JavaScript value including DOM nodes and functions.It must return a JSON-serializable value — DOM nodes, functions, and circular references are not supported.It can only return strings.It must return a Promise — synchronous return values are ignored.Submit answersarrow-left PreviousDownloadsHandlesNext arrow-right