JavaScript の評価
イントロダクション
Playwright スクリプトは Playwright 環境で実行されます。ページスクリプトはブラウザページ環境で実行されます。これらの環境は交差しません。異なるプロセス内の異なる仮想マシンで、場合によっては異なるコンピュータ上で実行されています。
page.evaluate() API は、ウェブページのコンテキストで JavaScript 関数を実行し、結果を Playwright 環境に持ち帰ることができます。evaluate
では、window
や document
などのブラウザグローバルを使用できます。
- 同期
- 非同期
href = page.evaluate('() => document.location.href')
href = await page.evaluate('() => document.location.href')
結果が Promise の場合、または関数が非同期の場合、evaluate は自動的に解決されるまで待機します。
- 同期
- 非同期
status = page.evaluate("""async () => {
response = await fetch(location.href)
return response.status
}""")
status = await 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"
result = await 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)
data = "some data"
# Pass |data| as a parameter.
result = await page.evaluate("""data => {
window.myApp.use(data)
}""", data)
評価引数
page.evaluate() のような Playwright 評価メソッドは、単一のオプションの引数を取ります。この引数は、シリアライズ可能な値とJSHandle インスタンスの組み合わせにすることができます。ハンドルは、それらが表す値に自動的に変換されます。
- 同期
- 非同期
# 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 })
# A primitive value.
await page.evaluate('num => num', 42)
# An array.
await page.evaluate('array => array.length', [1, 2, 3])
# An object.
await page.evaluate('object => object.foo', { 'foo': 'bar' })
# A single handle.
button = await page.evaluate_handle('button')
await page.evaluate('button => button.textContent', button)
# Alternative notation using JSHandle.evaluate.
await button.evaluate('(button, from) => button.textContent.substring(from)', 5)
# Object with multiple handles.
button1 = await page.evaluate_handle('window.button1')
button2 = await page.evaluate_handle('window.button2')
await 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.
await 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.
await page.evaluate("""
([b1, b2]) => b1.textContent + b2.textContent""",
[button1, button2])
# Any mix of serializables and handles works.
await page.evaluate("""
x => x.button1.textContent + x.list[0].textContent + String(x.foo)""",
{ 'button1': button1, 'list': [button2], 'foo': None })
初期化スクリプト
ロードを開始する前にページで何かを評価すると便利な場合があります。たとえば、モックやテストデータを設定したい場合があります。
この場合は、page.add_init_script() または browser_context.add_init_script() を使用してください。以下の例では、Math.random()
を定数値に置き換えます。
まず、モックを含む 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")
# In your test, assuming the "preload.js" file is in the "mocks" directory.
await page.add_init_script(path="mocks/preload.js")