|
| 1 | +import { createBdd } from "playwright-bdd"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | + |
| 4 | +import { test } from "../../fixtures"; |
| 5 | + |
| 6 | +import { expect } from "../../assertions"; |
| 7 | + |
| 8 | +import { SbomListPage } from "../../pages/sbom-list/SbomListPage"; |
| 9 | +import { SbomDetailsPage } from "../../pages/sbom-details/SbomDetailsPage"; |
| 10 | +import { DetailsPage } from "../../helpers/DetailsPage"; |
| 11 | +import { SearchPage } from "../../helpers/SearchPage"; |
| 12 | +import { clickAndVerifyDownload } from "../../pages/Helpers"; |
| 13 | +import { |
| 14 | + downloadLicenseReport, |
| 15 | + extractLicenseReport, |
| 16 | + findCsvWithHeader, |
| 17 | + findPackageRow, |
| 18 | +} from "../../pages/LicenseExportHelpers"; |
| 19 | + |
| 20 | +export const { Given, When, Then } = createBdd(test); |
| 21 | + |
| 22 | +let downloadedFilename: string; |
| 23 | +let downloadedFilePath: string; |
| 24 | +let extractionPath: string; |
| 25 | +let packageLicenseFilePath: string; |
| 26 | +let licenseReferenceFilePath: string; |
| 27 | +let selectedPackageRow: Record<string, string>; |
| 28 | + |
| 29 | +Given( |
| 30 | + "User Searches for CycloneDX SBOM {string} using Search Text box", |
| 31 | + async ({ page }, sbomName: string) => { |
| 32 | + const listPage = await SbomListPage.build(page); |
| 33 | + const toolbar = await listPage.getToolbar(); |
| 34 | + await toolbar.applyFilter({ "Filter text": sbomName }); |
| 35 | + }, |
| 36 | +); |
| 37 | + |
| 38 | +Given( |
| 39 | + "User Searches for CycloneDX SBOM {string} using Search Text box and Navigates to Search results page", |
| 40 | + async ({ page }, sbomName: string) => { |
| 41 | + const searchPage = new SearchPage(page, "Search"); |
| 42 | + await searchPage.generalSearch("SBOMs", sbomName); |
| 43 | + }, |
| 44 | +); |
| 45 | + |
| 46 | +When( |
| 47 | + "User Selects CycloneDX SBOM {string} from the Search Results", |
| 48 | + async ({ page }, sbomName: string) => { |
| 49 | + const listPage = await SbomListPage.fromCurrentPage(page); |
| 50 | + const table = await listPage.getTable(); |
| 51 | + await table.waitUntilDataIsLoaded(); |
| 52 | + |
| 53 | + await expect(table).toHaveColumnWithValue("Name", sbomName); |
| 54 | + }, |
| 55 | +); |
| 56 | + |
| 57 | +When( |
| 58 | + "User Clicks on SBOM name hyperlink from the Search Results", |
| 59 | + async ({ page }) => { |
| 60 | + const listPage = await SbomListPage.fromCurrentPage(page); |
| 61 | + const table = await listPage.getTable(); |
| 62 | + const rows = await table.getRows(); |
| 63 | + await rows.first().getByRole("link").click(); |
| 64 | + }, |
| 65 | +); |
| 66 | + |
| 67 | +Then("Application Navigates to SBOM Explorer page", async ({ page }) => { |
| 68 | + await expect(page).toHaveURL(/\/sboms\/[^/]+/); |
| 69 | +}); |
| 70 | + |
| 71 | +When('User Clicks "Action" button', async ({ page }) => { |
| 72 | + const detailsPage = new DetailsPage(page); |
| 73 | + await detailsPage.openActionsMenu(); |
| 74 | +}); |
| 75 | + |
| 76 | +Then('"Download License Report" Option should be visible', async ({ page }) => { |
| 77 | + const detailsPage = new DetailsPage(page); |
| 78 | + await detailsPage.verifyActionIsVisibleInMenu("Download License Report"); |
| 79 | +}); |
| 80 | + |
| 81 | +When('Selects "Download License Report" option', async ({ page }) => { |
| 82 | + const detailsPage = new DetailsPage(page); |
| 83 | + downloadedFilename = await clickAndVerifyDownload( |
| 84 | + page, |
| 85 | + async () => |
| 86 | + await detailsPage.page |
| 87 | + .getByRole("menuitem", { name: "Download License Report" }) |
| 88 | + .click(), |
| 89 | + ); |
| 90 | +}); |
| 91 | + |
| 92 | +Then( |
| 93 | + "Licenses associated with the SBOM should be downloaded in TAR.GZ format using the SBOM name", |
| 94 | + async ({ page }) => { |
| 95 | + const sbomName = await page.getByRole("heading").first().innerText(); |
| 96 | + expect(downloadedFilename).toContain(sbomName); |
| 97 | + expect(downloadedFilename.endsWith(".tar.gz")).toBeTruthy(); |
| 98 | + }, |
| 99 | +); |
| 100 | + |
| 101 | +Given( |
| 102 | + "User is on SBOM Explorer page for the CycloneDX SBOM {string}", |
| 103 | + async ({ page }, sbomName: string) => { |
| 104 | + await SbomDetailsPage.build(page, sbomName); |
| 105 | + }, |
| 106 | +); |
| 107 | + |
| 108 | +When('User Clicks on "Download License Report" button', async ({ page }) => { |
| 109 | + const detailsPage = new DetailsPage(page); |
| 110 | + await detailsPage.openActionsMenu(); |
| 111 | + |
| 112 | + downloadedFilename = await clickAndVerifyDownload( |
| 113 | + page, |
| 114 | + async () => |
| 115 | + await detailsPage.page |
| 116 | + .getByRole("menuitem", { name: "Download License Report" }) |
| 117 | + .click(), |
| 118 | + ); |
| 119 | +}); |
| 120 | + |
| 121 | +Given( |
| 122 | + "User has Downloaded the License information for CycloneDX SBOM {string}", |
| 123 | + async ({ page }, sbomName: string) => { |
| 124 | + await SbomDetailsPage.build(page, sbomName); |
| 125 | + downloadedFilePath = await downloadLicenseReport(page); |
| 126 | + }, |
| 127 | +); |
| 128 | + |
| 129 | +When("User extracts the Downloaded license TAR.GZ file", async () => { |
| 130 | + extractionPath = await extractLicenseReport(downloadedFilePath); |
| 131 | +}); |
| 132 | + |
| 133 | +Then( |
| 134 | + "Extracted files should contain two CSVs, one for Package license information and another one for License reference", |
| 135 | + async () => { |
| 136 | + const files = fs.readdirSync(extractionPath); |
| 137 | + const csvFiles = files.filter((file) => file.endsWith(".csv")); |
| 138 | + expect(csvFiles.length).toBe(2); |
| 139 | + }, |
| 140 | +); |
| 141 | + |
| 142 | +Given( |
| 143 | + "User extracted the CycloneDX SBOM {string} license compressed file", |
| 144 | + async ({ page }, sbomName: string) => { |
| 145 | + await SbomDetailsPage.build(page, sbomName); |
| 146 | + downloadedFilePath = await downloadLicenseReport(page); |
| 147 | + extractionPath = await extractLicenseReport(downloadedFilePath); |
| 148 | + }, |
| 149 | +); |
| 150 | + |
| 151 | +When("User Opens the package license information file", async () => { |
| 152 | + packageLicenseFilePath = findCsvWithHeader( |
| 153 | + extractionPath, |
| 154 | + "SBOM name", |
| 155 | + "Package license information file", |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +Then( |
| 160 | + "The file should have the following headers - SBOM name, SBOM id, package name, package group, package version, package purl, package cpe and license", |
| 161 | + async () => { |
| 162 | + const content = fs.readFileSync(packageLicenseFilePath, "utf-8"); |
| 163 | + const headers = content.split("\n")[0].trim(); |
| 164 | + const expectedHeaders = |
| 165 | + '"SBOM name"\t"SBOM id"\t"package name"\t"package group"\t"package version"\t"package purl"\t"package cpe"\t"license"\t"license type"'; |
| 166 | + expect(headers).toBe(expectedHeaders); |
| 167 | + }, |
| 168 | +); |
| 169 | + |
| 170 | +When("User Opens the license reference file", async () => { |
| 171 | + licenseReferenceFilePath = findCsvWithHeader( |
| 172 | + extractionPath, |
| 173 | + "licenseId", |
| 174 | + "License reference file", |
| 175 | + ); |
| 176 | +}); |
| 177 | + |
| 178 | +Then( |
| 179 | + "The file should have the following headers - licenseId, name, extracted text and comment", |
| 180 | + async () => { |
| 181 | + const content = fs.readFileSync(licenseReferenceFilePath, "utf-8"); |
| 182 | + const headers = content.split("\n")[0].trim(); |
| 183 | + const expectedHeaders = '"licenseId"\t"name"\t"extracted text"\t"comment"'; |
| 184 | + expect(headers).toBe(expectedHeaders); |
| 185 | + }, |
| 186 | +); |
| 187 | + |
| 188 | +Given( |
| 189 | + "User is on license reference {string} file", |
| 190 | + async ({ page }, sbomName: string) => { |
| 191 | + await SbomDetailsPage.build(page, sbomName); |
| 192 | + downloadedFilePath = await downloadLicenseReport(page); |
| 193 | + extractionPath = await extractLicenseReport(downloadedFilePath); |
| 194 | + licenseReferenceFilePath = findCsvWithHeader( |
| 195 | + extractionPath, |
| 196 | + "licenseId", |
| 197 | + "License reference file", |
| 198 | + ); |
| 199 | + }, |
| 200 | +); |
| 201 | + |
| 202 | +Then("The License reference CSV should be empty", async () => { |
| 203 | + const content = fs.readFileSync(licenseReferenceFilePath, "utf-8"); |
| 204 | + const lines = content.trim().split("\n"); |
| 205 | + // Only header should be present |
| 206 | + expect(lines.length).toBe(1); |
| 207 | +}); |
| 208 | + |
| 209 | +Given( |
| 210 | + "User is on SBOM license information file for {string}", |
| 211 | + async ({ page }, sbomName: string) => { |
| 212 | + await SbomDetailsPage.build(page, sbomName); |
| 213 | + downloadedFilePath = await downloadLicenseReport(page); |
| 214 | + extractionPath = await extractLicenseReport(downloadedFilePath); |
| 215 | + packageLicenseFilePath = findCsvWithHeader( |
| 216 | + extractionPath, |
| 217 | + "SBOM name", |
| 218 | + "Package license information file", |
| 219 | + ); |
| 220 | + }, |
| 221 | +); |
| 222 | + |
| 223 | +When( |
| 224 | + "User selects the package {string} with Single license id", |
| 225 | + async ({ page: _page }, packageName: string) => { |
| 226 | + selectedPackageRow = findPackageRow(packageLicenseFilePath, packageName); |
| 227 | + }, |
| 228 | +); |
| 229 | + |
| 230 | +When( |
| 231 | + "User selects a package {string} with Single license id with cpe information", |
| 232 | + async ({ page: _page }, packageName: string) => { |
| 233 | + selectedPackageRow = findPackageRow(packageLicenseFilePath, packageName); |
| 234 | + }, |
| 235 | +); |
| 236 | + |
| 237 | +Then( |
| 238 | + "{string} column should match {string} from SBOM", |
| 239 | + async ({ page: _page }, columnName: string, expectedValue: string) => { |
| 240 | + const actual = selectedPackageRow[columnName].replace(/\n/g, " "); |
| 241 | + expect(actual).toBe(expectedValue); |
| 242 | + }, |
| 243 | +); |
| 244 | + |
| 245 | +Then( |
| 246 | + "{string} column should be empty", |
| 247 | + async ({ page: _page }, columnName: string) => { |
| 248 | + expect(selectedPackageRow[columnName]).toBe(""); |
| 249 | + }, |
| 250 | +); |
0 commit comments