跳到主要內容

評估 JavaScript

簡介

Playwright 腳本在您的 Playwright 環境中執行。您的頁面腳本在瀏覽器頁面環境中執行。這些環境不會互相影響,它們在不同的虛擬機器、不同的進程中執行,甚至可能在不同的電腦上執行。

page.evaluate() API 可以在網頁的上下文中執行 JavaScript 函數,並將結果帶回 Playwright 環境。瀏覽器全域變數,例如 windowdocument,可以在 evaluate 中使用。

href = page.evaluate('() => document.location.href')

如果結果是 Promise,或者函數是非同步的,evaluate 將自動等待直到它被解析。

status = page.evaluate("""async () => {
response = await fetch(location.href)
return response.status
}""")

不同的環境

評估的腳本在瀏覽器環境中執行,而您的測試在測試環境中執行。這表示您無法在頁面中使用測試中的變數,反之亦然。相反地,您應該將它們作為引數明確傳遞。

以下程式碼片段是錯誤的,因為它直接使用了變數

data = "some data"
result = page.evaluate("""() => {
// WRONG: there is no "data" in the web page.
window.myApp.use(data)
}""")

以下程式碼片段是正確的,因為它將值作為引數明確傳遞

data = "some data"
# Pass |data| as a parameter.
result = page.evaluate("""data => {
window.myApp.use(data)
}""", data)

評估引數

Playwright 評估方法,例如 page.evaluate(),接受單個可選引數。此引數可以是 可序列化 值和 JSHandle 實例的混合。處理 (Handles) 會自動轉換為它們所代表的值。

# A primitive value.
page.evaluate('num => num', 42)

# An array.
page.evaluate('array => array.length', [1, 2, 3])

# An object.
page.evaluate('object => object.foo', { 'foo': 'bar' })

# A single handle.
button = page.evaluate_handle('window.button')
page.evaluate('button => button.textContent', button)

# Alternative notation using JSHandle.evaluate.
button.evaluate('(button, from) => button.textContent.substring(from)', 5)

# Object with multiple handles.
button1 = page.evaluate_handle('window.button1')
button2 = page.evaluate_handle('.button2')
page.evaluate("""o => o.button1.textContent + o.button2.textContent""",
{ 'button1': button1, 'button2': button2 })

# Object destructuring works. Note that property names must match
# between the destructured object and the argument.
# Also note the required parenthesis.
page.evaluate("""
({ button1, button2 }) => button1.textContent + button2.textContent""",
{ 'button1': button1, 'button2': button2 })

# Array works as well. Arbitrary names can be used for destructuring.
# Note the required parenthesis.
page.evaluate("""
([b1, b2]) => b1.textContent + b2.textContent""",
[button1, button2])

# Any mix of serializables and handles works.
page.evaluate("""
x => x.button1.textContent + x.list[0].textContent + String(x.foo)""",
{ 'button1': button1, 'list': [button2], 'foo': None })

初始化腳本

有時在頁面開始載入之前評估某些內容是很方便的。例如,您可能想要設定一些模擬 (mocks) 或測試資料。

在這種情況下,請使用 page.add_init_script()browser_context.add_init_script()。在下面的範例中,我們將把 Math.random() 替換為常數值。

首先,建立一個包含模擬 (mock) 的 preload.js 檔案。

// preload.js
Math.random = () => 42;

接下來,將初始化腳本新增到頁面。

# In your test, assuming the "preload.js" file is in the "mocks" directory.
page.add_init_script(path="mocks/preload.js")