-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1fa77ce
Showing
31 changed files
with
5,366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// .eslintrc.json | ||
|
||
{ | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"es2021": true, | ||
"node": true, | ||
"jest": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 12 | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"never" | ||
], | ||
"no-trailing-spaces": [ | ||
"error" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Artifacts | ||
on: push | ||
jobs: | ||
cypress-run: | ||
runs-on: ubuntu-22.04 | ||
name: Artifacts | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: cypress-io/github-action@v6 | ||
# after the test run completes store videos and any screenshots | ||
- uses: actions/upload-artifact@v4 | ||
# add the line below to store screenshots only on failures | ||
if: failure() | ||
with: | ||
name: cypress-screenshots | ||
path: cypress/screenshots | ||
if-no-files-found: ignore # 'warn' or 'error' are also available, defaults to `warn` | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: cypress-videos | ||
path: cypress/videos | ||
if-no-files-found: ignore # 'warn' or 'error' are also available, defaults to `warn` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
cypress.env.json | ||
cypress/downloads/ | ||
cypress/screenshots/ | ||
cypress/videos/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Testando aplicação ServeRest com Cypress | ||
|
||
Olá, seja muito bem-vindo(a)! | ||
|
||
Fiz este projeto de exemplo para demonstração de testes automatizados de GUI e API escritos com [Cypress](https://cypress.io) utilizando a aplicação [ServeRest](https://front.serverest.dev) que foi desenvolvida e é mantida por [Paulo Gonçalves](https://github.com/PauloGoncalvesBH). | ||
|
||
|
||
## Pré-requisitos | ||
|
||
Para baixar e rodar este projeto, você precisará das seguintes tecnologias instaladas em seu computador: | ||
|
||
- [git](https://git-scm.com/downloads) (usei a versão `2.47.0` enquanto escrevia este documento) | ||
- [Node.js](https://nodejs.org/en/) (usei a versão `v20.17.0` enquanto escrevia este documento) | ||
- npm (usei a versão `10.8.2` enquanto escrevia este documento) | ||
|
||
**Obs:** Ao instalar o Node.js, o npm é instalado automaticamente. | ||
|
||
## Instalação | ||
|
||
Após clonar o projeto, execute o comando `npm install` (ou `npm i` para a versão curta) para instalar as dependências de desenvolvimento. | ||
|
||
## Consumindo o ServeRest | ||
|
||
O ServeRest está disponível de forma [online](https://serverest.dev), no [npm](https://www.npmjs.com/package/serverest) e no [docker](https://hub.docker.com/r/paulogoncalvesbh/serverest/). | ||
|
||
Todas essas opções possuem as mesmas rotas, regras, dados pré-cadastrados e documentação. Escolha a melhor opção para você. | ||
|
||
No ambiente online os dados cadastrados são removidos diariamente, enquanto que no local basta reiniciar o ServeRest. | ||
|
||
> Para facilitar a execução do projeto por terceiros, ele foi configurado por padrão para utilizar a aplicação de forma online. Porém, pode-se utilizar a opção de ambiente local caso precise que os dados não sejam alterados por outro usuário. | ||
Acesse **<https://serverest.dev>** para visualizar a documentação e as rotas disponíveis. | ||
|
||
## Executando os testes | ||
|
||
Neste projeto, você pode rodar os testes em modo interativo ou modo [_headless_](https://docs.cypress.io/guides/guides/command-line). | ||
|
||
### Modo _headless_ | ||
|
||
Execute o comando `npm test` (ou `npm t` para a versão curta) para rodar a todos os testes em modo [_headless_](https://docs.cypress.io/guides/guides/command-line). | ||
|
||
|
||
### Modo interativo | ||
|
||
Execute o comando `npm run cy:open` para abrir a Cypress App e rodar os testes. | ||
|
||
### Análise estática | ||
|
||
Para análise estática de código estou utilizando a biblioteca [_eslint-plugin-cypress_](https://www.npmjs.com/package/eslint-plugin-cypress). | ||
Para realizar a análise estática de código, basta rodar o comando `npm run lint` ou execute diretamente o comando `npm run lint:fix` para realizar a análise e corrigir automaticamente os problemas encontrados. | ||
___ | ||
|
||
Feito com ☕ e ❤️ por [Renan](https://github.com/RenanCardoso). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const { defineConfig } = require('cypress') | ||
|
||
module.exports = defineConfig({ | ||
e2e: { | ||
baseUrl: 'https://front.serverest.dev', // URL base da aplicação em teste | ||
apiUrl: 'https://serverest.dev', // URL base da API da aplicação em teste | ||
env: { | ||
hideCredentials: true, // Como boa prática, é usado este variável de teste para que o token de acesso (o qual é um dados sensível) fique protegido | ||
requestMode: true, // feedback visual ocorra mesmo que estejamos utilizando o comando cy.request() | ||
snapshotOnly: true // para que nos testes de GUI, também tenhamos feedback visual quando chamadas de API estiverem rodando | ||
}, | ||
experimentalRunAllSpecs: true // para rodar todos os testes em modo interativo | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"userEmail": "[email protected]", | ||
"userPassword": "teste" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": [ | ||
"plugin:cypress/recommended" | ||
], | ||
"rules": { | ||
"cypress/no-force": "error", | ||
"cypress/assertion-before-screenshot": "error", | ||
"cypress/require-data-selectors": "warning", | ||
"cypress/no-pause": "error" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
describe('Login', () => { | ||
it('realizar login com sucesso via API', () => { | ||
const userEmail = Cypress.env('userEmail') | ||
const userPassword = Cypress.env('userPassword') | ||
|
||
cy.apiLogin(userEmail, userPassword).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Deletar produto via API', () => { | ||
let accessToken = '' | ||
|
||
const product = { | ||
id: '', | ||
name: `product ${faker.food.ingredient()}`, | ||
price: faker.number.int({ min: 1, max: 1000 }), | ||
description: faker.lorem.lines(1), | ||
quantity: faker.number.int({ min: 1, max: 1000 }) | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
cy.apiRegisterProduct(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(201) | ||
product.id = response.body._id | ||
}) | ||
}) | ||
}) | ||
|
||
it('deletar produto com sucesso', () => { | ||
cy.apiDeleteProductById(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Editar produto via API', () => { | ||
let accessToken = '' | ||
|
||
const product = { | ||
id: '', | ||
name: `product ${faker.food.ingredient()}`, | ||
price: faker.number.int({ min: 1, max: 1000 }), | ||
description: faker.lorem.lines(1), | ||
quantity: faker.number.int({ min: 1, max: 1000 }) | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
cy.apiRegisterProduct(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(201) | ||
product.id = response.body._id | ||
}) | ||
}) | ||
}) | ||
|
||
it('editar produto com sucesso', () => { | ||
const newProduct = { | ||
id: '', | ||
name: `product ${faker.food.ingredient()}`, | ||
price: faker.number.int({ min: 1, max: 1000 }), | ||
description: faker.lorem.lines(1), | ||
quantity: faker.number.int({ min: 1, max: 1000 }) | ||
} | ||
|
||
cy.apiEditProductById(accessToken, product, newProduct).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
describe('Listar produtos via API', () => { | ||
let accessToken = '' | ||
const product = { | ||
|
||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
}) | ||
}) | ||
|
||
it('listar com sucesso', () => { | ||
cy.apiListProducts(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Cadastrar produto via API', () => { | ||
let accessToken = '' | ||
|
||
const product = { | ||
name: `product ${faker.food.ingredient()}`, | ||
price: faker.number.int({ min: 1, max: 1000 }), | ||
description: faker.lorem.lines(1), | ||
quantity: faker.number.int({ min: 1, max: 1000 }) | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
}) | ||
}) | ||
|
||
it('cadastrar com sucesso', () => { | ||
cy.apiRegisterProduct(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(201) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Buscar produto por ID via API', () => { | ||
let accessToken = '' | ||
|
||
const product = { | ||
id: '', | ||
name: `product ${faker.food.ingredient()}`, | ||
price: faker.number.int({ min: 1, max: 1000 }), | ||
description: faker.lorem.lines(1), | ||
quantity: faker.number.int({ min: 1, max: 1000 }) | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
cy.apiRegisterProduct(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(201) | ||
product.id = response.body._id | ||
}) | ||
}) | ||
}) | ||
|
||
it('buscar produto com sucesso', () => { | ||
cy.apiSearchProductById(accessToken, product).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Deletar usuário via API', () => { | ||
let accessToken = '' | ||
|
||
const user = { | ||
id: '', | ||
name: faker.person.fullName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: faker.internet.password({ length: 20 }), | ||
administrator: null | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
cy.apiRegisterUser(accessToken, user).then((response) => { | ||
expect(response.status).to.equal(201) | ||
user.id = response.body._id | ||
}) | ||
}) | ||
}) | ||
|
||
context('usuário administrador', () => { | ||
user.administrator = 'true' // Configura o administrador para true | ||
it('deletar com sucesso', () => { | ||
cy.apiDeleteUserById(accessToken, user).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
describe('Editar usuário via API', () => { | ||
let accessToken = '' | ||
|
||
const user = { | ||
name: faker.person.fullName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: faker.internet.password({ length: 20 }), | ||
administrator: 'true' | ||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
}) | ||
}) | ||
|
||
context('usuário administrador', () => { | ||
const newUser = { | ||
id: '', | ||
name: faker.person.fullName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: faker.internet.password({ length: 20 }), | ||
administrator: 'true' | ||
} | ||
it('editar com sucesso', () => { | ||
cy.apiEditUserById(accessToken, user, newUser).then((response) => { | ||
expect(response.status).to.equal(201) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
describe('Listar usuários via API', () => { | ||
let accessToken = '' | ||
const user = { | ||
|
||
} | ||
|
||
before(() => { | ||
cy.apiLogin().then((response) => { | ||
accessToken = response.body.authorization | ||
}) | ||
}) | ||
|
||
it('listar com sucesso', () => { | ||
cy.apiListUsers(accessToken, user).then((response) => { | ||
expect(response.status).to.equal(200) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.