{"id":33567,"date":"2024-11-25T11:24:35","date_gmt":"2024-11-25T03:24:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33567\/"},"modified":"2024-11-25T11:24:35","modified_gmt":"2024-11-25T03:24:35","slug":"%e8%af%a6%e8%a7%a3%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%87%aa%e5%8a%a8%e5%8c%96%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33567\/","title":{"rendered":"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/5f3f8f9e4534a604.jpg\" class=\"aligncenter\" title=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe\" alt=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe\" \/><\/p>\n<p>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u3011<\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u8fd1\u671f\u56e2\u961f\u6253\u7b97\u505a\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u5de5\u5177\uff0c\u671f\u671b\u80fd\u591f\u505a\u5230\u4e1a\u52a1\u4eba\u5458\u64cd\u4f5c\u4e00\u904d\u5c0f\u7a0b\u5e8f\u540e\uff0c\u81ea\u52a8\u8fd8\u539f\u4e4b\u524d\u7684\u64cd\u4f5c\u8def\u5f84\uff0c\u5e76\u4e14\u6355\u83b7\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4ee5\u6b64\u6765\u5224\u65ad\u8fd9\u6b21\u53d1\u5e03\u662f\u5426\u4f1a\u5f71\u54cd\u5c0f\u7a0b\u5e8f\u7684\u57fa\u7840\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/94f061110a7a39066bc5a29c6a86c1d7-0.png\" style=\"max-width:90%\" title=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe1\" alt=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe1\" \/> <\/p>\n<p>\u4e0a\u8ff0\u63cf\u8ff0\u770b\u4f3c\u7b80\u5355\uff0c\u4f46\u662f\u4e2d\u95f4\u8fd8\u662f\u6709\u4e9b\u96be\u70b9\u7684\uff0c\u7b2c\u4e00\u4e2a\u96be\u70b9\u5c31\u662f\u5982\u4f55\u5728\u4e1a\u52a1\u4eba\u5458\u64cd\u4f5c\u5c0f\u7a0b\u5e8f\u7684\u65f6\u5019\u8bb0\u5f55\u64cd\u4f5c\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u96be\u70b9\u5c31\u662f\u5982\u4f55\u5c06\u8bb0\u5f55\u7684\u64cd\u4f5c\u8def\u5f84\u8fdb\u884c\u8fd8\u539f\u3002<\/p>\n<h2>\u81ea\u52a8\u5316 SDK<\/h2>\n<p>\u5982\u4f55\u5c06\u64cd\u4f5c\u8def\u5f84\u8fd8\u539f\u8fd9\u4e2a\u95ee\u9898\uff0c\u9996\u9009\u5b98\u65b9\u63d0\u4f9b\u7684 SDK\uff1a miniprogram-automator\u3002<\/p>\n<p>\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316 SDK \u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u5957\u901a\u8fc7\u5916\u90e8\u811a\u672c\u64cd\u63a7\u5c0f\u7a0b\u5e8f\u7684\u65b9\u6848\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u76ee\u7684\u3002\u901a\u8fc7\u8be5 SDK\uff0c\u4f60\u53ef\u4ee5\u505a\u5230\u4ee5\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u63a7\u5236\u5c0f\u7a0b\u5e8f\u8df3\u8f6c\u5230\u6307\u5b9a\u9875\u9762<\/li>\n<li>\u83b7\u53d6\u5c0f\u7a0b\u5e8f\u9875\u9762\u6570\u636e<\/li>\n<li>\u83b7\u53d6\u5c0f\u7a0b\u5e8f\u9875\u9762\u5143\u7d20\u72b6\u6001<\/li>\n<li>\u89e6\u53d1\u5c0f\u7a0b\u5e8f\u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6<\/li>\n<li>\u5f80 AppService \u6ce8\u5165\u4ee3\u7801\u7247\u6bb5<\/li>\n<li>\u8c03\u7528 wx \u5bf9\u8c61\u4e0a\u4efb\u610f\u63a5\u53e3<\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u7684\u63cf\u8ff0\u90fd\u6765\u81ea\u5b98\u65b9\u6587\u6863\uff0c\u5efa\u8bae\u9605\u8bfb\u540e\u9762\u5185\u5bb9\u4e4b\u524d\u53ef\u4ee5\u5148\u770b\u770b\u5b98\u65b9\u6587\u6863\uff0c\u5f53\u7136\u5982\u679c\u4e4b\u524d\u7528\u8fc7 puppeteer \uff0c\u4e5f\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\uff0capi \u57fa\u672c\u4e00\u81f4\u3002\u4e0b\u9762\u7b80\u5355\u4ecb\u7ecd\u4e0b SDK \u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<pre>\/\/ \u5f15\u5165sdkconst automator = require('miniprogram-automator')\/\/ \u542f\u52a8\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177automator.launch({  \/\/ \u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli \u5de5\u5177\n  \/\/ Windows\u4e0b\u4e3a\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli.bat\n  \/\/ MacOS\u4e0b\u4e3a\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli\n  cliPath: 'path\/to\/cli',  \/\/ \u9879\u76ee\u5730\u5740\uff0c\u5373\u8981\u8fd0\u884c\u7684\u5c0f\u7a0b\u5e8f\u7684\u8def\u5f84\n  projectPath: 'path\/to\/project',\n}).then(async miniProgram =&gt; { \/\/ miniProgram \u4e3a IDE \u542f\u52a8\u540e\u7684\u5b9e\u4f8b\n    \/\/ \u542f\u52a8\u5c0f\u7a0b\u5e8f\u91cc\u7684 index \u9875\u9762\n  const page = await miniProgram.reLaunch('\/page\/index\/index')  \/\/ \u7b49\u5f85 500 ms\n  await page.waitFor(500)  \/\/ \u83b7\u53d6\u9875\u9762\u5143\u7d20\n  const element = await page.$('.main-btn')  \/\/ \u70b9\u51fb\u5143\u7d20\n  await element.tap()    \/\/ \u5173\u95ed IDE\n  await miniProgram.close()\n})\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6709\u4e2a\u5730\u65b9\u9700\u8981\u63d0\u9192\u4e00\u4e0b\uff1a\u4f7f\u7528 SDK \u4e4b\u524d\u9700\u8981\u5f00\u542f\u5f00\u53d1\u8005\u5de5\u5177\u7684\u670d\u52a1\u7aef\u53e3\uff0c\u8981\u4e0d\u7136\u4f1a\u542f\u52a8\u5931\u8d25\u3002<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/94f061110a7a39066bc5a29c6a86c1d7-1.png\" style=\"max-width:90%\" title=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe2\" alt=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe2\" \/> <\/p>\n<h2>\u6355\u83b7\u7528\u6237\u884c\u4e3a<\/h2>\n<p>\u6709\u4e86\u8fd8\u539f\u64cd\u4f5c\u8def\u5f84\u7684\u529e\u6cd5\uff0c\u63a5\u4e0b\u6765\u5c31\u8981\u89e3\u51b3\u8bb0\u5f55\u64cd\u4f5c\u8def\u5f84\u7684\u96be\u9898\u4e86\u3002<\/p>\n<p>\u5728\u5c0f\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u4e0d\u80fd\u50cf web \u4e2d\u901a\u8fc7\u4e8b\u4ef6\u5192\u6ce1\u7684\u65b9\u5f0f\u5728 window \u4e2d\u6355\u83b7\u6240\u6709\u7684\u4e8b\u4ef6\uff0c\u597d\u5728\u5c0f\u7a0b\u5e8f\u6240\u4ee5\u7684\u9875\u9762\u548c\u7ec4\u4ef6\u90fd\u5fc5\u987b\u901a\u8fc7 Page \u3001Component \u65b9\u6cd5\u6765\u5305\u88c5\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u6539\u5199\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0c\u62e6\u622a\u4f20\u5165\u7684\u65b9\u6cd5\uff0c\u5e76\u5224\u65ad\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5426\u4e3a event \u5bf9\u8c61\uff0c\u4ee5\u6b64\u6765\u6355\u83b7\u6240\u6709\u7684\u4e8b\u4ef6\u3002<\/p>\n<pre>\/\/ \u6682\u5b58\u539f\u751f\u65b9\u6cd5const originPage = Pageconst originComponent = Component\/\/ \u6539\u5199 PagePage = (params) =&gt; {  const names = Object.keys(params)  for (const name of names) {    \/\/ \u8fdb\u884c\u65b9\u6cd5\u62e6\u622a\n    if (typeof obj[name] === 'function') {\n      params[name] = hookMethod(name, params[name], false)\n    }\n  }\n  originPage(params)\n}\/\/ \u6539\u5199 ComponentComponent = (params) =&gt; {  if (params.methods) {      const { methods } = params      const names = Object.keys(methods)      for (const name of names) {        \/\/ \u8fdb\u884c\u65b9\u6cd5\u62e6\u622a\n        if (typeof methods[name] === 'function') {\n          methods[name] = hookMethod(name, methods[name], true)\n        }\n      }\n  }\n  originComponent(params)\n}const hookMethod = (name, method, isComponent) =&gt; {  return function(...args) {    const [evt] = args \/\/ \u53d6\u51fa\u7b2c\u4e00\u4e2a\u53c2\u6570\n    \/\/ \u5224\u65ad\u662f\u5426\u4e3a event \u5bf9\u8c61\n    if (evt &amp;&amp; evt.target &amp;&amp; evt.type) {      \/\/ \u8bb0\u5f55\u7528\u6237\u884c\u4e3a\n    }    return method.apply(this, args)\n  }\n}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u7684\u4ee3\u7801\u53ea\u662f\u4ee3\u7406\u4e86\u6240\u6709\u7684\u4e8b\u4ef6\u65b9\u6cd5\uff0c\u5e76\u4e0d\u80fd\u7528\u6765\u8fd8\u539f\u7528\u6237\u7684\u884c\u4e3a\uff0c\u8981\u8fd8\u539f\u7528\u6237\u884c\u4e3a\u8fd8\u5fc5\u987b\u77e5\u9053\u8be5\u4e8b\u4ef6\u7c7b\u578b\u662f\u5426\u662f\u9700\u8981\u7684\uff0c\u6bd4\u5982\u70b9\u51fb\u3001\u957f\u6309\u3001\u8f93\u5165\u3002<\/p>\n<pre>const evtTypes = [    'tap', \/\/ \u70b9\u51fb\n    'input', \/\/ \u8f93\u5165\n    'confirm', \/\/ \u56de\u8f66\n    'longpress' \/\/ \u957f\u6309]const hookMethod = (name, method) =&gt; {  return function(...args) {    const [evt] = args \/\/ \u53d6\u51fa\u7b2c\u4e00\u4e2a\u53c2\u6570\n    \/\/ \u5224\u65ad\u662f\u5426\u4e3a event \u5bf9\u8c61\n    if (\n      evt &amp;&amp; evt.target &amp;&amp; evt.type &amp;&amp;\n      evtTypes.includes(evt.type) \/\/ \u5224\u65ad\u4e8b\u4ef6\u7c7b\u578b\n    ) {      \/\/ \u8bb0\u5f55\u7528\u6237\u884c\u4e3a\n    }    return method.apply(this, args)\n  }\n}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u786e\u5b9a\u4e8b\u4ef6\u7c7b\u578b\u4e4b\u540e\uff0c\u8fd8\u9700\u8981\u660e\u786e\u70b9\u51fb\u7684\u5143\u7d20\u5230\u5e95\u662f\u54ea\u4e2a\uff0c\u4f46\u662f\u5c0f\u7a0b\u5e8f\u91cc\u9762\u6bd4\u8f83\u5751\u7684\u5730\u65b9\u5c31\u662f\uff0cevent \u5bf9\u8c61\u7684 target \u5c5e\u6027\u4e2d\uff0c\u5e76\u6ca1\u6709\u5143\u7d20\u7684\u7c7b\u540d\uff0c\u4f46\u662f\u53ef\u4ee5\u83b7\u53d6\u5143\u7d20\u7684 dataset\u3002<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/cc4dc8131e2222a23aad398460c68743-2.png\" style=\"max-width:90%\" title=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe3\" alt=\"\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u63d2\u56fe3\" \/> <\/p>\n<p>\u4e3a\u4e86\u51c6\u786e\u7684\u83b7\u53d6\u5143\u7d20\uff0c\u6211\u4eec\u9700\u8981\u5728\u6784\u5efa\u4e2d\u589e\u52a0\u4e00\u4e2a\u6b65\u9aa4\uff0c\u4fee\u6539 wxml \u6587\u4ef6\uff0c\u5c06\u6240\u6709\u5143\u7d20\u7684 class \u5c5e\u6027\u590d\u5236\u4e00\u4efd\u5230 data-className \u4e2d\u3002<\/p>\n<pre>&lt;!-- \u6784\u5efa\u524d --&gt;&lt;view class=\"close-btn\"&gt;&lt;\/view&gt;&lt;view class=\"{{mainClassName}}\"&gt;&lt;\/view&gt;&lt;!-- \u6784\u5efa\u540e --&gt;&lt;view class=\"close-btn\" data-className=\"close-btn\"&gt;&lt;\/view&gt;&lt;view class=\"{{mainClassName}}\" data-className=\"{{mainClassName}}\"&gt;&lt;\/view&gt;\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f46\u662f\u83b7\u53d6\u5230 class \u4e4b\u540e\uff0c\u53c8\u4f1a\u6709\u53e6\u4e00\u4e2a\u5751\uff0c\u5c0f\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\u5e76\u4e0d\u80fd\u76f4\u63a5\u83b7\u53d6\u9875\u9762\u91cc\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u4e2d\u7684\u5143\u7d20\uff0c\u5fc5\u987b\u5148\u83b7\u53d6\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u3002<\/p>\n<pre>&lt;!-- Page --&gt;&lt;toast text=\"loading\" show=\"{{showToast}}\" \/&gt;&lt;!-- Component --&gt;&lt;view class=\"toast\" wx:if=\"{{show}}\"&gt;\n  &lt;text class=\"toast-text\"&gt;{{text}}&lt;\/text&gt;\n  &lt;view class=\"toast-close\" \/&gt;&lt;\/view&gt;\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/ \u5982\u679c\u76f4\u63a5\u67e5\u627e .toast-close \u4f1a\u5f97\u5230 nullconst element = await page.$('.toast-close')\nelement.tap() \/\/ Error!\/\/ \u5fc5\u987b\u5148\u901a\u8fc7\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684 tagName \u627e\u5230\u81ea\u5b9a\u4e49\u7ec4\u4ef6\/\/ \u518d\u4ece\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u4e2d\u901a\u8fc7 className \u67e5\u627e\u5bf9\u5e94\u5143\u7d20const element = await page.$('toast .toast-close')\nelement.tap()\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5728\u6784\u5efa\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u8fd8\u9700\u8981\u4e3a\u5143\u7d20\u63d2\u5165 tagName\u3002<\/p>\n<pre>&lt;!-- \u6784\u5efa\u524d --&gt;&lt;view class=\"close-btn\" \/&gt;&lt;toast text=\"loading\" show=\"{{showToast}}\" \/&gt;&lt;!-- \u6784\u5efa\u540e --&gt;&lt;view class=\"close-btn\" data-className=\"close-btn\" data-tagName=\"view\" \/&gt;&lt;toast text=\"loading\" show=\"{{showToast}}\" data-tagName=\"toast\" \/&gt;\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6109\u5feb\u7684\u8bb0\u5f55\u7528\u6237\u884c\u4e3a\u4e86\u3002<\/p>\n<pre>\/\/ \u8bb0\u5f55\u7528\u6237\u884c\u4e3a\u7684\u6570\u7ec4const actions = [];\/\/ \u6dfb\u52a0\u7528\u6237\u884c\u4e3aconst addAction = (type, query, value = '') =&gt; {\n  actions.push({    time: Date.now(),\n    type,\n    query,\n    value\n  })\n}\/\/ \u4ee3\u7406\u4e8b\u4ef6\u65b9\u6cd5const hookMethod = (name, method, isComponent) =&gt; {  return function(...args) {    const [evt] = args \/\/ \u53d6\u51fa\u7b2c\u4e00\u4e2a\u53c2\u6570\n    \/\/ \u5224\u65ad\u662f\u5426\u4e3a event \u5bf9\u8c61\n    if (\n      evt &amp;&amp; evt.target &amp;&amp; evt.type &amp;&amp;\n      evtTypes.includes(evt.type) \/\/ \u5224\u65ad\u4e8b\u4ef6\u7c7b\u578b\n    ) {      const { type, target, detail } = evt      const { id, dataset = {} } = target        const { className = '' } = dataset        const { value = '' } = detail \/\/ input\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff0c\u8f93\u5165\u6846\u7684\u503c\n      \/\/ \u8bb0\u5f55\u7528\u6237\u884c\u4e3a\n      let query = ''\n      if (isComponent) {        \/\/ \u5982\u679c\u662f\u7ec4\u4ef6\u5185\u7684\u65b9\u6cd5\uff0c\u9700\u8981\u83b7\u53d6\u5f53\u524d\u7ec4\u4ef6\u7684 tagName\n        query = `${this.dataset.tagName} `\n      }      if (id) {        \/\/ id \u5b58\u5728\uff0c\u5219\u76f4\u63a5\u901a\u8fc7 id \u67e5\u627e\u5143\u7d20\n        query += id\n      } else {        \/\/ id \u4e0d\u5b58\u5728\uff0c\u624d\u901a\u8fc7 className \u67e5\u627e\u5143\u7d20\n        query += className\n      }\n      addAction(type, query, value)\n    }    return method.apply(this, args)\n  }\n}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5230\u8fd9\u91cc\u5df2\u7ecf\u8bb0\u5f55\u4e86\u7528\u6237\u6240\u6709\u7684\u70b9\u51fb\u3001\u8f93\u5165\u3001\u56de\u8f66\u76f8\u5173\u7684\u64cd\u4f5c\u3002\u4f46\u662f\u8fd8\u6709\u6eda\u52a8\u5c4f\u5e55\u7684\u64cd\u4f5c\u6ca1\u6709\u8bb0\u5f55\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4ee3\u7406 Page \u7684 onPageScroll \u65b9\u6cd5\u3002<\/p>\n<pre>\/\/ \u8bb0\u5f55\u7528\u6237\u884c\u4e3a\u7684\u6570\u7ec4const actions = [];\/\/ \u6dfb\u52a0\u7528\u6237\u884c\u4e3aconst addAction = (type, query, value = '') =&gt; {  if (type === 'scroll' || type === 'input') {    \/\/ \u5982\u679c\u4e0a\u4e00\u6b21\u884c\u4e3a\u4e5f\u662f\u6eda\u52a8\u6216\u8f93\u5165\uff0c\u5219\u91cd\u7f6e value \u5373\u53ef\n    const last = this.actions[this.actions.length - 1]    if (last &amp;&amp; last.type === type) {\n      last.value = value\n      last.time = Date.now()      return\n    }\n  }\n  actions.push({    time: Date.now(),\n    type,\n    query,\n    value\n  })\n}\n\nPage = (params) =&gt; {  const names = Object.keys(params)  for (const name of names) {    \/\/ \u8fdb\u884c\u65b9\u6cd5\u62e6\u622a\n    if (typeof obj[name] === 'function') {\n      params[name] = hookMethod(name, params[name], false)\n    }\n  }  const { onPageScroll } = params  \/\/ \u62e6\u622a\u6eda\u52a8\u4e8b\u4ef6\n  params.onPageScroll = function (...args) {    const [evt] = args    const { scrollTop } = evt\n    addAction('scroll', '', scrollTop)\n    onPageScroll.apply(this, args)\n  }\n  originPage(params)\n}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u4f18\u5316\u70b9\uff0c\u5c31\u662f\u6eda\u52a8\u64cd\u4f5c\u8bb0\u5f55\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5224\u65ad\u4e00\u4e0b\u4e0a\u6b21\u64cd\u4f5c\u662f\u5426\u4e5f\u4e3a\u6eda\u52a8\u64cd\u4f5c\uff0c\u5982\u679c\u662f\u540c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5219\u53ea\u9700\u8981\u4fee\u6539\u4e00\u4e0b\u6eda\u52a8\u8ddd\u79bb\u5373\u53ef\uff0c\u56e0\u4e3a\u4e24\u6b21\u6eda\u52a8\u53ef\u4ee5\u4e00\u6b65\u5230\u4f4d\u3002\u540c\u7406\uff0c\u8f93\u5165\u4e8b\u4ef6\u4e5f\u662f\uff0c\u8f93\u5165\u7684\u503c\u4e5f\u53ef\u4ee5\u4e00\u6b65\u5230\u4f4d\u3002<\/p>\n<h2>\u8fd8\u539f\u7528\u6237\u884c\u4e3a<\/h2>\n<p>\u7528\u6237\u64cd\u4f5c\u5b8c\u6bd5\u540e\uff0c\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u7528\u6237\u884c\u4e3a\u7684 json \u6587\u672c\uff0c\u628a json \u6587\u672c\u590d\u5236\u51fa\u6765\u540e\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u81ea\u52a8\u5316\u5de5\u5177\u8fd0\u884c\u4e86\u3002<\/p>\n<pre>\/\/ \u5f15\u5165sdkconst automator = require('miniprogram-automator')\/\/ \u7528\u6237\u64cd\u4f5c\u884c\u4e3aconst actions = [\n  { type: 'tap', query: 'goods .title', value: '', time: 1596965650000 },\n  { type: 'scroll', query: '', value: 560, time: 1596965710680 },\n  { type: 'tap', query: 'gotoTop', value: '', time: 1596965770000 }\n]\/\/ \u542f\u52a8\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177automator.launch({  projectPath: 'path\/to\/project',\n}).then(async miniProgram =&gt; {  let page = await miniProgram.reLaunch('\/page\/index\/index')  \n  let prevTime  for (const action of actions) {    const { type, query, value, time } = action    if (prevTime) {      \/\/ \u8ba1\u7b97\u4e24\u6b21\u64cd\u4f5c\u4e4b\u95f4\u7684\u7b49\u5f85\u65f6\u95f4\n        await page.waitFor(time - prevTime)\n    }    \/\/ \u91cd\u7f6e\u4e0a\u6b21\u64cd\u4f5c\u65f6\u95f4\n    prevTime = time    \n    \/\/ \u83b7\u53d6\u5f53\u524d\u9875\u9762\u5b9e\u4f8b\n    page = await miniProgram.currentPage()    switch (type) {      case 'tap':            const element = await page.$(query)        await element.tap()        break;      case 'input':            const element = await page.$(query)        await element.input(value)        break;      case 'confirm':            const element = await page.$(query)                await element.trigger('confirm', { value });        break;      case 'scroll':        await miniProgram.pageScrollTo(value)        break;\n    }    \/\/ \u6bcf\u6b21\u64cd\u4f5c\u7ed3\u675f\u540e\uff0c\u7b49\u5f85 5s\uff0c\u9632\u6b62\u9875\u9762\u8df3\u8f6c\u8fc7\u7a0b\u4e2d\uff0c\u540e\u9762\u7684\u64cd\u4f5c\u627e\u4e0d\u5230\u9875\u9762\n    await page.waitFor(5000)\n  }    \/\/ \u5173\u95ed IDE\n  await miniProgram.close()\n})\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u7684\u8fd8\u539f\u4e86\u7528\u6237\u7684\u64cd\u4f5c\u884c\u4e3a\uff0c\u5b9e\u9645\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u4f1a\u6d89\u53ca\u5230\u7f51\u7edc\u8bf7\u6c42\u548c localstorage \u7684 mock\uff0c\u8fd9\u91cc\u4e0d\u518d\u5c55\u5f00\u8bb2\u8ff0\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u63a5\u5165 jest \u5de5\u5177\uff0c\u66f4\u52a0\u65b9\u4fbf\u7528\u4f8b\u7684\u7f16\u5199\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u770b\u4f3c\u5f88\u96be\u7684\u9700\u6c42\uff0c\u53ea\u8981\u7528\u5fc3\u53bb\u53d1\u6398\uff0c\u603b\u80fd\u627e\u5230\u5bf9\u5e94\u7684\u89e3\u51b3\u529e\u6cd5\u3002\u53e6\u5916\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u5de5\u5177\u771f\u7684\u6709\u5f88\u591a\u5751\uff0c\u9047\u5230\u95ee\u9898\u53ef\u4ee5\u5148\u5230\u5c0f\u7a0b\u5e8f\u793e\u533a\u53bb\u627e\u627e\uff0c\u5927\u90e8\u5206\u5751\u90fd\u6709\u524d\u4eba\u8e29\u8fc7\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e00\u65f6\u65e0\u6cd5\u89e3\u51b3\u7684\u95ee\u9898\u53ea\u80fd\u60f3\u5176\u4ed6\u529e\u6cd5\u6765\u89c4\u907f\u3002\u6700\u540e\u795d\u613f\u5929\u4e0b\u65e0 bug\u3002<\/p>\n<blockquote><p>\n  \u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u5fae\u4fe1\u516c\u4f17\u53f7\u5f00\u53d1\u6559\u7a0b\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8be6\u89e3\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u3011 \u80cc\u666f \u8fd1\u671f\u56e2\u961f\u6253\u7b97\u505a\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u5de5\u5177\uff0c\u671f\u671b\u80fd\u591f\u505a\u5230\u4e1a\u52a1\u4eba\u5458\u64cd\u4f5c\u4e00\u904d\u5c0f\u7a0b\u5e8f\u540e\uff0c\u81ea\u52a8\u8fd8\u539f\u4e4b\u524d\u7684\u64cd\u4f5c\u8def\u5f84\uff0c\u5e76\u4e14\u6355\u83b7\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4ee5\u6b64\u6765\u5224\u65ad\u8fd9\u6b21\u53d1\u5e03\u662f\u5426\u4f1a\u5f71\u54cd\u5c0f\u7a0b\u5e8f\u7684\u57fa\u7840\u529f\u80fd\u3002 \u4e0a\u8ff0\u63cf\u8ff0\u770b\u4f3c\u7b80\u5355\uff0c\u4f46\u662f\u4e2d\u95f4\u8fd8\u662f\u6709\u4e9b\u96be\u70b9\u7684\uff0c\u7b2c\u4e00\u4e2a\u96be\u70b9\u5c31\u662f\u5982\u4f55\u5728\u4e1a\u52a1\u4eba\u5458\u64cd\u4f5c\u5c0f\u7a0b\u5e8f\u7684\u65f6\u5019\u8bb0\u5f55\u64cd\u4f5c\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u96be\u70b9\u5c31\u662f\u5982\u4f55\u5c06\u8bb0\u5f55\u7684\u64cd\u4f5c\u8def\u5f84\u8fdb\u884c\u8fd8\u539f\u3002 \u81ea\u52a8\u5316 SDK \u5982\u4f55\u5c06\u64cd\u4f5c\u8def\u5f84\u8fd8\u539f\u8fd9\u4e2a\u95ee\u9898\uff0c\u9996\u9009\u5b98\u65b9\u63d0\u4f9b\u7684 SDK\uff1a miniprogram-automator\u3002 \u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316 SDK \u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u5957\u901a\u8fc7\u5916\u90e8\u811a\u672c\u64cd\u63a7\u5c0f\u7a0b\u5e8f\u7684\u65b9\u6848\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u7a0b\u5e8f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u76ee\u7684\u3002\u901a\u8fc7\u8be5 SDK\uff0c\u4f60\u53ef\u4ee5\u505a\u5230\u4ee5\u4e0b\u4e8b\u60c5\uff1a \u63a7\u5236\u5c0f\u7a0b\u5e8f\u8df3\u8f6c\u5230\u6307\u5b9a\u9875\u9762 \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u9875\u9762\u6570\u636e \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u9875\u9762\u5143\u7d20\u72b6\u6001 \u89e6\u53d1\u5c0f\u7a0b\u5e8f\u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6 \u5f80 AppService \u6ce8\u5165\u4ee3\u7801\u7247\u6bb5 \u8c03\u7528 wx \u5bf9\u8c61\u4e0a\u4efb\u610f\u63a5\u53e3 &#8230; \u4e0a\u9762\u7684\u63cf\u8ff0\u90fd\u6765\u81ea\u5b98\u65b9\u6587\u6863\uff0c\u5efa\u8bae\u9605\u8bfb\u540e\u9762\u5185\u5bb9\u4e4b\u524d\u53ef\u4ee5\u5148\u770b\u770b\u5b98\u65b9\u6587\u6863\uff0c\u5f53\u7136\u5982\u679c\u4e4b\u524d\u7528\u8fc7 puppeteer \uff0c\u4e5f\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\uff0capi \u57fa\u672c\u4e00\u81f4\u3002\u4e0b\u9762\u7b80\u5355\u4ecb\u7ecd\u4e0b SDK \u7684\u4f7f\u7528\u65b9\u5f0f\u3002 \/\/ \u5f15\u5165sdkconst automator = require(&#8216;miniprogram-automator&#8217;)\/\/ \u542f\u52a8\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177automator.launch({ \/\/ \u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli \u5de5\u5177 \/\/ Windows\u4e0b\u4e3a\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli.bat \/\/ MacOS\u4e0b\u4e3a\u5b89\u88c5\u8def\u5f84\u4e0b\u7684 cli cliPath: &#8216;path\/to\/cli&#8217;, \/\/ \u9879\u76ee\u5730\u5740\uff0c\u5373\u8981\u8fd0\u884c\u7684\u5c0f\u7a0b\u5e8f\u7684\u8def\u5f84 projectPath: &#8216;path\/to\/project&#8217;, }).then(async miniProgram =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33567","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33567"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33567\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}