playwright-json-runner
Version:
Extends Playwright to run tests using JSON-based test definitions.
235 lines (226 loc) • 5.99 kB
JavaScript
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var zod = require('zod');
var zodToJsonSchema = require('zod-to-json-schema');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
var zodToJsonSchema__default = /*#__PURE__*/_interopDefault(zodToJsonSchema);
var withLabelSchema = zod.z.object({
label: zod.z.string().optional()
});
var withDescriptionSchema = zod.z.object({
description: zod.z.string().optional()
});
var testObjectSchema = withLabelSchema.merge(withDescriptionSchema);
var PlaywrightRoleOptionsSchema = zod.z.object({
checked: zod.z.boolean().optional(),
disabled: zod.z.boolean().optional(),
exact: zod.z.boolean().optional(),
expanded: zod.z.boolean().optional(),
includeHidden: zod.z.boolean().optional(),
level: zod.z.number().optional(),
// For name, accept string or RegExp.
// If you strictly need to parse only real RegExp objects at runtime, keep it like this.
// If you want to accept a "string that might be a pattern," consider a string-based approach.
name: zod.z.union([zod.z.string(), zod.z.instanceof(RegExp)]).optional(),
pressed: zod.z.boolean().optional(),
selected: zod.z.boolean().optional()
}).optional();
var PlaywrightRoleSchema = zod.z.enum([
"alert",
"alertdialog",
"application",
"article",
"banner",
"blockquote",
"button",
"caption",
"cell",
"checkbox",
"code",
"columnheader",
"combobox",
"complementary",
"contentinfo",
"definition",
"deletion",
"dialog",
"directory",
"document",
"emphasis",
"feed",
"figure",
"form",
"generic",
"grid",
"gridcell",
"group",
"heading",
"img",
"insertion",
"link",
"list",
"listbox",
"listitem",
"log",
"main",
"marquee",
"math",
"meter",
"menu",
"menubar",
"menuitem",
"menuitemcheckbox",
"menuitemradio",
"navigation",
"none",
"note",
"option",
"paragraph",
"presentation",
"progressbar",
"radio",
"radiogroup",
"region",
"row",
"rowgroup",
"rowheader",
"scrollbar",
"search",
"searchbox",
"separator",
"slider",
"spinbutton",
"status",
"strong",
"subscript",
"superscript",
"switch",
"tab",
"table",
"tablist",
"tabpanel",
"term",
"textbox",
"time",
"timer",
"toolbar",
"tooltip",
"tree",
"treegrid",
"treeitem"
]);
// src/schemas/locators/locator-parameters.ts
var selectorStrategyParamsSchema = zod.z.object({
type: zod.z.literal("selector"),
value: zod.z.string()
});
var roleStrategyParamsSchema = zod.z.object({
type: zod.z.literal("role"),
value: zod.z.object({
role: PlaywrightRoleSchema,
options: PlaywrightRoleOptionsSchema
}).describe("the values for role are role name and then optiosn object e.g. {value: {role: 'link', options: {name: 'sign on'}}}")
});
var testIdStrategyParamsSchema = zod.z.object({
type: zod.z.literal("testId"),
value: zod.z.string()
});
var textStrategyParamsSchema = zod.z.object({
type: zod.z.literal("text"),
value: zod.z.string()
});
var locatorParamsSchema;
var nestedStrategyParamsSchema = zod.z.lazy(
() => zod.z.object({
type: zod.z.literal("nested"),
parent: locatorParamsSchema,
child: locatorParamsSchema
})
);
locatorParamsSchema = zod.z.union([
selectorStrategyParamsSchema,
roleStrategyParamsSchema,
testIdStrategyParamsSchema,
textStrategyParamsSchema,
nestedStrategyParamsSchema
]);
var actionTypeSchema = zod.z.enum([
"setfieldvalue",
"click",
"navigate",
"expect",
"assertFieldValueEquals",
"assertFieldValueContains",
"assertElementExists",
"sleep"
]).describe("The type of action to perform");
var testActionSchema = testObjectSchema.extend({
type: actionTypeSchema,
value: zod.z.string().optional(),
playwrightFunction: zod.z.string().optional().describe("on verify steps, the expect function to use (e.g. toBe is the playwright equivalent to: expect(locator).toBe(value)"),
locator: locatorParamsSchema.optional().describe("Locator to use for the action"),
selector: zod.z.string().optional().describe("Selector to use for the action (replaces locator)")
});
var testStepSchema = testObjectSchema.extend({
description: zod.z.string(),
actions: zod.z.array(testActionSchema)
});
// src/schemas/test-scenario.ts
var testScenarioSchema = testObjectSchema.extend({
name: zod.z.string(),
steps: zod.z.array(testStepSchema)
});
// src/schemas/test-run.ts
var testRunSchema = testObjectSchema.extend({
browser: zod.z.enum(["chrome", "firefox", "webkit"]),
host: zod.z.string(),
scenarios: zod.z.array(testScenarioSchema)
});
function dumpSchema() {
return zodToJsonSchema__default.default(testRunSchema, {
name: "TestRun"
});
}
(async function main() {
const [, , command, outputFile] = process.argv;
if (command === "dump-json-schema") {
const fileName = outputFile || "playwright-json-runner-schema.json";
try {
const schemaJson = await dumpSchema();
fs__namespace.writeFileSync(fileName, typeof schemaJson === "string" ? schemaJson : JSON.stringify(schemaJson, null, 2));
console.log(`Schema dumped to ${fileName}`);
} catch (error) {
console.error("Error dumping JSON Schema:", error);
process.exit(1);
}
} else {
console.log(
`Usage:
playwright-json-runner dump-json-schema <outputFile>
Example:
playwright-json-runner dump-json-schema schema.json`
);
process.exit(0);
}
})();
//# sourceMappingURL=cli.js.map
//# sourceMappingURL=cli.js.map