From 096baf9a3c86e0d3075625f49a683c7d1016828e Mon Sep 17 00:00:00 2001 From: Florian THIERRY Date: Mon, 31 Mar 2025 23:03:24 +0200 Subject: [PATCH] Add test example to scrap a web page. --- cypress.config.js | 9 ++++ cypress/e2e/sandbox.cy.js | 94 +++++++++++++++++++++++++++++++++++ cypress/fixtures/example.json | 5 ++ cypress/support/commands.js | 25 ++++++++++ cypress/support/e2e.js | 17 +++++++ package.json | 3 +- 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 cypress.config.js create mode 100644 cypress/e2e/sandbox.cy.js create mode 100644 cypress/fixtures/example.json create mode 100644 cypress/support/commands.js create mode 100644 cypress/support/e2e.js diff --git a/cypress.config.js b/cypress.config.js new file mode 100644 index 0000000..97f47c4 --- /dev/null +++ b/cypress.config.js @@ -0,0 +1,9 @@ +const { defineConfig } = require("cypress"); + +module.exports = defineConfig({ + e2e: { + setupNodeEvents(on, config) { + // implement node event listeners here + }, + }, +}); diff --git a/cypress/e2e/sandbox.cy.js b/cypress/e2e/sandbox.cy.js new file mode 100644 index 0000000..5278c6b --- /dev/null +++ b/cypress/e2e/sandbox.cy.js @@ -0,0 +1,94 @@ +// describe('template spec', () => { +// it('passes', () => { +// cy.visit('https://example.cypress.io') +// }) +// }) + +/* +function sum(a, b) { + return a + b; +} + +function minus(a, b) { + return a - b; +} + +const resultatDeUnEtDeux = sum(1, 2); + +const sum2 = (a, b) => { + console.log(a + b); +}; + +Un lambda s'écrit de la manière suivante : +(...) => { + ... +} + +sum2(10, 20); + +const x = 3; +const y = 2; + +const monOperationArithmetique = sum; + +const result = monOperationArithmetique(x, y); + +console.log(result); + + +const testName = 'mon premier test'; +const testCode = () => { + console.log('Ça fonctionne !'); +} +it(testName, testCode); +*/ + +/* +class MyService(self): + def functionA(self, x): + # ... + pass + + def functionB(self): + pass + +describe('MyService', () => { + describe('functionA', () => { + it('should return true when user age is under 18', () => { + //... + }); + + it('should return false when user age is lower or equals 18', () => { + + }); + }); +}); +*/ + +// describe('Le site ouest-france-immo', () => { +// it('doit se charger dans cypress', () => { +// cy.visit('https://www.ouestfrance-immo.com/louer/appartement--2-pieces/rennes-beaulieu-35-35000/?pieces'); + + +// cy.get('.header') +// .should('exist'); +// }); + +// }); + +describe('Le site codiki.org', () => { + it('doit se charger dans cypress', () => { + cy.visit('https://codiki.org'); + + cy.get('.publication') + .each(($div) => { + cy.wrap($div).find('div').find('h1').invoke('text').as('h1Text'); + + cy.get('@h1Text').then((text) => { + //cy.log(text); + console.log(text); + }); + }); + }); +}); + diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000..02e4254 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000..66ea16e --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,25 @@ +// *********************************************** +// This example commands.js shows you how to +// create various custom commands and overwrite +// existing commands. +// +// For more comprehensive examples of custom +// commands please read more here: +// https://on.cypress.io/custom-commands +// *********************************************** +// +// +// -- This is a parent command -- +// Cypress.Commands.add('login', (email, password) => { ... }) +// +// +// -- This is a child command -- +// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) +// +// +// -- This is a dual command -- +// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) +// +// +// -- This will overwrite an existing command -- +// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) \ No newline at end of file diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js new file mode 100644 index 0000000..3eaffff --- /dev/null +++ b/cypress/support/e2e.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example support/e2e.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import './commands' \ No newline at end of file diff --git a/package.json b/package.json index 37246ec..0d34341 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "version": "1.0.0", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "cypress": "npx cypress open" }, "author": "", "license": "ISC",