メインコンテンツにスキップ

JavaScript の評価

イントロダクション

Playwright スクリプトは Playwright 環境で実行されます。ページスクリプトはブラウザページ環境で実行されます。これらの環境は交差しません。異なるプロセス内の異なる仮想マシンで、場合によっては異なるコンピュータ上で実行されています。

page.evaluate() API は、ウェブページのコンテキストで JavaScript 関数を実行し、結果を Playwright 環境に持ち帰ることができます。evaluate では、windowdocument などのブラウザグローバルを使用できます。

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)

評価引数

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 })

初期化スクリプト

ロードを開始する前にページで何かを評価すると便利な場合があります。たとえば、モックやテストデータを設定したい場合があります。

この場合は、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")