UNPKG

@cuppet/core

Version:

Core testing framework components for Cuppet - BDD framework based on Cucumber and Puppeteer

128 lines (112 loc) 4.88 kB
const { BeforeAll, AfterAll, Before, After, AfterStep, Status } = require('@cucumber/cucumber'); const { BrowserManager, AppiumManager, MqttManager, KafkaManager } = require('./managers'); const fs = require('fs'); const config = require('config'); const dataStore = require('../../src/dataStorage'); const profile = process.env.NODE_CONFIG_ENV || 'default'; let screenshotPath = config.get('screenshotsPath').toString() ?? 'screenshots/'; // ==== BeforeAll and AfterAll do not have access to test scope 'this' // ==== Before and After do // executed once before any test BeforeAll(async function () { await dataStore.createFile(); console.log(`Tests started at: ${new Date()}`); }); AfterStep(async function (testCase) { /** * If the test is not passed or the test is not tagged with @api, take a screenshot */ const arrayTags = testCase.pickle.tags; const found = arrayTags.find((item) => item.name === '@api'); if (testCase.result.status !== Status.PASSED && found === undefined) { let stepName = testCase.pickle.uri; const name = stepName.replace(/[/\\.]/g, '_'); const baseScreenshotPath = `${screenshotPath}${profile}`; if (!fs.existsSync(baseScreenshotPath)) { fs.mkdirSync(baseScreenshotPath, { recursive: true }); } let path = `${baseScreenshotPath}/screenshot_${name}.png`; let screenshot = await this.page.screenshot({ path: path, fullPage: true }); console.log(`Screenshot taken: ${name}`); // Convert Uint8Array to Buffer because Cucumber cannot work directly with Uint8Arrays const buffer = Buffer.from(screenshot.buffer, screenshot.byteOffset, screenshot.byteLength); this.attach(buffer, 'image/png'); } }); // executed once after all tests AfterAll(async function () { const date = new Date(); console.log(`Tests completed at: ${date.toString()}`); }); // executed before every test Before(async function (testCase) { // Get Browser config arguments array const browserArgs = config.get('browserOptions.args'); // Get default browser viewport const browserViewport = config.get('browserOptions.viewport.default'); // Check for basic auth credentials in config let credentials; if (config.has('basicAuth')) { credentials = { username: config.get('basicAuth.authUser').toString(), password: config.get('basicAuth.authPass').toString(), }; } // Get Appium capabilities from config const appiumCapabilities = config.get('appiumCapabilities'); // Check if the test is tagged with @appium, @mqtt or neither const arrayTags = testCase.pickle.tags; const appiumTag = arrayTags.find((item) => item.name === '@appium'); const mqttTag = arrayTags.find((item) => item.name === '@mqtt'); const apiTag = arrayTags.find((item) => item.name === '@api'); const kafkaTag = arrayTags.find((item) => item.name === '@kafka'); // Initialize MQTT Manager if @mqtt tag is present if (mqttTag) { const mqttManager = new MqttManager(); await mqttManager.initialize(); this.mqttManager = mqttManager; } // Initialize Kafka Manager if @kafka tag is present if (kafkaTag) { const kafkaManager = new KafkaManager(); await kafkaManager.initialize(); this.kafkaManager = kafkaManager; } // Initialize browser unless it's API-only or MQTT-only test if (!appiumTag && !apiTag && !mqttTag && !kafkaTag) { const browserManager = new BrowserManager(browserViewport, browserArgs, credentials); await browserManager.initialize(); // Assign created browser, page, and scenario name to global variables this.browserManager = browserManager; this.browser = browserManager.browser; this.page = browserManager.page; this.scenarioName = testCase.pickle.name; } else if (appiumTag) { const appiumManager = new AppiumManager(appiumCapabilities); await appiumManager.initialize(); this.appiumManager = appiumManager; this.appiumDriver = appiumManager.appiumDriver; } }); // executed after every test After(async function (testCase) { if (testCase.result?.status !== Status.PASSED) { console.log(`Scenario: '${testCase.pickle.name}' - has failed...\r\n`); } // Cleanup MQTT connection if present if (this.mqttManager) { await this.mqttManager.stop(); this.mqttManager = null; } // Cleanup browser/appium connections if (this.browser) { await this.browserManager.stop(); } else if (this.appiumDriver) { await this.appiumManager.stop(); } // Cleanup Kafka connection if present if (this.kafkaManager) { await this.kafkaManager.stop(); this.kafkaManager = null; } });