{"id":5038,"date":"2024-01-13T19:29:30","date_gmt":"2024-01-13T11:29:30","guid":{"rendered":"https:\/\/www.kenpai.com\/cms\/5038\/"},"modified":"2024-01-13T19:29:30","modified_gmt":"2024-01-13T11:29:30","slug":"appium-server-%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90%e4%b9%8b%e5%90%af%e5%8a%a8%e8%bf%90%e8%a1%8cexpress-http%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.kenpai.com\/cms\/5038\/","title":{"rendered":"Appium Server \u6e90\u7801\u5206\u6790\u4e4b\u542f\u52a8\u8fd0\u884cExpress http\u670d\u52a1\u5668"},"content":{"rendered":"<\/p>\n<p>\u901a\u8fc7\u4e0a\u4e00\u4e2a\u7cfb\u5217Appium Android Bootstrap\u6e90\u7801\u5206\u6790\u6211\u4eec\u4e86\u89e3\u5230\u4e86appium\u5728\u5b89\u5353\u76ee\u6807\u673a\u5668\u4e0a\u662f\u5982\u4f55\u901a\u8fc7bootstrap\u8fd9\u4e2a\u670d\u52a1\u6765\u63a5\u6536appium\u4ecepc\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u547d\u4ee4\uff0c\u5e76\u6700\u7ec8\u4f7f\u7528uiautomator\u6846\u67b6\u8fdb\u884c\u5904\u7406\u7684\u3002\u5927\u5bb6\u8fd8\u6ca1\u6709\u8fd9\u65b9\u9762\u7684\u80cc\u666f\u77e5\u8bc6\u7684\u8bdd\u5efa\u8bae\u5148\u53bb\u770b\u4e00\u4e0b\uff0c\u4ee5\u4e0b\u5217\u51fa\u6765\u65b9\u4fbf\u5927\u5bb6\u53c2\u8003\uff1a<\/p>\n<p style=\"text-align:\">\n<p>\u300aAppium Android Bootstrap\u6e90\u7801\u5206\u6790\u4e4b\u7b80\u4ecb\u300b\u300aAppium Android Bootstrap\u6e90\u7801\u5206\u6790\u4e4b\u63a7\u4ef6AndroidElement\u300b\u300aAppium Android Bootstrap\u6e90\u7801\u5206\u6790\u4e4b\u547d\u4ee4\u89e3\u6790\u6267\u884c\u300b\u300aAppium Android Bootstrap\u6e90\u7801\u5206\u6790\u4e4b\u542f\u52a8\u8fd0\u884c\u300b<\/p>\n<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u77e5\u9053\u4e86\u76ee\u6807\u673a\u5668\u7aef\u7684\u5904\u7406\u540e\uff0c\u6211\u4eec\u7406\u6240\u5f53\u7136\u9700\u8981\u641e\u6e05\u695abootstrap\u5ba2\u6237\u7aef\uff0c\u4e5f\u5c31\u662fAppium Server\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u8fd9\u4e2a\u5c31\u662f\u8fd9\u4e2a\u7cfb\u5217\u6587\u7ae0\u7684\u521d\u8877\u3002<\/p>\n<\/p>\n<p>Appium Server\u5176\u5b9e\u62e5\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p>\u5b83\u662f\u4e2ahttp\u670d\u52a1\u5668\uff0c\u5b83\u4e13\u95e8\u63a5\u6536\u4ece\u5ba2\u6237\u7aef\u901a\u8fc7\u57fa\u4e8ehttp\u7684REST\u534f\u8bae\u53d1\u9001\u8fc7\u6765\u7684\u547d\u4ee4\u4ed6\u662fbootstrap\u5ba2\u6237\u7aef\uff1a\u5b83\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u7684\u547d\u4ee4\u540e\uff0c\u9700\u8981\u60f3\u529e\u6cd5\u628a\u8fd9\u4e9b\u547d\u4ee4\u53d1\u9001\u7ed9\u76ee\u6807\u5b89\u5353\u673a\u5668\u7684bootstrap\u6765\u9a71\u52a8uiatuomator\u6765\u505a\u4e8b\u60c5<\/p>\n<\/p>\n<p>\u6211\u4eec\u4eca\u5929\u63cf\u8ff0\u7684\u5c31\u662f\u7b2c\u4e00\u70b9\u3002\u5927\u5bb6\u5148\u770b\u4e0b\u6211\u4ee5\u524d\u753b\u7684\u4e00\u4e2aappium\u67b6\u6784\u56fe\u597d\u6709\u4e2a\u57fa\u672c\u6982\u5ff5\uff1aAppium Server\u5927\u6982\u662f\u5728\u54ea\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u5de5\u4f5c\u7684<\/p>\n<\/p>\n<p>\u540c\u65f6\u6211\u4eec\u4e5f\u5148\u770b\u4e0bAppium Server\u7684\u6e90\u7801\u5e03\u5c40,\u540e\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u4ee3\u7801\u7ed3\u6784\u6982\u5ff5\uff1a<\/p>\n<\/p>\n<p>\u5f00\u59cb\u4e4b\u524d\u5148\u58f0\u660e\u4e00\u4e0b\uff0c\u56e0\u4e3aappium server\u662f\u57fa\u4e8e\u5f53\u4eca\u70ed\u672c\u7684nodejs\u7f16\u5199\u7684\uff0c\u800c\u6211\u672c\u4eba\u5e76\u4e0d\u662f\u5199javascript\u51fa\u8eab\u7684\uff0c\u53ea\u662f\u5728\u5199\u8fd9\u7bc7\u6587\u7ae0\u7684\u65f6\u5019\u82b1\u4e86\u51e0\u4e2a\u5c0f\u65f6\u53bb\u4e86\u89e3\u4e86\u4e0bjavascript\u7684\u8bed\u6cd5\uff0c\u4f46\u662f\u6211\u76f8\u4fe1\u8bed\u8a00\u662f\u76f8\u540c\u7684\uff0c\u53bb\u770b\u61c2\u8fd9\u4e9b\u4ee3\u7801\u8fd8\u662f\u6ca1\u6709\u592a\u5927\u95ee\u9898\u7684\u3002\u4f46\uff0c\u4e07\u4e00\u5f53\u4e2d\u771f\u6709\u8bef\u5bfc\u5927\u5bb6\u7684\u5730\u65b9\uff0c\u8fd8\u656c\u8bf7\u5927\u5bb6\u6307\u51fa\u6765\uff0c\u4ee5\u514d\u7978\u5bb3\u8bfb\u8005&#8230;<\/p>\n<\/p>\n<p>1.\u8fd0\u884c\u53c2\u6570\u51c6\u5907<\/p>\n<\/p>\n<p>Appium \u670d\u52a1\u5668\u542f\u52a8\u7684\u5165\u53e3\u5c31\u5728bin\u4e0b\u9762\u7684appium.js\u8fd9\u4e2a\u6587\u4ef6\u91cc\u9762.\u5728\u4e00\u5f00\u59cb\u7684\u65f6\u5019\u8fd9\u4e2ajavascript\u5c31\u4f1a\u5148\u53bb\u5bfc\u5165\u5fc5\u987b\u7684\u6a21\u5757\u7136\u540e\u5bf9\u542f\u52a8\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n<\/p>\n<p>var net = require(\\\\\\&#8217;net\\\\\\&#8217;) , repl = require(\\\\\\&#8217;repl\\\\\\&#8217;) , logFactory = require(\\\\\\&#8217;..\/lib\/server\/logger.js\\\\\\&#8217;) , parser = require(\\\\\\&#8217;..\/lib\/server\/parser.js\\\\\\&#8217;); require(\\\\\\&#8217;colors\\\\\\&#8217;); var args = parser().parseArgs();\u53c2\u6570\u7684\u89e3\u6790\u65f6\u5728&lsquo;..\/lib\/server\/parser.js\\\\\\&#8217;\u91cc\u9762\u7684\uff0c\u6587\u4ef6\u4e00\u5f00\u59cb\u5c31\u6307\u5b9a\u4f7f\u7528\u4e86nodejs\u63d0\u4f9b\u7684\u4e13\u95e8\u5bf9\u53c2\u6570\u8fdb\u884c\u89e3\u6790\u7684argparse\u6a21\u5757\u7684 ArgumentPaser\u7c7b\uff0c\u5177\u4f53\u8fd9\u4e2a\u7c7b\u65f6\u600e\u4e48\u7528\u7684\u5927\u5bb6\u81ea\u5df1google\u5c31\u597d\u4e86\uff1a<\/p>\n<\/p>\n<p>var ap = require(\\\\\\&#8217;argparse\\\\\\&#8217;).ArgumentParser\u7136\u540e\u8be5javascript\u811a\u672c\u5c31\u4f1a\u5b9e\u4f8b\u5316\u8fd9\u4e2aArgumentParser\u6765\u542f\u52a8\u5bf9\u53c2\u6570\u7684\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p>\/\/ Setup all the command line argument parsing module.exports = function () { var parser = new ap({ version: pkgObj.version, addHelp: true, description: \\\\\\&#8217;A webdriver-compatible server for use with native and hybrid iOS and Android applications.\\\\\\&#8217; }); _.each(args, function (arg) { parser.addArgument(arg[0], arg[1]); }); parser.rawArgs = args; return parser; };ArgumentPaser\u4f1a\u5bf9\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u6bcf\u4e00\u4e2aargs\u8fdb\u884c\u5206\u6790\uff0c\u5982\u679c\u6709\u63d0\u4f9b\u5bf9\u5e94\u53c2\u6570\u8bbe\u7f6e\u7684\u5c31\u8fdb\u884c\u8bbe\u7f6e\uff0c\u6ca1\u6709\u7684\u8bdd\u5c31\u4f1a\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8fd9\u91cc\u6211\u4eec\u63d0\u51e0\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u53c2\u6570\u4f5c\u4e3a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p>var args = [ &#8230; [[\\\\\\&#8217;-a\\\\\\&#8217;, \\\\\\&#8217;&#8211;address\\\\\\&#8217;], { defaultValue: \\\\\\&#8217;0.0.0.0\\\\\\&#8217; , required: false , example: &quot;0.0.0.0&quot; , help: \\\\\\&#8217;IP Address to listen on\\\\\\&#8217; }], &#8230; [[\\\\\\&#8217;-p\\\\\\&#8217;, \\\\\\&#8217;&#8211;port\\\\\\&#8217;], { defaultValue: 4723 , required: false , type: \\\\\\&#8217;int\\\\\\&#8217; , example: &quot;4723&quot; , help: \\\\\\&#8217;port to listen on\\\\\\&#8217; }], &#8230; [[\\\\\\&#8217;-bp\\\\\\&#8217;, \\\\\\&#8217;&#8211;bootstrap-port\\\\\\&#8217;], { defaultValue: 4724 , dest: \\\\\\&#8217;bootstrapPort\\\\\\&#8217; , required: false , type: \\\\\\&#8217;int\\\\\\&#8217; , example: &quot;4724&quot; , help: \\\\\\'(Android-only) port to use on device to talk to Appium\\\\\\&#8217; }], &#8230; ]; address :\u6307\u5b9ahttp\u670d\u52a1\u5668\u76d1\u542c\u7684ip\u5730\u5740\uff0c\u6ca1\u6709\u6307\u5b9a\u7684\u8bdd\u9ed8\u8ba4\u5c31\u76d1\u542c\u672c\u673aport :\u6307\u5b9ahttp\u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u6ca1\u6709\u6307\u5b9a\u7684\u8bdd\u9ed8\u8ba4\u76d1\u542c4723\u7aef\u53e3bootstrap-port :\u6307\u5b9a\u8981\u8fde\u63a5\u4e0a\u5b89\u5353\u76ee\u6807\u673a\u5668\u7aef\u7684socket\u76d1\u542c\u7aef\u53e3\uff0c\u9ed8\u8ba44724<\/p>\n<\/p>\n<p>2. \u521b\u5efaExpress HTTP\u670d\u52a1\u5668<\/p>\n<\/p>\n<p>Appium\u652f\u6301\u4e24\u79cd\u65b9\u5f0f\u542f\u52a8\uff0c\u4e00\u79cd\u662f\u5728\u63d0\u4f9b&#8211;shell\u7684\u60c5\u51b5\u4e0b\u63d0\u4f9b\u4ea4\u4e92\u5f0f\u7f16\u8f91\u5668\u7684\u542f\u52a8\u65b9\u5f0f\uff0c\u8fd9\u4e2a\u5c31\u597d\u6bd4\u4f60\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u8f93\u5165node\uff0c\u7136\u540e\u5f39\u51fa\u547d\u4ee4\u884c\u4ea4\u4e92\u8f93\u5165\u754c\u9762\u8ba9\u4f60\u4e00\u884c\u884c\u7684\u8f93\u5165\u8c03\u8bd5\u8fd0\u884c;\u53e6\u5916\u4e00\u79cd\u5c31\u662f\u6211\u4eec\u6b63\u5e38\u7684\u542f\u52a8\u65b9\u5f0f\u800c\u4e0d\u9700\u8981\u7528\u6237\u7684\u4ea4\u4e92\uff0c\u8fd9\u4e2a\u4e5f\u5c31\u662f\u6211\u4eec\u4eca\u5929\u5173\u6ce8\u7684\u91cd\u70b9\uff1a<\/p>\n<\/p>\n<p>if (process.argv[2] &amp;&amp; process.argv[2].trim() === &quot;&#8211;shell&quot;) { startRepl(); } else { appium.run(args, function () { \/* console.log(\\\\\\&#8217;Rock and roll.\\\\\\&#8217;.grey); *\/ }); }\u8fd9\u91ccappium\u8fd9\u4e2a\u53d8\u91cf\u662f\u4ece\u5176\u4ed6\u5730\u65b9\u5bfc\u5165\u4e86\uff0c\u6211\u4eec\u56de\u5230\u811a\u672c\u8f83\u524d\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p>var args = parser().parseArgs(); logFactory.init(args); var appium = require(\\\\\\&#8217;..\/lib\/server\/main.js\\\\\\&#8217;);\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e2a\u811a\u672c\u9996\u5148\u4f1a\u8c03\u7528parser\u7684\u6a21\u5757\u53bb\u5206\u6790\u7528\u6237\u8f93\u5165\u7684\u53c2\u6570\u7136\u540e\u4fdd\u5b58\u8d77\u6765(\u81f3\u4e8e\u600e\u4e48\u89e3\u6790\u7684\u5c31\u4e0d\u53bb\u770b\u4e86\uff0c\u65e0\u975e\u662f\u8bfb\u53d6\u6bcf\u4e2a\u53c2\u6570\u7136\u540e\u4fdd\u5b58\u8d77\u6765\u800c\u5df2\uff0c\u5927\u5bb6\u770b\u4e0b\u672c\u4eba\u524d\u9762\u5206\u6790\u7684\u5176\u4ed6\u6e90\u7801\u662f\u600e\u4e48\u83b7\u5f97\u542f\u52a8\u53c2\u6570\u7684\u5c31\u6e05\u695a\u4e86)\uff0c\u7136\u540e\u5f80\u4e0b\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230appium\u8fd9\u4e2a\u53d8\u91cf\u662f\u4ece\\\\\\&#8217;..\/lib\/server\/main.js\\\\\\&#8217;\u8fd9\u4e2a\u811a\u672c\u5bfc\u8fdb\u6765\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5c31\u9700\u8981\u53bb\u5230\u8fd9\u4e2a\u811a\u672c\uff0c\u6d4f\u89c8\u5230\u811a\u672c\u6700\u4e0b\u9762\u7684\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p>module.exports.run = main;\u5b83\u662f\u628amain\u8fd9\u4e2a\u65b9\u6cd5\u4ee5run\u7684\u540d\u5b57\u5bfc\u51fa\u7ed9\u5176\u4ed6\u6a21\u5757\u4f7f\u7528\u4e86\uff0c\u6240\u4ee5\u56de\u5230\u4e86\u6700\u4e0a\u9762\u7684\uff1a<\/p>\n<\/p>\n<p>appium.run(args, function () { \/* console.log(\\\\\\&#8217;Rock and roll.\\\\\\&#8217;.grey); *\/ });<\/p>\n<\/p>\n<p>\u5c31\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86\\\\\\&#8217;main.js\\\\\\&#8217;\u7684:<\/p>\n<\/p>\n<p>main(args, function () { \/* console.log(\\\\\\&#8217;Rock and roll.\\\\\\&#8217;.grey); *\/ });\u6211\u4eec\u5f80\u4e0b\u770bmain\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u9996\u5148\u5b83\u4f1a\u505a\u4e00\u4e9b\u57fa\u672c\u7684\u53c2\u6570\u68c0\u67e5\uff0c\u7136\u540e\u521d\u59cb\u5316\u4e86\u4e00\u4e2aexpress\u5b9e\u4f8b(Express\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u57fa\u4e8eNode.js\u7684Web\u5f00\u53d1\u6846\u67b6\uff0c\u63d0\u4f9b\u5404\u79cd\u6a21\u5757\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u642d\u5efa\u4e00\u4e2a\u5177\u6709\u5b8c\u6574\u529f\u80fd\u7684\u7f51\u7ad9\uff0c\u5f3a\u70c8\u5efa\u8bae\u4e0d\u6e05\u695a\u5176\u4f7f\u7528\u7684\u7ae5\u978b\u5148\u53bb\u770b\u4e0b\u725b\u4eba\u962e\u4e00\u5cf0\u7684\u300aExpress\u6846\u67b6\u300b)\uff0c\u7136\u540e\u5982\u5e73\u5e38\u4e00\u6837\u521b\u5efa\u4e00\u4e2ahttp\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p>var main = function (args, readyCb, doneCb) { &#8230; var rest = express() , server = http.createServer(rest); &#8230; }<\/p>\n<\/p>\n<p>\u53ea\u662f\u8fd9\u4e2ahttp\u670d\u52a1\u5668\u8ddf\u666e\u901a\u7684\u670d\u52a1\u5668\u552f\u4e00\u7684\u5dee\u522b\u662fcreateServer\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u4ece\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u53d8\u6210\u4e86\u4e00\u4e2aEpress\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u5b83\u4f7f\u7528\u4e86express\u6846\u67b6\u5bf9http\u6a21\u5757\u8fdb\u884c\u518d\u5305\u88c5\u7684\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528express\u7684\u529f\u80fd\u548c\u65b9\u6cd5\u6765\u5feb\u901f\u5efa\u7acbhttp\u670d\u52a1\uff0c\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<p>\u901a\u8fc7 express\u7684get\uff0cpost\u7b49\u5feb\u901f\u8bbe\u7f6e\u8def\u7531\u3002\u7528\u4e8e\u6307\u5b9a\u4e0d\u540c\u7684\u8bbf\u95ee\u8def\u5f84\u6240\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u53eb\u505a&ldquo;\u8def\u7531&rdquo;(routing)\uff0c\u8fd9\u4e2a\u4e5f\u662f\u4e3a\u4ec0\u4e48\u8bf4express\u662f\u7b26\u5408RestFul\u98ce\u683c\u7684\u6846\u67b6\u7684\u539f\u56e0\u4e4b\u4e00\u4e86\u4f7f\u7528express\u7684use\u65b9\u6cd5\u6765\u8bbe\u7f6e\u4e2d\u95f4\u4ef6\u7b49\u3002\u81f3\u4e8e\u4ec0\u4e48\u662f\u4e2d\u95f4\u4ef6\uff0c\u7b80\u5355\u8bf4\uff0c\u4e2d\u95f4\u4ef6(middleware)\u5c31\u662f\u5904\u7406HTTP\u8bf7\u6c42\u7684\u51fd\u6570\uff0c\u7528\u6765\u5b8c\u6210\u5404\u79cd\u7279\u5b9a\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u68c0\u67e5\u7528\u6237\u662f\u5426\u767b\u5f55\u3001\u5206\u6790\u6570\u636e\u3001\u4ee5\u53ca\u5176\u4ed6\u5728\u9700\u8981\u6700\u7ec8\u5c06\u6570\u636e\u53d1\u9001\u7ed9\u7528\u6237\u4e4b\u524d\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u5b83\u6700\u5927\u7684\u7279\u70b9\u5c31\u662f\uff0c\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u5904\u7406\u5b8c\uff0c\u518d\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u3002<\/p>\n<\/p>\n<p>\u6bd4\u5982\u4e0a\u9762\u521b\u5efahttp\u670d\u52a1\u5668\u540e\u6240\u505a\u7684\u52a8\u4f5c\u5c31\u662f\u8bbe\u7f6e\u4e00\u5806\u4e2d\u95f4\u4ef6\u6765\u5b8c\u6210\u7279\u5b9a\u7684\u4efb\u52a1\u6765\u5904\u7406http\u8bf7\u6c42\u7684\uff1a<\/p>\n<\/p>\n<p>var main = function (args, readyCb, doneCb) { &#8230; rest.use(domainMiddleware()); rest.use(morgan(function (tokens, req, res) { \/\/ morgan output is redirected straight to winston logger.info(requestEndLoggingFormat(tokens, req, res), (res.jsonResp || \\\\\\&#8217;\\\\\\&#8217;).grey); })); rest.use(favicon(path.join(__dirname, \\\\\\&#8217;static\/favicon.ico\\\\\\&#8217;))); rest.use(express.static(path.join(__dirname, \\\\\\&#8217;static\\\\\\&#8217;))); rest.use(allowCrossDomain); rest.use(parserWrap); rest.use(bodyParser.urlencoded({extended: true})); \/\/ 8\/18\/14: body-parser requires that we supply the limit field to ensure the server can \/\/ handle requests large enough for Appium\\\\\\&#8217;s use cases. Neither Node nor HTTP spec defines a max \/\/ request size, so any hard-coded request-size limit is arbitrary. Units are in bytes (ie &quot;gb&quot; == &quot;GB&quot;, \/\/ not &quot;Gb&quot;). Using 1GB because&#8230;, well because it\\\\\\&#8217;s arbitrary and 1GB is sufficiently large for 99.99% \/\/ of testing scenarios while still providing an upperbounds to reduce the odds of squirrelliness. rest.use(bodyParser.json({limit: \\\\\\&#8217;1gb\\\\\\&#8217;})); &#8230; }\u6211\u4eec\u8fd9\u91cc\u4ee5\u7b2c\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u4e3a\u4f8b\u5b50\uff0c\u770b\u770b\u5b83\u662f\u600e\u4e48\u901a\u8fc7domain\u8fd9\u4e2a\u6a21\u5757\u6765\u5904\u7406\u5f02\u5e38\u7684(\u6ce8\u610fnotejs\u662f\u51fa\u540d\u7684\u5355\u7ebf\u7a0b\uff0c\u975e\u963b\u585e\u7684\u6846\u67b6\uff0c\u6b63\u5e38\u7684try,catch\u662f\u6293\u83b7\u4e0d\u4e86\u4efb\u4f55\u5f02\u5e38\u5904\u7406\u7684\uff0c\u56e0\u4e3a\u76f8\u5e94\u7684\u4ee3\u7801\u4e0d\u4f1a\u7b49\u5f85\u5982i\/o\u64cd\u4f5c\u7b49\u7ed3\u679c\u5c31\u7acb\u523b\u8fd4\u56de\u7684\uff0c\u6240\u4ee5nodejs\u540e\u6765\u5f15\u5165\u4e86domain\u8fd9\u4e2a\u6a21\u5757\u6765\u4e13\u95e8\u5904\u7406\u8fd9\u79cd\u4e8b\u60c5\u3002\u5176\u5b9e\u6211\u8ba4\u4e3a\u539f\u7406\u8fd8\u662f\u56de\u8c03\uff0c\u628ahttp\u8fc7\u6765\u7684nodejs\u63d0\u4f9b\u7684request\uff0c\u548cresponse\u53c2\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\u63d0\u4f9b\u7ed9\u56de\u8c03\u51fd\u6570\uff0c\u7136\u540e\u4e00\u65e6\u76f8\u5e94\u4e8b\u4ef6\u53d1\u751f\u4e86\u5c31\u51fa\u53d1\u56de\u8c03\u7136\u540e\u64cd\u4f5c\u8fd9\u4e24\u4e2a\u53c2\u6570\u8fdb\u884c\u8fd4\u56de)\uff1a<\/p>\n<\/p>\n<p>module.exports.domainMiddleware = function () { return function (req, res, next) { var reqDomain = domain.create(); reqDomain.add(req); reqDomain.add(res); res.on(\\\\\\&#8217;close\\\\\\&#8217;, function () { setTimeout(function () { reqDomain.dispose(); }, 5000); }); reqDomain.on(\\\\\\&#8217;error\\\\\\&#8217;, function (err) { logger.error(\\\\\\&#8217;Unhandled error:\\\\\\&#8217;, err.stack, getRequestContext(req)); }); reqDomain.run(next); }; };\u5927\u5bb6\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u56de\u8c03\u4e2d\u95f4\u4ef6(\u51fd\u6570)\uff1a<\/p>\n<\/p>\n<p>\u5148\u521b\u5efa\u4e00\u4e2adomain\u7136\u540e\u628ahttp\u7684request\u548cresponse\u589e\u52a0\u5230\u8fd9\u4e2adomain\u91cc\u9762\u7136\u540e\u9274\u5b9a\u76f8\u5e94\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u6bd4\u5982\u53d1\u751ferror\u7684\u65f6\u5019\u5c31\u6253\u5370\u76f8\u5e94\u7684\u65e5\u8bb0\u7136\u540e\u8c03\u7528\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6765\u8fdb\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u5904\u7406<\/p>\n<\/p>\n<p>\u5176\u4ed6\u7684\u4e2d\u95f4\u4ef6\u8fd9\u91cc\u6211\u5c31\u4e0d\u82b1\u65f6\u95f4\u4e00\u4e00\u53bb\u5206\u6790\u4e86\uff0c\u5927\u5bb6\u5404\u81ea\u8ddf\u8e2a\u4e0b\u6216\u8005google\u5e94\u8be5\u5c31\u6e05\u695a\u662f\u7528\u6765\u505a\u4ec0\u4e48\u4e8b\u60c5\u7684\u4e86\uff0c\u56e0\u4e3a\u6211\u81ea\u5df1\u5c31\u662f\u8fd9\u4e48\u5e72\u7684\u3002<\/p>\n<\/p>\n<p>main\u51fd\u6570\u5728\u4e3ahttp\u670d\u52a1\u5668\u5efa\u7acb\u597d\u4e2d\u95f4\u4ef6\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u53bb\u521b\u5efa\u4e00\u4e2aappium\u670d\u52a1\u5668\uff0c\u6ce8\u610f\u8fd9\u91ccappium\u670d\u52a1\u5668\u548chttp\u670d\u52a1\u5668\u662f\u4e0d\u4e00\u6837\u7684\uff0chttp\u670d\u52a1\u5668\u662f\u7528\u6765\u76d1\u542cappium\u5ba2\u6237\u7aef\uff0c\u4e5f\u5c31\u662fselenium\uff0c\u6211\u4eec\u7684\u811a\u672c\u53d1\u9001\u8fc7\u6765\u7684http\u7684rest\u8bf7\u6c42\u7684;appium\u670d\u52a1\u5668\u9664\u4e86\u62e5\u6709\u7740\u8fd9\u4e2ahttp\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u4e4b\u5916\uff0c\u8fd8\u5305\u542b\u5176\u4ed6\u5982\u548c\u76ee\u6807\u8bbe\u5907\u7aef\u7684bootstrap\u901a\u4fe1\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p>var main = function (args, readyCb, doneCb) { &#8230; \/\/ Instantiate the appium instance var appiumServer = appium(args); \/\/ Hook up REST http interface appiumServer.attachTo(rest); &#8230; }\u8fd9\u91cc\u4f1a\u53bb\u8c03\u7528appium\u6784\u9020\u51fd\u6570\u5b9e\u4f8b\u5316\u4e00\u4e2aappium\u670d\u52a1\u5668\uff0c\u7136\u540e\u628a\u521a\u624d\u521b\u5efa\u7684express\u5bf9\u8c61rest\u7ed9\u4f20\u5230\u8be5\u670d\u52a1\u5668\u5b9e\u4f8b\u4fdd\u5b58\u8d77\u6765\u3002\u90a3\u4e48\u8fd9\u91cc\u8fd9\u4e2aappium\u7c7b\u53c8\u662f\u4ece\u54ea\u91cc\u6765\u7684\u5462?\u6211\u4eec\u8fd4\u56de\u5230main.js\u7684\u524d\u9762\uff1a<\/p>\n<\/p>\n<p>var http = require(\\\\\\&#8217;http\\\\\\&#8217;) , express = require(\\\\\\&#8217;express\\\\\\&#8217;) &#8230; , appium = require(\\\\\\&#8217;..\/appium.js\\\\\\&#8217;)\u53ef\u4ee5\u770b\u5230\u5b83\u662f\u4ece\u4e0a\u5c42\u76ee\u5f55\u7684appium.js\u5bfc\u51fa\u6765\u7684\uff0c\u6211\u4eec\u8fdb\u53bb\u770b\u770b\u5b83\u7684\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p>var Appium = function (args) { this.args = _.clone(args); this.args.callbackAddress = this.args.callbackAddress || this.args.address; this.args.callbackPort = this.args.callbackPort || this.args.port; \/\/ we need to keep an unmodified copy of the args so that we can restore \/\/ any server arguments between sessions to their default values \/\/ (otherwise they might be overridden by session-level caps) this.serverArgs = _.clone(this.args); this.rest = null; this.webSocket = null; this.deviceType = null; this.device = null; this.sessionId = null; this.desiredCapabilities = {}; this.oldDesiredCapabilities = {}; this.session = null; this.preLaunched = false; this.sessionOverride = this.args.sessionOverride; this.resetting = false; this.defCommandTimeoutMs = this.args.defaultCommandTimeout * 1000; this.commandTimeoutMs = this.defCommandTimeoutMs; this.commandTimeout = null; };\u53ef\u4ee5\u770b\u5230\u521d\u59cb\u5316\u7684\u65f6\u5019this.rest\u8fd9\u4e2a\u6210\u5458\u53d8\u91cf\u662f\u8bbe\u7f6e\u6210null\u7684\uff0c\u6240\u4ee5\u521a\u63d0\u5230\u7684main\u4e2d\u7684\u6700\u540e\u4e00\u6b65\u5c31\u662f\u8c03\u7528\u8fd9\u4e2aappium.js\u4e2d\u7684attachTo\u65b9\u6cd5\u628aexpress\u5b9e\u4f8brest\u7ed9\u8bbe\u7f6e\u5230appium\u670d\u52a1\u5668\u5bf9\u8c61\u91cc\u9762\u7684\uff1a<\/p>\n<\/p>\n<p>Appium.prototype.attachTo = function (rest) { this.rest = rest; };\u5b9e\u4f8b\u5316appium \u670d\u52a1\u5668\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u8981\u8bbe\u7f6e\u597d\u4ececlient\u7aef\u8fc7\u6765\u7684\u8bf7\u6c42\u7684\u6570\u636e\u8def\u7531\u4e86,\u8fd9\u4e2a\u4e0b\u4e00\u7bc7\u6587\u7ae0\u8ba8\u8bbaAppium Server\u5982\u4f55\u8ddfbootstrap\u901a\u4fe1\u65f6\u4f1a\u53e6\u5916\u8fdb\u884c\u8ba8\u8bba\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5230\u5982\u4f55\u5c06\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u53d1\u9001\u7ed9bootstrap\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p>var main = function (args, readyCb, doneCb) { &#8230; routing(appiumServer); &#8230; }<\/p>\n<\/p>\n<p>\u8bbe\u7f6e\u597d\u8def\u7531\u540e\uff0cmain\u5f80\u540e\u5c31\u4f1a\u5bf9\u670d\u52a1\u5668\u505a\u4e00\u4e9b\u57fa\u672c\u914d\u7f6e\uff0c\u7136\u540e\u8c03\u7528helpers.js\u7684startListening\u65b9\u6cd5\u6765\u5f00\u542fhttp\u670d\u52a1\u5668\u7684\u76d1\u542c\u5de5\u4f5c\uff0c\u5927\u5bb6\u8981\u6ce8\u610f\u5230\u73b0\u5728\u4e3a\u6b62http\u670d\u52a1\u5668server\u65f6\u521b\u5efa\u8d77\u6765\u4e86\uff0c\u4f46\u662f\u8fd8\u6ca1\u6709\u771f\u6b63\u5f00\u59cb\u76d1\u542c\u63a5\u53d7\u8fde\u63a5\u548c\u6570\u636e\u7684\u7684\u5de5\u4f5c\u7684\uff1a<\/p>\n<\/p>\n<p>var main = function (args, readyCb, doneCb) { &#8230; function (cb) { startListening(server, args, parser, appiumVer, appiumRev, appiumServer, cb); } &#8230; }\u6ce8\u610f\u5b83\u4f20\u5165\u7684\u51e0\u4e2a\u91cd\u8981\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p>server\uff1a\u57fa\u4e8eexpress\u5b9e\u4f8b\u521b\u5efa\u7684http\u670d\u52a1\u5668\u5b9e\u4f8bargs\uff1a\u53c2\u6570parser\uff1a\u53c2\u6570\u89e3\u6790\u5668appiumVer: \u5728&lsquo;\\\\\\&#8217;..\/..\/package.json\\\\\\&#8217;&lsquo;\u6587\u4ef6\u4e2d\u6307\u5b9a\u7684appium\u7248\u672c\u53f7appiumRev\uff1a\u901a\u8fc7\u4e0a\u9762\u63d0\u53ca\u7684\u8fdb\u884c\u670d\u52a1\u5668\u57fa\u672c\u914d\u7f6e\u65f6\u89e3\u6790\u51fa\u6765\u7684\u7248\u672c\u4fee\u6b63\u53f7appiumServer: \u521a\u624d\u521b\u5efa\u7684appium\u670d\u52a1\u5668\u5b9e\u4f8b\uff0c\u91cc\u9762\u5305\u542b\u4e86\u4e00\u4e2aexpress\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u548c\u7b2c\u4e00\u4e2a\u53c2\u6570server\u7528\u6765\u521b\u5efahttp\u670d\u52a1\u5668\u7684express\u5b9e\u4f8b\u65f6\u4e00\u6837\u7684<\/p>\n<\/p>\n<p>3. \u542f\u52a8http\u670d\u52a1\u5668\u76d1\u542c \u5230\u4e86\u8fd9\u91cc\uff0c\u6574\u4e2a\u57fa\u4e8eExpress\u7684http\u670d\u52a1\u5668\u5df2\u7ecf\u51c6\u5907\u59a5\u5f53\uff0c\u53ea\u5dee\u4e00\u4e2ago\u547d\u4ee4\u4e86\uff0c\u8fd9\u4e2ago\u547d\u4ee4\u5c31\u662f\u6211\u4eec\u8fd9\u91cc\u7684\u542f\u52a8\u76d1\u542c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p>module.exports.startListening = function (server, args, parser, appiumVer, appiumRev, appiumServer, cb) { var alreadyReturned = false; server.listen(args.port, args.address, function () { var welcome = &quot;Welcome to Appium v&quot;   appiumVer; if (appiumRev) { welcome  = &quot; (REV &quot;   appiumRev   &quot;)&quot;; } logger.info(welcome); var logMessage = &quot;Appium REST http interface listener started on &quot;   args.address   &quot;:&quot;   args.port; logger.info(logMessage); startAlertSocket(server, appiumServer); if (args.nodeconfig !== null) { gridRegister.registerNode(args.nodeconfig, args.address, args.port); } var showArgs = getNonDefaultArgs(parser, args); if (_.size(showArgs)) { logger.debug(&quot;Non-default server args: &quot;   JSON.stringify(showArgs)); } var deprecatedArgs = getDeprecatedArgs(parser, args); if (_.size(deprecatedArgs)) { logger.warn(&quot;Deprecated server args: &quot;   JSON.stringify(deprecatedArgs)); } logger.info(\\\\\\&#8217;Console LogLevel: \\\\\\&#8217;   logger.transports.console.level); if (logger.transports.file) { logger.info(\\\\\\&#8217;File LogLevel: \\\\\\&#8217;   logger.transports.file.level); } });\u8fd9\u4e2a\u65b9\u6cd5\u770b\u4e0a\u53bb\u5f88\u957f\uff0c\u5176\u5b9e\u5f88\u591a\u90fd\u662f\u4f20\u7ed9\u76d1\u542c\u65b9\u6cd5\u7684\u56de\u8c03\u51fd\u6570\u7684\u540e\u671f\u53c2\u6570\u68c0\u67e5\u548c\u4fe1\u606f\u6253\u5370\u4ee5\u53ca\u9519\u8bef\u5904\u7406\uff0c\u5173\u952e\u7684\u5c31\u662f\u6700\u524d\u9762\u7684\u542f\u52a8http\u76d1\u542c\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p>server.listen(args.port, args.address, function () { &#8230; \u8fd9\u91cc\u7684server\u5c31\u662f\u4e0a\u9762\u63d0\u53ca\u7684\u57fa\u4e8eexpress\u6846\u67b6\u642d\u5efa\u7684Http Server\u5b9e\u4f8b\uff0c\u4f20\u5165\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p>args.port :\u5c31\u662f\u7b2c\u4e00\u8282\u63d0\u8d77\u7684http\u670d\u52a1\u5668\u7684\u76d1\u542c\u7aef\u53e3\uff0c\u9ed8\u8ba44723args.adress :\u5c31\u662f\u7b2c\u4e00\u8282\u63d0\u53ca\u7684http\u670d\u52a1\u5668\u76d1\u542c\u5730\u5740\uff0c\u9ed8\u8ba4\u672c\u5730function \uff1a\u4e00\u7cfb\u5217\u56de\u8c03\u51fd\u6570\u6765\u8fdb\u884c\u9519\u8bef\u5904\u7406\u7b49<\/p>\n<\/p>\n<p>4. \u5c0f\u7ed3 \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u63cf\u8ff0\u4e86appium server\u662f\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u4e8eexpress\u6846\u67b6\u7684http\u670d\u52a1\u5668\uff0c\u7136\u540e\u542f\u52a8\u76f8\u5e94\u7684\u76d1\u542c\u65b9\u6cd5\u6765\u83b7\u5f97\u4eceappium client\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u6570\u636e\u3002<\/p>\n<p>\u66f4\u591a\u5173\u4e8e\u4e91\u670d\u52a1\u5668\uff0c<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/domain.html\" title=\"\u57df\u540d\u6ce8\u518c\" target=\"_blank\">\u57df\u540d<\/a><\/span>\u6ce8\u518c\uff0c\u865a\u62df\u4e3b\u673a\u7684\u95ee\u9898\uff0c\u8bf7\u8bbf\u95ee<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">\u57a6\u6d3e\u79d1\u6280<\/a><\/span>\u5b98\u7f51\uff1a<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">www.kenpai.com<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4e0a\u4e00\u4e2a\u7cfb\u5217Appium Android Bootstrap\u6e90\u7801\u5206\u6790\u6211\u4eec\u4e86\u89e3\u5230\u4e86appium\u5728\u5b89\u5353\u76ee\u6807\u673a\u5668\u4e0a\u662f\u5982\u4f55\u901a\u8fc7bootstrap\u8fd9\u4e2a\u670d\u52a1\u6765\u63a5\u6536<\/p>\n","protected":false},"author":2,"featured_media":4350,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3322],"tags":[3906],"class_list":["post-5038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ktougao","tag-appium-server-express-http"],"_links":{"self":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/comments?post=5038"}],"version-history":[{"count":0,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5038\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media\/4350"}],"wp:attachment":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media?parent=5038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/categories?post=5038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/tags?post=5038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}