UNPKG

create-testplane

Version:

A tool for fast and simple Testplane configuration

157 lines (149 loc) 7.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extendWithTypescript = exports.connectTestingLibraryToTestplaneConfig = exports.asExpression = exports.addModule = exports.defineVariable = exports.writeTestExample = exports.printSuccessMessage = exports.baseGeneralPromptsHandler = exports.packageNameFromPlugin = exports.optsFromArgv = exports.inquirerPrompt = void 0; const lodash_1 = __importDefault(require("lodash")); const path_1 = __importDefault(require("path")); const inquirer_1 = require("./inquirer"); const colors_1 = require("./colors"); const packageManagement_1 = require("../constants/packageManagement"); const fsUtils_1 = __importDefault(require("../fsUtils")); const defaultTestplaneConfig_1 = require("../constants/defaultTestplaneConfig"); var inquirer_2 = require("./inquirer"); Object.defineProperty(exports, "inquirerPrompt", { enumerable: true, get: function () { return inquirer_2.inquirerPrompt; } }); const optsFromArgv = (argv) => { if (!argv["_"].length) { console.info(colors_1.Colors.fillYellow(`Initializing project in ${process.cwd()}`)); argv["_"] = ["."]; } return { path: path_1.default.resolve(process.cwd(), argv["_"][0]), language: argv.lang === "js" ? "js" : "ts", extraQuestions: Boolean(argv.verbose), }; }; exports.optsFromArgv = optsFromArgv; const packageNameFromPlugin = (plugin) => { for (const pluginSuffix of packageManagement_1.pluginSuffixes) { if (plugin.endsWith(pluginSuffix)) { return plugin.substring(0, plugin.length - pluginSuffix.length); } } return plugin; }; exports.packageNameFromPlugin = packageNameFromPlugin; const baseGeneralPromptsHandler = async (testplaneConfig, answers) => { if (lodash_1.default.isString(answers.baseUrl)) { testplaneConfig.baseUrl = answers.baseUrl; } if (lodash_1.default.isString(answers.gridUrl)) { testplaneConfig.gridUrl = answers.gridUrl; } if (answers.addChromePhone) { const browserId = "chrome-phone"; const version = await (0, inquirer_1.inquirerPrompt)({ type: "input", message: "chrome-phone: Enter android chrome-phone version (ex: phone-67.1):", }); lodash_1.default.mergeWith(testplaneConfig, { sets: { "touch-phone": { browsers: [browserId], files: [`${defaultTestplaneConfig_1.defaultTestplaneTestsDir}/**/*.testplane.(t|j)s`], }, }, browsers: { [browserId]: { desiredCapabilities: { platformName: "Android", deviceName: "android", version, browserName: "chrome", }, }, }, }, (objValue, srcValue) => (lodash_1.default.isArray(objValue) ? lodash_1.default.uniq(objValue.concat(srcValue)) : undefined)); } return testplaneConfig; }; exports.baseGeneralPromptsHandler = baseGeneralPromptsHandler; const printSuccessMessage = (configNotes) => { const successMessage = ` Inside that directory, you can run: ${colors_1.Colors.fillTeal("npx testplane")} Runs testplane tests ${colors_1.Colors.fillTeal("npx testplane gui")} Launches testplane's gui `; console.info(successMessage); if (configNotes.length) { console.info("You also need to complete the setup:"); configNotes.forEach(note => console.info(` ${colors_1.Colors.fillYellow((0, exports.packageNameFromPlugin)(note.plugin))} ${note.configNote} `)); } }; exports.printSuccessMessage = printSuccessMessage; const writeTestExample = async (dirPath, ext) => { const testExample = ` describe("test examples", () => { it("docs search test", async ({browser}) => { await browser.openAndWait("https://testplane.io/"); // Find by tag name const navBar = await browser.$("nav"); // Find by aria-label await navBar.$("aria/Search").click(); // Find by placeholder const fileSearchInput = await browser.findByPlaceholderText("Search docs"); await fileSearchInput.waitForDisplayed(); await fileSearchInput.setValue("config"); // Find by selector const fileSearchResults = await browser.$("ul[aria-labelledby='docsearch-label']"); // Find by role const fileSearchResultsItems = await fileSearchResults.findAllByRole("option"); await expect(fileSearchResultsItems.length).toBeGreaterThan(1); }); }); `; await fsUtils_1.default.writeTest(dirPath, `example.testplane.${ext}`, testExample); }; exports.writeTestExample = writeTestExample; const asString = (str, quote) => { const escapedString = str.replace(new RegExp(quote, "gi"), `\\${quote}`); return quote + escapedString + quote; }; const defineVariable = (config, { name, value, isExpr }) => { const quote = lodash_1.default.get(config, ["__template", "quote"], "'"); return lodash_1.default.set(config, ["__variables", name], isExpr ? value : asString(value, quote)); }; exports.defineVariable = defineVariable; const addModule = (config, variableName, moduleName = variableName) => { return lodash_1.default.set(config, ["__modules", variableName], moduleName); }; exports.addModule = addModule; const asExpression = (value) => `__expression: ${value}`; exports.asExpression = asExpression; const connectTestingLibraryToTestplaneConfig = (config) => { (0, exports.addModule)(config, "{ setupBrowser }", "@testplane/testing-library"); if (config.__language === "js") { const prepareBrowserJsFunction = (0, exports.asExpression)("browser => { setupBrowser(browser) }"); lodash_1.default.set(config, "prepareBrowser", prepareBrowserJsFunction); } else if (config.__language === "ts") { (0, exports.addModule)(config, "type { WdioBrowser }", "testplane"); const prepareBrowserTsFunction = (0, exports.asExpression)("(browser: WdioBrowser) => { setupBrowser(browser) }"); lodash_1.default.set(config, "prepareBrowser", prepareBrowserTsFunction); } }; exports.connectTestingLibraryToTestplaneConfig = connectTestingLibraryToTestplaneConfig; const extendWithTypescript = async (packageNamesToInstall, appPath) => { packageNamesToInstall.push("typescript"); const testplaneTsConfigPath = path_1.default.join(appPath, defaultTestplaneConfig_1.defaultTestplaneTestsDir, "tsconfig.json"); const defaultTestplaneTsConfig = lodash_1.default.set({}, ["compilerOptions", "types"], ["@testplane/globals", "@testplane/testing-library"]); await fsUtils_1.default.writeJson(testplaneTsConfigPath, defaultTestplaneTsConfig); }; exports.extendWithTypescript = extendWithTypescript; //# sourceMappingURL=index.js.map