UNPKG

5.16 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.getLocalWhistleServer = exports.launchSync = exports.launch = void 0;
7const fs_1 = __importDefault(require("fs"));
8const matman_core_1 = require("matman-core");
9const lodash_1 = __importDefault(require("lodash"));
10const PageDriverSync_1 = __importDefault(require("./model/PageDriverSync"));
11const PageDriverAsync_1 = __importDefault(require("./model/PageDriverAsync"));
12const caller_1 = require("./util/caller");
13const whistle_1 = require("./util/whistle");
14/**
15 * 获取新的 PageDriverOpts
16 *
17 * @param {PageDriverOpts} opts
18 */
19function getPageDriverOpts(opts) {
20 const pageDriverOpts = lodash_1.default.merge({}, opts);
21 let { caseModuleFilePath } = pageDriverOpts;
22 // 特殊处理 caseModuleFilePath,如果没有,则自动获取
23 if (!caseModuleFilePath || !fs_1.default.existsSync(caseModuleFilePath)) {
24 // 注意,这里会有一些局限:假如使用者将该方法放在公共文件里面,则获取的值将会是同一个
25 // 此时,需要手动定义并传入 caseModuleFilePath 来规避
26 caseModuleFilePath = caller_1.getCallerPath();
27 // 如果无法获得 caseModuleFilePath,则直接抛出错误
28 if (!caseModuleFilePath || !fs_1.default.existsSync(caseModuleFilePath)) {
29 throw new Error(`Could not find caseModuleFilePath!`);
30 }
31 // 更新
32 pageDriverOpts.caseModuleFilePath = caseModuleFilePath;
33 }
34 // 自动计算是哪个文件在调用 case 脚本,然后以调用者的文件名来做标记
35 // 由于调用者脚本本身已经按目录存储,且同一个目录中不同调用者脚本文件名肯定不一样
36 // 这样就能够区分标记了
37 if (!pageDriverOpts.tag) {
38 pageDriverOpts.tag = caller_1.getCallerPath(caseModuleFilePath);
39 }
40 // 如果标签就是 case 文件本身路径,则清空,否则就会造成生成的文件名重叠
41 if (pageDriverOpts.tag === caseModuleFilePath) {
42 pageDriverOpts.tag = '';
43 }
44 return pageDriverOpts;
45}
46/**
47 * 获得同步的 PageDriver
48 *
49 * @param {BrowserRunner} browserRunner 浏览器运行器,目前支持 puppeteer 和 nightmare 两种
50 * @param {PageDriverOpts} pageDriverOpts
51 * @param {MatmanConfigOpts} matmanConfigOpts
52 */
53function launch(browserRunner, pageDriverOpts, matmanConfigOpts) {
54 // 处理下原始传入的 pageDriverOpts,部分设置默认值
55 const newPageDriverOpts = getPageDriverOpts(pageDriverOpts);
56 // 查找 matman config
57 const matmanConfig = matman_core_1.findMatmanConfig(newPageDriverOpts.caseModuleFilePath, matmanConfigOpts);
58 // matman config 必须得存在
59 if (!matmanConfig) {
60 throw new Error(`Could not find ${matman_core_1.MATMAN_CONFIG_FILE} or matman config setting!`);
61 }
62 // console.log('newPageDriverOpts', newPageDriverOpts);
63 // console.log('matmanConfig', matmanConfig);
64 return new PageDriverAsync_1.default(browserRunner, matmanConfig, newPageDriverOpts);
65}
66exports.launch = launch;
67/**
68 * 获得同步的 PageDriver
69 *
70 * @param {BrowserRunner} browserRunner 浏览器运行器,目前支持 puppeteer 和 nightmare 两种
71 * @param {PageDriverOpts} pageDriverOpts
72 * @param {MatmanConfigOpts} matmanConfigOpts
73 */
74function launchSync(browserRunner, pageDriverOpts, matmanConfigOpts) {
75 // 处理下原始传入的 pageDriverOpts,部分设置默认值
76 const newPageDriverOpts = getPageDriverOpts(pageDriverOpts);
77 // 查找 matman config
78 const matmanConfig = matman_core_1.findMatmanConfig(newPageDriverOpts.caseModuleFilePath, matmanConfigOpts);
79 // matman config 必须得存在
80 if (!matmanConfig) {
81 throw new Error(`Could not find ${matman_core_1.MATMAN_CONFIG_FILE} or matman config setting!`);
82 }
83 // console.log('newPageDriverOpts', newPageDriverOpts);
84 // console.log('matmanConfig', matmanConfig);
85 return new PageDriverSync_1.default(browserRunner, matmanConfig, newPageDriverOpts);
86}
87exports.launchSync = launchSync;
88/**
89 * 获得本地 whistle 地址
90 *
91 * @param {Number} port 指定端口
92 * @param {Boolean} doNotAutoCheckStartedPort 不需要自动获得已经启动的端口
93 */
94async function getLocalWhistleServer(port, doNotAutoCheckStartedPort) {
95 // process.env.WHISTLE_PORT 该值拥有最高优先级,主要用于自动化测试场景
96 if (process.env.WHISTLE_PORT) {
97 return `127.0.0.1:${process.env.WHISTLE_PORT}`;
98 }
99 let whisltePort;
100 if (!doNotAutoCheckStartedPort) {
101 try {
102 whisltePort = await whistle_1.checkIfWhistleStarted();
103 }
104 catch (e) {
105 if (process.env.IS_DEV === '1') {
106 console.log('checkIfWhistleStarted() catch', e);
107 }
108 }
109 }
110 if (!whisltePort) {
111 // whistle 默认的端口号为 8899
112 whisltePort = port || 8899;
113 }
114 return `127.0.0.1:${whisltePort}`;
115}
116exports.getLocalWhistleServer = getLocalWhistleServer;