Electron
Playwright 具有 experimental 支援 Electron 自動化。您可以使用 electron 命名空間透過
const { _electron } = require('playwright');
Electron 自動化腳本的範例會是
const { _electron: electron } = require('playwright');
(async () => {
// Launch Electron app.
const electronApp = await electron.launch({ args: ['main.js'] });
// Evaluation expression in the Electron context.
const appPath = await electronApp.evaluate(async ({ app }) => {
// This runs in the main Electron process, parameter here is always
// the result of the require('electron') in the main app script.
return app.getAppPath();
});
console.log(appPath);
// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
// Print the title.
console.log(await window.title());
// Capture a screenshot.
await window.screenshot({ path: 'intro.png' });
// Direct Electron console to Node terminal.
window.on('console', console.log);
// Click button.
await window.click('text=Click me');
// Exit app.
await electronApp.close();
})();
支援的 Electron 版本為
- v12.2.0+
- v13.4.0+
- v14+
已知問題
如果您無法啟動 Electron,並且在啟動期間最終逾時,請嘗試以下操作
- 確保
nodeCliInspect
(FuseV1Options.EnableNodeCliInspectArguments) fuse 未 設定為false
。
方法
launch
Added in: v1.9啟動使用 executablePath 指定的 electron 應用程式。
用法
await electron.launch();
await electron.launch(options);
引數
options
Object (optional)-
acceptDownloads
boolean (optional)Added in: v1.12#是否自動下載所有附件。預設為
true
,其中接受所有下載。 -
args
Array<string> (optional)#啟動時要傳遞給應用程式的其他引數。您通常在此處傳遞主腳本名稱。
-
bypassCSP
boolean (optional)Added in: v1.12#切換繞過頁面的 Content-Security-Policy。預設為
false
。 -
colorScheme
null | "light" | "dark" | "no-preference" (optional)Added in: v1.12#模擬 prefers-colors-scheme 媒體功能,支援的值為
'light'
和'dark'
。請參閱 page.emulateMedia() 以取得更多詳細資訊。傳遞null
會將模擬重設為系統預設值。預設為'light'
。 -
從中啟動應用程式的目前工作目錄。
-
env
Object<string, string> (optional)#指定 Electron 可見的環境變數。預設為
process.env
。 -
executablePath
string (optional)#啟動給定的 Electron 應用程式。如果未指定,則啟動此套件中安裝的預設 Electron 可執行檔,位於
node_modules/.bin/electron
。 -
extraHTTPHeaders
Object<string, string> (optional)Added in: v1.12#一個物件,其中包含要隨每個請求傳送的其他 HTTP 標頭。預設為無。
-
httpCredentials
Object (optional)Added in: v1.12#-
username
string -
password
string -
origin
string (optional)限制在特定來源上傳送 http 憑證 (scheme://host:port).
-
send
"unauthorized" | "always" (optional)此選項僅適用於從對應的 APIRequestContext 傳送的請求,並且不影響從瀏覽器傳送的請求。
'always'
- 具有基本驗證憑證的Authorization
標頭將隨每個 API 請求傳送。'unauthorized
- 憑證僅在收到具有WWW-Authenticate
標頭的 401 (未經授權) 回應時傳送。預設為'unauthorized'
。
HTTP 驗證的憑證。如果未指定來源,則使用者名稱和密碼會在未經授權的回應時傳送至任何伺服器。
-
-
ignoreHTTPSErrors
boolean (optional)Added in: v1.12#是否在傳送網路請求時忽略 HTTPS 錯誤。預設為
false
。 -
locale
string (optional)Added in: v1.12#指定使用者地區設定,例如
en-GB
、de-DE
等。地區設定將影響navigator.language
值、Accept-Language
請求標頭值以及數字和日期格式規則。預設為系統預設地區設定。在我們的 模擬指南 中深入瞭解模擬。 -
offline
boolean (optional)Added in: v1.12#是否模擬網路離線。預設為
false
。深入瞭解 網路模擬。 -
recordHar
Object (optional)Added in: v1.12#-
omitContent
boolean (optional)用於控制是否從 HAR 中省略請求內容的可選設定。預設為
false
。已淘汰,請改用content
政策。 -
content
"omit" | "embed" | "attach" (optional)用於控制資源內容管理的可選設定。如果指定
omit
,則不會持久儲存內容。如果指定attach
,則資源會持久儲存為 ZIP 封存中的個別檔案或項目。如果指定embed
,則內容會依照 HAR 規格內嵌儲存在 HAR 檔案中。對於.zip
輸出檔案,預設為attach
,對於所有其他檔案副檔名,預設為embed
。 -
path
string檔案系統上要將 HAR 檔案寫入的路徑。如果檔案名稱以
.zip
結尾,則預設會使用content: 'attach'
。 -
mode
"full" | "minimal" (optional)設定為
minimal
時,僅記錄從 HAR 路由所需的資訊。這會省略大小、時間、頁面、Cookie、安全性以及其他類型的 HAR 資訊,這些資訊在從 HAR 重播時未使用。預設為full
。 -
urlFilter
string | RegExp (optional)用於篩選儲存在 HAR 中的請求的 glob 或 regex 模式。當透過內容選項提供 baseURL,且傳遞的 URL 是路徑時,它會透過
new URL()
建構函式合併。預設為無。
啟用 HAR 記錄所有頁面到
recordHar.path
檔案中。如果未指定,則不會記錄 HAR。請務必等待 browserContext.close() 以儲存 HAR。 -
-
recordVideo
Object (optional)Added in: v1.12#-
dir
string要將影片放入的目錄路徑。
-
size
Object (optional)錄製影片的可選尺寸。如果未指定,則大小將等於縮小以適合 800x800 的
viewport
。如果未明確設定viewport
,則影片大小預設為 800x450。如有必要,將縮小每個頁面的實際圖片以適合指定的大小。
啟用將所有頁面的影片錄製到
recordVideo.dir
目錄中。如果未指定,則不會錄製影片。請務必等待 browserContext.close() 以儲存影片。 -
-
timeout
number (optional)Added in: v1.15#等待應用程式啟動的最長時間 (以毫秒為單位)。預設為
30000
(30 秒)。傳遞0
以停用逾時。 -
timezoneId
string (optional)Added in: v1.12#變更內容的時區。請參閱 ICU's metaZones.txt 以取得支援的時區 ID 清單。預設為系統時區。
-
tracesDir
string (optional)Added in: v1.36#如果指定,追蹤會儲存到此目錄中。
-
傳回