JavaScriptの評価
はじめに
PlaywrightスクリプトはPlaywright環境で実行されます。ページのスクリプトはブラウザのページ環境で実行されます。これらの環境は互いに交差せず、異なるプロセス、異なる仮想マシン、そして場合によっては異なるコンピューター上で実行されます。
page.evaluate() APIは、ウェブページのコンテキストでJavaScript関数を実行し、結果をPlaywright環境に返すことができます。window
やdocument
のようなブラウザグローバルは、evaluate
で使用できます。
- 同期
- 非同期
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)
評価引数
Playwrightの評価メソッド(page.evaluate()など)は、単一のオプション引数を取ります。この引数は、シリアライズ可能な値と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")