現在選択されているフレームのコンテキストで実行するために、JavaScriptのスニペットをページに挿入します。実行されるスクリプトは同期とみなされます。
script引数は、関数本体の形式で実行するスクリプトを定義します。その関数が返す値はクライアントに返されます。

関数は、指定されたargs配列で呼び出され、値は指定された順序でargumentsオブジェクトからアクセスできます。

内部的には、`body`パラメータが関数の場合、`function.toString()`を使用して文字列に変換されます。現在のスコープへの参照は無視されます。

クロスブラウザの互換性を確保するために、指定された関数はES6形式(つまり `() => {}`)ではない必要があります。関数の実行が失敗した場合、コールバックの最初の引数にエラー情報が含まれます。

使用方法

                    .execute(body, [args], [callback])
                
                    .executeScript(body, [args], [callback])
                
                    .document.execute(body, [args], [callback])
                
                    .document.executeScript(body, [args], [callback])
                

describe('execute script', function() {
  it('executes a script in browser', function(browser) {
    browser.executeScript(function(imageData) {
      // resize operation
      return true;
    }, [imageData], function(result) {
      // whatever is returned by the script passed above will be available
      // as result.value
      console.log(result.value); // true
    });

    // scroll to the bottom of the page.
    browser.executeScript('window.scrollTo(0,document.body.scrollHeight);');
  });

  it('executes a script with ES6 async/await', async function(browser) {
    const result = await browser
      .document.executeScript(function(imageData) {
        // resize operation
        return true;
      }, [imageData]);

    console.log(result); // true
  });
});

パラメータ

名前 説明
body 文字列 | 関数

挿入する関数本体。

args 配列

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

callback
オプション
関数

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

戻り値

説明
*

スクリプトの結果。

参照

W3C WebDriver仕様