UNPKG

@serenity-js/playwright-test

Version:

Serenity/JS test runner adapter for Playwright Test, combining Playwright's developer experience with the advanced reporting and automation capabilities of Serenity/JS

94 lines 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventFactory = void 0; const core_1 = require("@serenity-js/core"); const events_1 = require("@serenity-js/core/events"); const io_1 = require("@serenity-js/core/io"); const model_1 = require("@serenity-js/core/model"); const PlaywrightSceneId_js_1 = require("./PlaywrightSceneId.js"); class EventFactory { requirementsHierarchy; constructor(rootDirectory) { this.requirementsHierarchy = new io_1.RequirementsHierarchy(new io_1.FileSystem(rootDirectory)); } createSceneStartEvents(test, result) { const sceneId = PlaywrightSceneId_js_1.PlaywrightSceneId.from(test.parent.project()?.name, test, result); const startTime = new core_1.Timestamp(result.startTime); const project = test.parent.project(); const projectName = project?.name ?? ''; const scenarioDetails = this.scenarioDetailsFrom(test); const allTags = this.tagsFrom(scenarioDetails, test.tags); if (projectName) { allTags.push(new model_1.ProjectTag(projectName)); } const events = []; if (test.retries > 0) { events.push(...this.createSceneSequenceEvents(sceneId, startTime, scenarioDetails, test, result)); } events.push(new events_1.SceneStarts(sceneId, scenarioDetails, startTime), new events_1.TestRunnerDetected(sceneId, new model_1.Name('Playwright'), startTime), ...allTags.map(tag => new events_1.SceneTagged(sceneId, tag, startTime))); return events; } createSceneSequenceEvents(sceneId, startTime, scenarioDetails, test, result) { const attempt = result.retry + 1; const parameters = { Retries: `Attempt #${attempt}` }; return [ new events_1.SceneSequenceDetected(sceneId, scenarioDetails, startTime), new events_1.SceneTemplateDetected(sceneId, new model_1.Description(''), startTime), new events_1.SceneParametersDetected(sceneId, scenarioDetails, new model_1.ScenarioParameters(new model_1.Name(''), new model_1.Description(`Max retries: ${test.retries}`), parameters)), ]; } createSceneFinishedEvent(test, result, scenarioOutcome) { const sceneId = PlaywrightSceneId_js_1.PlaywrightSceneId.from(test.parent.project()?.name, test, result); const duration = core_1.Duration.ofMilliseconds(result.duration); const sceneEndTime = new core_1.Timestamp(result.startTime).plus(duration); const scenarioDetails = this.scenarioDetailsFrom(test); return new events_1.SceneFinished(sceneId, scenarioDetails, scenarioOutcome, sceneEndTime); } uniqueTags(...tags) { const uniqueTags = {}; for (const tag of tags) { const { name, type } = tag.toJSON(); const key = `${name} ${type}`; uniqueTags[key] = tag; } return Object.values(uniqueTags); } scenarioDetailsFrom(test) { const { featureName, name } = this.scenarioMetadataFrom(test); const { file, line, column } = test.location; const nameWithoutTags = model_1.Tags.stripFrom(name); const repetitionSuffix = test.repeatEachIndex ? ` - Repetition ${test.repeatEachIndex}` : ''; const scenarioName = `${nameWithoutTags}${repetitionSuffix}`; return new model_1.ScenarioDetails(new model_1.Name(scenarioName), new model_1.Category(model_1.Tags.stripFrom(featureName)), new io_1.FileSystemLocation(io_1.Path.from(file), line, column)); } scenarioMetadataFrom(test) { const [root_, browserName_, fileName, describeOrItBlockTitle, ...nestedTitles] = test.titlePath(); const scenarioName = nestedTitles.join(' ').trim(); const name = scenarioName || describeOrItBlockTitle; const featureName = scenarioName ? describeOrItBlockTitle : fileName; return { featureName, name, }; } tagsFrom(scenarioDetails, extraTagValues) { const tagsFromRequirementsHierarchy = this.requirementsHierarchy.requirementTagsFor(scenarioDetails.location.path, scenarioDetails.category.value); const tagsFromTitle = model_1.Tags.from([ scenarioDetails.category.value, scenarioDetails.name.value, ].join(' ')); const extraTags = extraTagValues .filter(Boolean) .flatMap(tagValue => model_1.Tags.from(tagValue)); return [ ...tagsFromRequirementsHierarchy, ...this.uniqueTags(...tagsFromTitle, ...extraTags) ]; } } exports.EventFactory = EventFactory; //# sourceMappingURL=EventFactory.js.map