From d3866bdb03107fc8d8bde5e99e4d32b209d46254 Mon Sep 17 00:00:00 2001 From: tanvika Date: Sat, 24 Jan 2026 19:53:20 +0530 Subject: [PATCH 1/6] fix(e2e) : restore Cypress setup and add smoke test --- .../frontend/src/test/basic-e2e.test.ts | 279 ++++++--------- .../frontend/src/test/cypress.config.ts | 12 + .../cypress/e2e/1-getting-started/todo.cy.js | 143 ++++++++ .../e2e/2-advanced-examples/actions.cy.js | 321 ++++++++++++++++++ .../e2e/2-advanced-examples/aliasing.cy.js | 39 +++ .../e2e/2-advanced-examples/assertions.cy.js | 176 ++++++++++ .../e2e/2-advanced-examples/connectors.cy.js | 98 ++++++ .../e2e/2-advanced-examples/cookies.cy.js | 118 +++++++ .../e2e/2-advanced-examples/cypress_api.cy.js | 184 ++++++++++ .../e2e/2-advanced-examples/files.cy.js | 85 +++++ .../e2e/2-advanced-examples/location.cy.js | 32 ++ .../e2e/2-advanced-examples/misc.cy.js | 98 ++++++ .../e2e/2-advanced-examples/navigation.cy.js | 55 +++ .../network_requests.cy.js | 163 +++++++++ .../e2e/2-advanced-examples/querying.cy.js | 114 +++++++ .../spies_stubs_clocks.cy.js | 204 +++++++++++ .../e2e/2-advanced-examples/storage.cy.js | 117 +++++++ .../e2e/2-advanced-examples/traversal.cy.js | 121 +++++++ .../e2e/2-advanced-examples/utilities.cy.js | 107 ++++++ .../e2e/2-advanced-examples/viewport.cy.js | 58 ++++ .../e2e/2-advanced-examples/waiting.cy.js | 30 ++ .../e2e/2-advanced-examples/window.cy.js | 22 ++ .../src/test/cypress/e2e/e2e-smoke.cy.ts | 6 + .../src/test/cypress/fixtures/example.json | 5 + .../src/test/cypress/support/commands.js | 25 ++ .../frontend/src/test/cypress/support/e2e.js | 17 + 26 files changed, 2450 insertions(+), 179 deletions(-) create mode 100644 application/frontend/src/test/cypress.config.ts create mode 100644 application/frontend/src/test/cypress/e2e/1-getting-started/todo.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/actions.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/aliasing.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/assertions.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/connectors.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/cookies.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/cypress_api.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/files.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/location.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/misc.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/navigation.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/network_requests.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/querying.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/spies_stubs_clocks.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/storage.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/traversal.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/utilities.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/viewport.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/waiting.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/2-advanced-examples/window.cy.js create mode 100644 application/frontend/src/test/cypress/e2e/e2e-smoke.cy.ts create mode 100644 application/frontend/src/test/cypress/fixtures/example.json create mode 100644 application/frontend/src/test/cypress/support/commands.js create mode 100644 application/frontend/src/test/cypress/support/e2e.js diff --git a/application/frontend/src/test/basic-e2e.test.ts b/application/frontend/src/test/basic-e2e.test.ts index 7f5f8dd77..e4721dbd3 100644 --- a/application/frontend/src/test/basic-e2e.test.ts +++ b/application/frontend/src/test/basic-e2e.test.ts @@ -1,179 +1,100 @@ -// import puppeteer from 'puppeteer'; -// require('regenerator-runtime/runtime'); - -// describe('App.js', () => { -// var browser; -// var page; -// const debug = { -// // headless: false, -// // slowMo: 150, -// args: ['--no-sandbox', '--disable-setuid-sandbox'], // needed for docker, we trust the content of opencre.org -// }; -// const config = {}; -// beforeAll(async () => { -// jest.setTimeout(1000000); -// browser = await puppeteer.launch(debug); -// page = await browser.newPage(); -// page.setDefaultTimeout(15000); -// }); - -// it('contains the welcome text', { url: 'http://localhost:5000' }, async () => { -// await page.goto('http://localhost:5000'); -// await page.waitForSelector('#SearchBar'); -// const text = await page.$eval('#SearchBar', (e) => e.textContent); -// expect(text).toContain('Search'); -// }); - -// it('can search for random strs', { url: 'http://127.0.0.1:5000' }, async () => { -// await page.goto('http://127.0.0.1:5000'); -// await page.waitForSelector('#SearchBar'); -// await page.type('#SearchBar > div > input', 'asdf'); -// await page.click('#SearchBar > div > button'); -// await page.waitForSelector('.content'); -// const text = await page.$eval('.content', (e) => e.textContent); -// expect(text).toContain('No results match your search term'); -// }); - -// it( -// 'can search for cryptography using the free text method and it returns both Nodes and CRES', -// { url: 'http://127.0.0.1:5000' }, -// async () => { -// await page.goto('http://127.0.0.1:5000'); -// await page.waitForSelector('#SearchBar'); -// await page.type('#SearchBar > div > input', 'crypto'); -// await page.click('#SearchBar > div > button'); -// await page.waitForSelector('.content'); -// const text = await page.$eval('.content', (e) => e.textContent); -// expect(text).not.toContain('No results match your search term'); - -// await page.waitForSelector('.standard-page__links-container'); -// const results = await page.$$('.standard-page__links-container'); -// expect(results.length).toBeGreaterThan(1); - -// const cres = await page.$$('.cre-page >div>div>.standard-page__links-container'); -// expect(cres.length).toBeGreaterThan(1); - -// const docs = await page.$$('.cre-page >div>div:nth-child(2)>.standard-page__links-container'); -// expect(docs.length).toBeGreaterThan(1); -// } -// ); - -// it( -// 'can search for a standard by name, section and the standard page works as expected', -// { url: 'http://127.0.0.1:5000' }, -// async () => { -// await page.goto('http://127.0.0.1:5000/node/standard/ASVS'); -// await page.waitForSelector('.content'); -// const text = await page.$$('.content', (e) => e.textContent); -// expect(text).not.toContain('No results match your search term'); - -// await page.waitForSelector('.standard-page__links-container'); - -// // title match -// const page_title = await page.$eval('.standard-page__heading', (e) => e.textContent); -// expect(page_title).toContain('ASVS'); - -// // results -// const results = await page.$$('.standard-page__links-container'); -// expect(results.length).toBeGreaterThan(1); - -// // pagination -// const original_content = await page.content(); -// await page.click('a[type="pageItem"][value="2"]'); -// await page.waitForSelector('.content'); -// expect(await page.content()).not.toEqual(original_content); - -// // link to section -// await page.click('.standard-page__links-container>.title>a'); -// await page.waitForSelector('.content'); -// const url = await page.url(); -// expect(url).toContain('section'); -// const section = await page.$eval('.standard-page > span:nth-child(2)', (e) => e.textContent); -// expect(section).toContain('Reference:'); - -// // show reference -// const hrefs = await page.evaluate(() => -// Array.from(document.querySelectorAll('.section-page > a[href]'), (a) => a.getAttribute('href')) -// ); -// expect(hrefs[0]).toContain('https://'); - -// // link to at least one cre -// const cre_links = await page.$$('.cre-page__links-container > .title > a:nth-child(1)'); -// expect(cre_links.length).toBeGreaterThan(0); -// const cre_links_hrefs = await page.evaluate(() => -// Array.from(document.querySelectorAll('.cre-page__links-container > .title > a:nth-child(1)'), (a) => -// a.getAttribute('href') -// ) -// ); -// expect(cre_links_hrefs[0]).toContain('/cre/'); -// } -// ); - -// it('can search for a cre', { url: 'http://127.0.0.1:5000' }, async () => { -// await page.goto('http://127.0.0.1:5000'); -// await page.waitForSelector('#SearchBar'); -// await page.type('#SearchBar > div > input', '558-807'); -// await page.click('#SearchBar > div > button'); -// await page.waitForSelector('.content'); -// const text = await page.$$('.content', (e) => e.textContent); -// expect(text).not.toContain('No results match your search term'); - -// await page.waitForSelector('.standard-page__links-container'); - -// // title match -// const entry_title = await page.$eval('div.title.document-node', (e) => e.textContent); -// expect(entry_title).toContain('Mutually authenticate application and credential service provider'); - -// // results -// const results = await page.$$('.standard-page__links-container'); -// expect(results.length).toBe(1); - -// // // nesting -// await page.click('.dropdown'); -// const selector = -// '.standard-page__links-container>.document-node>.document-node__link-type-container:nth-child(2)'; -// await page.waitForSelector(selector); - -// const nested = await page.$$( -// '.standard-page__links-container>.document-node>.document-node__link-type-container>div>.accordion' -// ); -// expect(nested.length).toBeGreaterThan(1); -// }); - -// it('can filter', { url: 'http://127.0.0.1:5000' }, async () => { -// await page.goto('http://127.0.0.1:5000/cre/558-807?applyFilters=true&filters=asvs'); -// await page.waitForSelector('.cre-page__links-container'); -// // Get inner text -// const innerText = await page.evaluate( -// () => (document.querySelector('.cre-page__links-container') as HTMLElement)?.innerText -// ); -// expect(innerText).toContain('ASVS'); -// expect(innerText).toContain('CRE'); -// expect(innerText).not.toContain('NIST'); - -// // ensure case insensitive filtering -// await page.goto('http://127.0.0.1:5000/cre/558-807?applyFilters=true&filters=ASVS'); -// await page.waitForSelector('.cre-page__links-container'); -// const intxt = await page.evaluate( -// () => (document.querySelector('.cre-page__links-container') as HTMLElement)?.innerText -// ); -// expect(intxt).toContain('ASVS'); -// expect(intxt).toContain('CRE'); -// expect(intxt).not.toContain('NIST'); - -// const clearFilters = await page.evaluate( -// () => (document.querySelector('#clearFilterButton') as HTMLElement)?.innerText -// ); -// expect(clearFilters).toContain('Clear Filters'); -// }); - -// it('can smartlink', { url: 'http://127.0.0.1:5000' }, async () => { -// const response = await page.goto('http://127.0.0.1:5000/smartlink/standard/CWE/1002'); -// expect(response.url()).toBe('http://127.0.0.1:5000/node/standard/CWE/sectionid/1002'); - -// const redirectResponse = await page.goto('http://127.0.0.1:5000/smartlink/standard/CWE/404'); -// expect(redirectResponse.url()).toBe('https://cwe.mitre.org/data/definitions/404.html'); -// }); - -// afterAll(async () => await browser.close()); -// }); +/// +describe('OpenCRE App E2E Tests', () => { + + + beforeEach(() => { + // Visit home page before each test + cy.visit('/'); + }); + + it('contains the welcome text', () => { + cy.get('#SearchBar').should('exist').and('contain.text', 'Search'); + }); + + it('can search for random strings', () => { + cy.get('#SearchBar > div > input').type('asdf'); + cy.get('#SearchBar > div > button').click(); + cy.get('.content').should('contain.text', 'No results match your search term'); + }); + + it('can search for "crypto" and return Nodes and CRES', () => { + cy.get('#SearchBar > div > input').type('crypto'); + cy.get('#SearchBar > div > button').click(); + + cy.get('.content').should('not.contain.text', 'No results match your search term'); + + cy.get('.standard-page__links-container').should('have.length.greaterThan', 1); + cy.get('.cre-page .standard-page__links-container').should('have.length.greaterThan', 1); + cy.get('.cre-page div:nth-child(2) .standard-page__links-container').should('have.length.greaterThan', 1); + }); + + it('can search for a standard (ASVS) and verify page elements', () => { + cy.visit(`/node/standard/ASVS`); + + cy.get('.content').should('exist').and('not.contain.text', 'No results match your search term'); + cy.get('.standard-page__heading').should('contain.text', 'ASVS'); + cy.get('.standard-page__links-container').should('have.length.greaterThan', 1); + + // Pagination + cy.get('a[type="pageItem"][value="2"]').click(); + cy.get('.content').should('exist'); + + // Click first section link + cy.get('.standard-page__links-container > .title > a').first().click(); + cy.get('.content').should('exist'); + cy.url().should('contain', 'section'); + cy.get('.standard-page > span:nth-child(2)').should('contain.text', 'Reference:'); + + // Check reference links + cy.get('.section-page a[href]').first().should('have.attr', 'href').and('contain', 'https://'); + + // Verify at least one CRE link + cy.get('.cre-page__links-container > .title > a').first().should('exist'); + }); + + it('can search for a CRE (558-807) and verify content', () => { + cy.get('#SearchBar > div > input').type('558-807'); + cy.get('#SearchBar > div > button').click(); + + cy.get('.content').should('not.contain.text', 'No results match your search term'); + cy.get('div.title.document-node').should( + 'contain.text', + 'Mutually authenticate application and credential service provider' + ); + + cy.get('.standard-page__links-container').should('have.length', 1); + + // Check nested accordions + cy.get('.dropdown').click(); + cy.get( + '.standard-page__links-container > .document-node > .document-node__link-type-container > div > .accordion' + ).should('have.length.greaterThan', 1); + }); + + it('can filter CRE results', () => { + cy.visit(`/cre/558-807?applyFilters=true&filters=asvs`); + cy.get('.cre-page__links-container') + .should('contain.text', 'ASVS') + .and('contain.text', 'CRE') + .and('not.contain.text', 'NIST'); + + // Case-insensitive filter check + cy.visit(`/cre/558-807?applyFilters=true&filters=ASVS`); + cy.get('.cre-page__links-container') + .should('contain.text', 'ASVS') + .and('contain.text', 'CRE') + .and('not.contain.text', 'NIST'); + + cy.get('#clearFilterButton').should('contain.text', 'Clear Filters'); + }); + + it('can smartlink', () => { + cy.request(`/smartlink/standard/CWE/1002`).then((res) => { + expect(res.redirectedToUrl).to.include('/node/standard/CWE/sectionid/1002'); + }); + + cy.request(`smartlink/standard/CWE/404`).then((res) => { + expect(res.redirectedToUrl).to.eq('https://cwe.mitre.org/data/definitions/404.html'); + }); + }); +}); \ No newline at end of file diff --git a/application/frontend/src/test/cypress.config.ts b/application/frontend/src/test/cypress.config.ts new file mode 100644 index 000000000..7d59fb4a5 --- /dev/null +++ b/application/frontend/src/test/cypress.config.ts @@ -0,0 +1,12 @@ +import type { Cypress } from "cypress"; + +const config: Cypress.ConfigOptions = { + e2e: { + baseUrl: "http://localhost:9001", + setupNodeEvents(on, config) { + // node events + }, + }, +}; + +export default config; \ No newline at end of file diff --git a/application/frontend/src/test/cypress/e2e/1-getting-started/todo.cy.js b/application/frontend/src/test/cypress/e2e/1-getting-started/todo.cy.js new file mode 100644 index 000000000..4768ff923 --- /dev/null +++ b/application/frontend/src/test/cypress/e2e/1-getting-started/todo.cy.js @@ -0,0 +1,143 @@ +/// + +// Welcome to Cypress! +// +// This spec file contains a variety of sample tests +// for a todo list app that are designed to demonstrate +// the power of writing tests in Cypress. +// +// To learn more about how Cypress works and +// what makes it such an awesome testing tool, +// please read our getting started guide: +// https://on.cypress.io/introduction-to-cypress + +describe('example to-do app', () => { + beforeEach(() => { + // Cypress starts out with a blank slate for each test + // so we must tell it to visit our website with the `cy.visit()` command. + // Since we want to visit the same URL at the start of all our tests, + // we include it in our beforeEach function so that it runs before each test + cy.visit('https://example.cypress.io/todo') + }) + + it('displays two todo items by default', () => { + // We use the `cy.get()` command to get all elements that match the selector. + // Then, we use `should` to assert that there are two matched items, + // which are the two default items. + cy.get('.todo-list li').should('have.length', 2) + + // We can go even further and check that the default todos each contain + // the correct text. We use the `first` and `last` functions + // to get just the first and last matched elements individually, + // and then perform an assertion with `should`. + cy.get('.todo-list li').first().should('have.text', 'Pay electric bill') + cy.get('.todo-list li').last().should('have.text', 'Walk the dog') + }) + + it('can add new todo items', () => { + // We'll store our item text in a variable so we can reuse it + const newItem = 'Feed the cat' + + // Let's get the input element and use the `type` command to + // input our new list item. After typing the content of our item, + // we need to type the enter key as well in order to submit the input. + // This input has a data-test attribute so we'll use that to select the + // element in accordance with best practices: + // https://on.cypress.io/selecting-elements + cy.get('[data-test=new-todo]').type(`${newItem}{enter}`) + + // Now that we've typed our new item, let's check that it actually was added to the list. + // Since it's the newest item, it should exist as the last element in the list. + // In addition, with the two default items, we should have a total of 3 elements in the list. + // Since assertions yield the element that was asserted on, + // we can chain both of these assertions together into a single statement. + cy.get('.todo-list li') + .should('have.length', 3) + .last() + .should('have.text', newItem) + }) + + it('can check off an item as completed', () => { + // In addition to using the `get` command to get an element by selector, + // we can also use the `contains` command to get an element by its contents. + // However, this will yield the