execute
將一段 JavaScript 程式碼片段注入頁面,以便在目前所選框架的上下文中執行。假設執行的腳本是同步的,並且腳本的評估結果會返回給客戶端。
script 引數以函式主體的形式定義要執行的腳本。該函式返回的值將返回給客戶端。該函式將使用提供的 args 陣列調用,並且可以透過 arguments 物件以指定的順序存取這些值。
引數可以是任何 JSON 基本類型、陣列或 JSON 物件。定義 WebElement 參考的 JSON 物件將轉換為對應的 DOM 元素。同樣地,腳本結果中的任何 WebElements 都會以 WebElement JSON 物件的形式返回給客戶端。
用法
browser.execute(script, arguments)
參數
名稱 | 類型 | 詳細資訊 |
---|---|---|
script | String 、Function | 要執行的腳本。 |
arguments 選填 | param | 腳本引數 |
範例
execute.js
it('should inject javascript on the page', async () => {
const result = await browser.execute((a, b, c, d) => {
// browser context - you may not access client or console
return a + b + c + d
}, 1, 2, 3, 4)
// node.js context - client and console are available
console.log(result) // outputs: 10
});