{"id":10443,"date":"2023-11-28T15:44:27","date_gmt":"2023-11-28T07:44:27","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=10443"},"modified":"2023-11-28T15:44:27","modified_gmt":"2023-11-28T07:44:27","slug":"playwright%e6%89%93%e5%8c%85exe%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/11\/28\/playwright%e6%89%93%e5%8c%85exe%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"playwright\u6253\u5305exe\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"

Playwright \u662f\u4e00\u6b3e\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u722c\u866b\u7684 Node.js \u5e93\u3002\u5b83\u5185\u7f6e\u4e86 Chromium\u3001Firefox \u548c WebKit \u4e09\u79cd\u6d4f\u89c8\u5668\uff0c\u652f\u6301\u8de8\u5e73\u53f0\uff0c\u53ef\u7528\u4e8e\u6d4b\u8bd5\u548c\u722c\u866b\u7b49\u591a\u4e2a\u9886\u57df\u3002Playwright \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u81ea\u52a8\u622a\u56fe\u3001\u81ea\u52a8\u586b\u5145\u8868\u5355\u3001\u6a21\u62df\u952e\u76d8\u8f93\u5165\u7b49\u3002<\/p>\n

\u5728\u4f7f\u7528 Playwright \u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u6216\u722c\u866b\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5c06\u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u5176\u4ed6\u673a\u5668\u4e0a\u8fd0\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Electron \u548c pkg \u5de5\u5177\u5c06 Playwright \u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n

## Electron \u7b80\u4ecb<\/p>\n

Electron \u662f\u4e00\u6b3e\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\uff0c\u57fa\u4e8e Chromium \u548c Node.js\u3002\u5b83\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u4f7f\u7528 Web \u6280\u672f\uff08HTML\u3001CSS \u548c JavaScript\uff09\u5f00\u53d1\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002Electron \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Node.js \u5e94\u7528\u7a0b\u5e8f\uff0c\u5b83\u8fd0\u884c\u5728\u4e00\u4e2a\u9690\u85cf\u7684 Chromium \u5b9e\u4f8b\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e95\u5c42 API\u3002<\/p>\n

Electron \u7684\u4e3b\u8981\u4f18\u70b9\u662f\u8de8\u5e73\u53f0\uff0c\u5f00\u53d1\u8005\u53ea\u9700\u8981\u7f16\u5199\u4e00\u5957\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u540c\u65f6\u5728 Windows\u3001Mac \u548c Linux \u4e0a\u8fd0\u884c\u3002\u6b64\u5916\uff0cElectron \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u81ea\u52a8\u66f4\u65b0\u3001\u83dc\u5355\u680f\u3001\u5bf9\u8bdd\u6846\u7b49\u3002<\/p>\n

## pkg \u7b80\u4ecb<\/p>\n

pkg \u662f\u4e00\u6b3e\u7528\u4e8e\u5c06 Node.js \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301\u5c06\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210 Windows\u3001Mac \u548c Linux \u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u65e0\u9700\u5b89\u88c5 Node.js \u6216\u5176\u4ed6\u4f9d\u8d56\u9879\u3002pkg \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u4ee3\u7801\u6df7\u6dc6\u3001\u52a0\u5bc6\u7b49\u3002<\/p>\n

## \u6253\u5305\u6b65\u9aa4<\/p>\n

\u4e0b\u9762\u4ecb\u7ecd\u5c06 Playwright \u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6b65\u9aa4\uff1a<\/p>\n

### 1. \u5b89\u88c5 Electron \u548c pkg<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5 Electron \u548c pkg\u3002\u53ef\u4ee5\u901a\u8fc7 npm \u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n

“`<\/p>\n

npm install electron pkg –save-dev<\/p>\n

“`<\/p>\n

### 2. \u521b\u5efa\u4e3b\u8fdb\u7a0b\u6587\u4ef6<\/p>\n

\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a main.js \u7684\u6587\u4ef6\uff0c\u4f5c\u4e3a Electron \u7684\u4e3b\u8fdb\u7a0b\u6587\u4ef6\u3002\u4e3b\u8fdb\u7a0b\u6587\u4ef6\u8d1f\u8d23\u521b\u5efa\u7a97\u53e3\u3001\u52a0\u8f7d\u9875\u9762\u3001\u5904\u7406\u4e8b\u4ef6\u7b49\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 main.js \u6587\u4ef6\u793a\u4f8b\uff1a<\/p>\n

