Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 4: Topic 2 #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
56 changes: 56 additions & 0 deletions cypress/integration/alerts.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
29 changes: 29 additions & 0 deletions cypress/integration/frames.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
28 changes: 27 additions & 1 deletion cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,30 @@
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
// 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);
});

// 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);
});