現在選択されているフレームのコンテキストで実行するために、ページにJavaScriptのスニペットを挿入します。実行されるスクリプトは非同期であると想定されます。

挿入される関数は、非同期操作が完了したときに呼び出す必要があるdoneコールバックを引数として受け取ります。doneコールバックに渡された値がクライアントに返されます。
挿入された関数の追加の引数は、doneコールバックの前に渡される空でない配列として渡すことができます。

非同期スクリプトコマンドは、ページの読み込みをまたぐことはできません。スクリプトの結果を待っている間にアンロードイベントが発生すると、エラーが返されます。

使用方法

                    .executeAsync(body, [args], [callback])
                
                    .executeAsyncScript(body, [args], [callback])
                
                    .document.executeAsync(body, [args], [callback])
                
                    .document.executeAsyncScript(body, [args], [callback])
                

describe('execute async script', function() {
  it('executes async script in browser', function(browser) {
    browser.executeAsyncScript(function(done) {
      setTimeout(function() {
        done(true);
      }, 500);
    }, function(result) {
      // whatever is passed to the `done` callback in the script above
      // will be available as result.value
      console.log(result.value); // true
    });
  });

  it('executes a script with ES6 async/await', async function(browser) {
    const result = await browser
      .document.executeAsync(function(arg1, arg2, done) {
        setTimeout(function() {
          done(arg1);
        }, 500);
      }, [arg1, arg2]);

    // whatever is passed to the `done` callback in the script above
    // will be returned by the command when used with `await`.
    console.log(result); // arg1
  });
});

パラメータ

名前 説明
body 文字列 | 関数

挿入する関数本体。

args 配列

関数に渡される引数の配列。

callback
オプション
関数

コマンドが完了したときに呼び出されるオプションのコールバック関数。

戻り値

説明
*

スクリプトの結果。

参照

W3C WebDriver仕様