“`<\/p>\n

const { app, BrowserWindow } = require(‘electron’)<\/p>\n

function createWindow() {<\/p>\n

const win = new BrowserWindow({<\/p>\n

width: 800,<\/p>\n

height: 600,<\/p>\n

webPreferences: {<\/p>\n

nodeIntegration: true<\/p>\n

}<\/p>\n

})<\/p>\n

win.loadFile(‘index.html’)<\/p>\n

}<\/p>\n

app.whenReady().then(() => {<\/p>\n

createWindow()<\/p>\n

app.on(‘activate’, () => {<\/p>\n

if (BrowserWindow.getAllWindows().length === 0) {<\/p>\n

createWindow()<\/p>\n

}<\/p>\n

})<\/p>\n

})<\/p>\n

app.on(‘window-all-closed’, () => {<\/p>\n

if (process.platform !== ‘darwin’) {<\/p>\n

app.quit()<\/p>\n

}<\/p>\n

})jieba\u6253\u5305exe<\/a><\/p>\n

“`<\/p>\n

\u8be5\u6587\u4ef6\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u52a0\u8f7d\u4e86\u4e00\u4e2a\u540d\u4e3a index.html \u7684\u9875\u9762\u3002<\/p>\n

### 3. \u521b\u5efa index.html \u6587\u4ef6<\/p>\n

\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a index.html \u7684\u6587\u4ef6\uff0c\u4f5c\u4e3a Electron \u7684\u6e32\u67d3\u8fdb\u7a0b\u6587\u4ef6\u3002\u6e32\u67d3\u8fdb\u7a0b\u6587\u4ef6\u8d1f\u8d23\u663e\u793a\u9875\u9762\u5185\u5bb9\uff0c\u5904\u7406\u7528\u6237\u4ea4\u4e92\u7b49\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 index.html \u6587\u4ef6\u793a\u4f8b\uff1a<\/p>\n

“`<\/p>\n<\/p>\n

\n

\n

Hello World!<\/title><\/p>\n<\/p>\n<p>\n<p> Hello World!<\/p>\n<\/p>\n<p> const playwright = require(‘playwright’);<\/p>\n<p> (async () => {<\/p>\n<p> const browser = await playwright.chromium.launch();<\/p>\n<p> const context = await browser.newContext();<\/p>\n<p> const page = await context.newPage();<\/p>\n<p> await page.goto(‘https:\/\/www.baidu.com’);<\/p>\n<p> await page.screenshot({ path: ‘example.png’ });<\/p>\n<p> await browser.close();<\/p>\n<p> })();<\/p>\n<\/p>\n<\/p>\n<p>“`<\/p>\n<p>\u8be5\u6587\u4ef6\u4f7f\u7528 Playwright \u6253\u5f00\u4e86\u767e\u5ea6\u9996\u9875\uff0c\u5e76\u622a\u53d6\u4e86\u4e00\u5f20\u9875\u9762\u622a\u56fe\u3002<\/p>\n<p>### 4. \u6253\u5305\u5e94\u7528\u7a0b\u5e8f<\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<p>“`<\/p>\n<p>pkg . –target=host <a href=\"https:\/\/exe.yimenapp.com\/tool-304.html\">asp\u751f\u6210exe<\/a>–output=myapp<\/p>\n<p>“`<\/p>\n<p>\u8be5\u547d\u4ee4\u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8f93\u51fa\u5230 myapp \u76ee\u5f55\u4e0b\u3002<\/p>\n<p>### 5. \u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fdb\u5165 mya<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/20\/10\/35505183841.jpg\" \/><\/figure>\n<\/p>\n<p>pp \u76ee<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Playwright \u662f\u4e00\u6b3e\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u722c\u866b\u7684 Node.js \u5e93\u3002\u5b83\u5185\u7f6e\u4e86 Chromium\u3001Firefox \u548c WebKit \u4e09\u79cd\u6d4f\u89c8\u5668\uff0c\u652f\u6301\u8de8\u5e73\u53f0\uff0c\u53ef\u7528\u4e8e\u6d4b\u8bd5\u548c\u722c\u866b\u7b49\u591a\u4e2a\u9886\u57df\u3002Playwr<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[16412,24,116,12,1772],"topic":[],"class_list":["post-10443","post","type-post","status-publish","format-standard","hentry","category-zhuomianruanjian","tag-macos","tag-24","tag-116","tag-12","tag-exe"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/10443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=10443"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/10443\/revisions"}],"predecessor-version":[{"id":10478,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/10443\/revisions\/10478"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=10443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=10443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=10443"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=10443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}