From 33e3170ef7ed79668b36e8b09fa0a4718fb3a117 Mon Sep 17 00:00:00 2001 From: aasim Date: Sat, 13 Apr 2024 14:16:02 -0400 Subject: [PATCH 1/5] updated config to use cypress/integration as specs folder --- cypress.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cypress.config.js b/cypress.config.js index 97f47c4..06ea1a1 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -2,6 +2,8 @@ const { defineConfig } = require("cypress"); module.exports = defineConfig({ e2e: { + supportFile: 'cypress/support/commands.js', + specPattern: 'cypress/integration/**/*.spec.js', setupNodeEvents(on, config) { // implement node event listeners here }, From f46bbafcef813f3e36abb5fa443f83ea1c6de6cf Mon Sep 17 00:00:00 2001 From: aasim Date: Sat, 13 Apr 2024 14:16:55 -0400 Subject: [PATCH 2/5] created custom command to block ads that were interfering with tests --- cypress/support/commands.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 66ea16e..ed07f19 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -22,4 +22,24 @@ // // // -- This will overwrite an existing command -- -// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) \ No newline at end of file +// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) + +// visitWithAdBlocker: Visits a page and blocks ads +Cypress.Commands.add('visitWithAdBlocker', (url) => { + // Setup intercepts + cy.intercept('https://pagead2.googlesyndication.com/**', {statusCode: 200, body: ''}).as('blockAds'); + cy.intercept('https://www.googletagservices.com/tag/js/gpt.js', {statusCode: 200, body: ''}).as('blockGpt'); + cy.intercept('https://www.googletagmanager.com/gtm.js?id=GTM-MX8DD4S', {statusCode: 200, body: ''}).as('blockGtm'); + cy.intercept('https://cdn.ad.plus/player/adplus.js', {statusCode: 200, body: ''}).as('blockAdPlus'); + cy.intercept('**/*.gif', {statusCode: 200, body: ''}).as('blockGif'); + cy.intercept('**/usermatch.gif**', {statusCode: 200, body: ''}).as('blockUserMatch'); + cy.intercept('**/merge**', {statusCode: 200, body: ''}).as('blockMerge'); + cy.intercept('**/*.js', (req) => { + if (req.url.includes('google') || req.url.includes('adsbygoogle')) { + req.destroy(); + } + }).as('blockScripts'); + + // Visit the page + cy.visit(url); +}); From e3f6212dae4cfcc1952901f3ad10bd016ca9fb70 Mon Sep 17 00:00:00 2001 From: aasim Date: Sat, 13 Apr 2024 14:17:35 -0400 Subject: [PATCH 3/5] spec file to test all alerts --- cypress/integration/alerts.spec.js | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cypress/integration/alerts.spec.js diff --git a/cypress/integration/alerts.spec.js b/cypress/integration/alerts.spec.js new file mode 100644 index 0000000..f534539 --- /dev/null +++ b/cypress/integration/alerts.spec.js @@ -0,0 +1,56 @@ +describe('Alerts Handling on DEMOQA', () => { + beforeEach(() => { + cy.visitWithAdBlocker('https://demoqa.com/alerts'); + }); + + it('tests the [Alert] button which shows an alert box', () => { + cy.on('window:alert', (text) => { + expect(text).to.contains('You clicked a button'); + }); + cy.get('#alertButton').click(); + }); + + it('ensures the [Timer Alert] button triggers after exactly 5 seconds', () => { + cy.clock(); + + cy.on('window:alert', (text) => { + expect(text).to.equal('This alert appeared after 5 seconds'); + }); + + cy.get('#timerAlertButton').click(); + + cy.get('#statusMessage').should('not.exist'); + + cy.tick(5000); + }); + + + it('accepts a confirm alert and verifies actions taken on confirmation', () => { + cy.on('window:confirm', (str) => { + expect(str).to.eq('Do you confirm action?'); + return true; + }); + + cy.get('#confirmButton').click(); + cy.get('#confirmResult').should('contain', 'You selected Ok'); + }); + + it('cancels a confirm alert and verifies actions taken on cancellation', () => { + cy.on('window:confirm', (str) => { + expect(str).to.eq('Do you confirm action?'); + return false; + }); + + cy.get('#confirmButton').click(); + cy.get('#confirmResult').should('contain', 'You selected Cancel'); + }); + + it('accepts a prompt alert and verifies actions taken on confirmation', () => { + cy.window().then((win) => { + cy.stub(win, 'prompt').returns('Cypress Tester'); + }); + + cy.get('#promtButton').click(); + cy.get('#promptResult').should('contain', 'You entered Cypress Tester'); + }); +}); From 0a39ac19e2acb8c5112b5d85c5026c0683dc0dd8 Mon Sep 17 00:00:00 2001 From: aasim Date: Sat, 13 Apr 2024 15:09:32 -0400 Subject: [PATCH 4/5] added function to enter frame by selector --- cypress/support/commands.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ed07f19..45b0ffa 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -43,3 +43,9 @@ Cypress.Commands.add('visitWithAdBlocker', (url) => { // Visit the page cy.visit(url); }); + +// enterFrame: Enters a frame by its selector +Cypress.Commands.add('enterFrame', (selector) => { + return cy.get(selector).its('0.contentDocument.body').should('not.be.undefined') + .then(cy.wrap); +}); \ No newline at end of file From 692fb8e7f69d6c283e79635c5b22152e8d91a5a9 Mon Sep 17 00:00:00 2001 From: aasim Date: Sat, 13 Apr 2024 15:09:54 -0400 Subject: [PATCH 5/5] added tests for frames --- cypress/integration/frames.spec.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 cypress/integration/frames.spec.js diff --git a/cypress/integration/frames.spec.js b/cypress/integration/frames.spec.js new file mode 100644 index 0000000..1a6d43a --- /dev/null +++ b/cypress/integration/frames.spec.js @@ -0,0 +1,29 @@ +describe('Frames Handling on DEMOQA', () => { + beforeEach(() => { + cy.visitWithAdBlocker('https://demoqa.com/frames'); + }); + + // Test to check content and dimensions of frame1 + it('verifies content and dimensions within frame1', () => { + cy.get('#frame1') + .should('have.attr', 'width', '500px') + .and('have.attr', 'height', '350px'); + + cy.enterFrame('#frame1').within(() => { + cy.get('h1#sampleHeading').should('exist') + .and('have.text', 'This is a sample page'); + }); + }); + + // Test to check content and dimensions of frame2 + it('verifies content and dimensions within frame2', () => { + cy.get('#frame2') + .should('have.attr', 'width', '100px') + .and('have.attr', 'height', '100px'); + + cy.enterFrame('#frame2').within(() => { + cy.get('h1#sampleHeading').should('exist') + .and('have.text', 'This is a sample page'); + }); + }); +});