UNPKG

9.8 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const cli_helper_1 = require("./cli-helper");
13const cli_1 = require("./cli");
14const prompts = require("prompts");
15const typesOfTest = [
16 { title: "HTML Page", value: "html" },
17 {
18 title: "REST API (JSON Format)",
19 value: "json"
20 },
21 {
22 title: "Browser (Puppeteer)",
23 value: "browser"
24 }
25];
26const canAdd = ["suite", "scenario", "env", "tag"];
27function addSuite() {
28 return __awaiter(this, void 0, void 0, function* () {
29 cli_helper_1.printSubheader("Add New Suite");
30 if (!cli_1.Cli.config.isValid()) {
31 cli_1.Cli.log("Config file is invalid.");
32 cli_1.Cli.exit(1);
33 }
34 const standardQuestions = yield prompts([
35 {
36 type: "text",
37 name: "suiteName",
38 message: "Name of Suite",
39 initial: cli_1.Cli.commandArg2 || "smoke",
40 format: cli_helper_1.trimInput,
41 validate: function (input) {
42 return /^[a-z0-9][a-z0-9/\/_-]{1,62}[a-z0-9]$/i.test(input);
43 }
44 },
45 {
46 type: "text",
47 name: "suiteDescription",
48 message: "Description of Suite",
49 initial: "Basic Smoke Test of Site",
50 format: cli_helper_1.trimInput,
51 validate: function (input) {
52 return /^[a-z0-9].{1,63}$/i.test(input);
53 }
54 },
55 {
56 type: "text",
57 name: "scenarioDescription",
58 message: "First Scenario",
59 initial: "Homepage Loads",
60 format: cli_helper_1.trimInput,
61 validate: function (input) {
62 return /^[a-z0-9].{1,63}$/i.test(input);
63 }
64 },
65 {
66 type: "select",
67 name: "type",
68 message: "What type of test is this scenario?",
69 initial: 0,
70 choices: typesOfTest
71 },
72 {
73 type: "text",
74 name: "scenarioPath",
75 message: "Scenario Start Path",
76 initial: "/",
77 format: cli_helper_1.trimInput,
78 validate: function (input) {
79 return /^\/.{0,63}$/i.test(input);
80 }
81 },
82 {
83 type: "list",
84 name: "tags",
85 message: "Add Tags (Optional)",
86 initial: "",
87 separator: " "
88 }
89 ]);
90 cli_1.Cli.log("");
91 yield cli_1.Cli.addSuite({
92 name: standardQuestions.suiteName,
93 description: standardQuestions.suiteDescription,
94 tags: standardQuestions.tags
95 }, {
96 description: standardQuestions.scenarioDescription,
97 type: standardQuestions.type,
98 path: standardQuestions.scenarioPath
99 });
100 cli_1.Cli.log("Created new test suite.");
101 cli_1.Cli.list([
102 "Suite file created: " + standardQuestions.suiteName,
103 "Scenario added: " + standardQuestions.scenarioDescription,
104 "Config file updated"
105 ]);
106 cli_1.Cli.log("");
107 cli_1.Cli.exit(0);
108 });
109}
110function addScenario() {
111 return __awaiter(this, void 0, void 0, function* () {
112 cli_helper_1.printSubheader("Add New Scenaio");
113 const suites = cli_helper_1.stringArrayToPromptChoices(cli_1.Cli.config.getSuiteNames());
114 if (suites.length == 0) {
115 cli_1.Cli.log("");
116 cli_1.Cli.log("You have not created any test suites yet. You should do that first.");
117 cli_1.Cli.log("");
118 cli_1.Cli.log("To add a test suite:");
119 cli_1.Cli.log("flagpole add suite");
120 cli_1.Cli.log("");
121 cli_1.Cli.exit(1);
122 }
123 const responses = yield prompts([
124 {
125 type: "select",
126 name: "suite",
127 message: "What suite do you want to add it to?",
128 initial: cli_1.Cli.commandArg2 || "",
129 choices: suites,
130 validate: function (input) {
131 return input.length > 0;
132 }
133 },
134 {
135 type: "select",
136 name: "type",
137 message: "What type of test is this scenario?",
138 initial: 0,
139 choices: typesOfTest
140 },
141 {
142 type: "text",
143 name: "scenarioDescription",
144 message: "Description of Scenario",
145 initial: "Some Other Page Loads",
146 format: cli_helper_1.trimInput,
147 validate: function (input) {
148 return /^[a-z0-9].{1,63}$/i.test(input);
149 }
150 },
151 {
152 type: "text",
153 name: "scenarioPath",
154 message: "Scenario Start Path",
155 initial: "/some-other-page",
156 format: cli_helper_1.trimInput,
157 validate: function (input) {
158 return /^\/.{0,63}$/i.test(input);
159 }
160 }
161 ]);
162 const suite = cli_1.Cli.config.suites[responses.suite];
163 if (!suite) {
164 cli_1.Cli.log(`Invalid suite: ${responses.suite}`);
165 cli_1.Cli.log("");
166 cli_1.Cli.exit(1);
167 }
168 yield cli_1.Cli.addScenario(suite, {
169 description: responses.scenarioDescription,
170 path: responses.scenarioPath,
171 type: responses.type
172 });
173 cli_1.Cli.log("Appended new scenario to suite:");
174 cli_1.Cli.log(suite.getSourcePath());
175 cli_1.Cli.log("");
176 cli_1.Cli.log("Scenario added to that suite:");
177 cli_1.Cli.log(responses.scenarioDescription);
178 cli_1.Cli.log("");
179 cli_1.Cli.exit(0);
180 });
181}
182function addEnv() {
183 return __awaiter(this, void 0, void 0, function* () {
184 cli_helper_1.printSubheader("Add New Environment");
185 const responses = yield prompts([
186 {
187 type: "text",
188 name: "name",
189 message: "What do you want to call the environment?",
190 initial: cli_1.Cli.commandArg2 || "",
191 validate: function (input) {
192 return /^[a-z0-9]{1,12}$/i.test(input);
193 }
194 },
195 {
196 type: "text",
197 name: "defaultDomain",
198 message: "Default Domain (optional)",
199 format: cli_helper_1.trimInput
200 }
201 ]);
202 cli_1.Cli.config.addEnvironment({
203 name: responses.name,
204 defaultDomain: responses.defaultDomain
205 });
206 yield cli_1.Cli.config.save();
207 cli_1.Cli.log("Added new environment.");
208 cli_1.Cli.list(["Config file updated"]);
209 cli_1.Cli.log("");
210 cli_1.Cli.exit(0);
211 });
212}
213function addTag() {
214 return __awaiter(this, void 0, void 0, function* () {
215 const responses = yield prompts([
216 {
217 type: "text",
218 name: "tag",
219 message: "Tag to Add",
220 validate: tag => {
221 return /^[a-z][a-z0-9_-][a-z0-0]+$/i.test(tag)
222 ? true
223 : "Tag should be a single alpha-numeric word";
224 },
225 format: cli_helper_1.trimInput
226 },
227 {
228 type: "multiselect",
229 name: "suites",
230 min: 1,
231 message: "Suites to apply it to",
232 choices: cli_helper_1.stringArrayToPromptChoices(cli_1.Cli.config.getSuiteNames())
233 }
234 ]);
235 responses.suites.forEach((suiteName) => {
236 cli_1.Cli.config.suites[suiteName].addTag(responses.tag);
237 });
238 cli_1.Cli.config.save();
239 });
240}
241function add() {
242 return __awaiter(this, void 0, void 0, function* () {
243 cli_1.Cli.hideBanner = true;
244 cli_helper_1.printHeader();
245 let type = cli_1.Cli.commandArg || "";
246 if (!canAdd.includes(type)) {
247 type = (yield prompts({
248 type: "select",
249 name: "thingToAdd",
250 message: "What do you want to add?",
251 choices: [
252 { value: "suite", title: "Suite" },
253 { value: "scenario", title: "Scenario" },
254 { value: "env", title: "Environment" },
255 { value: "tag", title: "Tag" }
256 ]
257 })).thingToAdd;
258 }
259 if (type == "scenario") {
260 addScenario();
261 }
262 else if (type == "env") {
263 addEnv();
264 }
265 else if (type == "tag") {
266 addTag();
267 }
268 else {
269 addSuite();
270 }
271 });
272}
273exports.add = add;
274//# sourceMappingURL=add.js.map
\No newline at end of file