Skip to content

feat: Documentação e testes com Cypress #41

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

Open
wants to merge 1 commit 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
57 changes: 30 additions & 27 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# contract testing
logs/
pacts/
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# contract testing
logs/
pacts/

# log video cypress
cypress-ui.log
2 changes: 1 addition & 1 deletion CNAME
Original file line number Diff line number Diff line change
@@ -1 +1 @@
serverest.netlify.app
serverest.netlify.app
9 changes: 9 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
70 changes: 70 additions & 0 deletions cypress/e2e/login.spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe('login spec', () => {
const baseUrl = 'https://front.serverest.dev/login'

// Cenário 1: Login válido
it('Deve logar com sucesso', () => {
cy.visit(baseUrl) // Navegar para a página de login
cy.get('input[name=email]').type('ateste@qa.com') // Digitar email válido
cy.get('input[name=password]').type('1234') // Digitar senha válida
cy.get('button[type=submit]').click() // Clicar no botão de login

// Validar redirecionamento para a URL esperada
cy.url().should('include', '/admin/home') // Verifica se a URL contém '/admin/home'
})

// Cenário 2: Login com e-mail não registrado
it('Exibe erro para email não registrado', () => {
cy.visit(baseUrl)
cy.get('input[name=email]').type('naoexiste@qa.com')
cy.get('input[name=password]').type('teste')
cy.get('button[type=submit]').click()
cy.contains('Email e/ou senha inválidos').should('be.visible') // Validação
})

// Cenário 3: Login com e-mail válido e senha inválida
it('Exibe erro para senha inválida', () => {
cy.visit(baseUrl)
cy.get('input[name=email]').type('fulano@qa.com')
cy.get('input[name=password]').type('senhaerrada')
cy.get('button[type=submit]').click()
cy.contains('Email e/ou senha inválidos').should('be.visible') // Validação
})

// Cenário 4: Login com todos os campos em branco
it('Exibe erro para campos em branco', () => {
cy.visit(baseUrl) // Navegar para a página de login
cy.get('button[type=submit]').click() // Clicar no botão de login sem preencher os campos

// Validar mensagens de erro exibidas
cy.get('.form > :nth-child(3)').should('contain', 'Email é obrigatório') // Mensagem correta para o email
cy.get(':nth-child(4) > :nth-child(2)').should('contain', 'Password é obrigatório') // Mensagem correta para a senha
Comment on lines +39 to +40
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more robust data-testid selector instead of a nth-child selector to reduce brittleness and improve test reliability.

Suggested change
cy.get('.form > :nth-child(3)').should('contain', 'Email é obrigatório') // Mensagem correta para o email
cy.get(':nth-child(4) > :nth-child(2)').should('contain', 'Password é obrigatório') // Mensagem correta para a senha
cy.get('[data-testid="email-error"]').should('contain', 'Email é obrigatório') // Mensagem correta para o email
cy.get('[data-testid="password-error"]').should('contain', 'Password é obrigatório') // Mensagem correta para a senha

Copilot uses AI. Check for mistakes.

Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a dedicated data-testid attribute for this element, which will make the test less dependent on the DOM structure and easier to maintain.

Suggested change
cy.get(':nth-child(4) > :nth-child(2)').should('contain', 'Password é obrigatório') // Mensagem correta para a senha
cy.get('[data-testid="password-error"]').should('contain', 'Password é obrigatório') // Mensagem correta para a senha

Copilot uses AI. Check for mistakes.

})

// Cenário 5: Login com senha em branco
it('Exibe erro para senha em branco', () => {
cy.visit(baseUrl) // Navegar para a página de login
cy.get('input[name=email]').type('fulano@qa.com') // Digitar email válido
cy.get('button[type=submit]').click() // Clicar no botão de login sem preencher a senha

// Validar mensagem de erro exibida para senha em branco
cy.get('.alert > :nth-child(2)').should('contain', 'Password é obrigatório') // Mensagem correta
})

// Cenário 6: Acessar página de cadastro pelo botão "Cadastre-se"
it('Redireciona para página de cadastro', () => {
cy.visit(baseUrl) // Navegar para a página de login

// Clicar no botão "Cadastre-se" usando o data-testid correto
cy.get('[data-testid="cadastrar"]').click()

// Validar redirecionamento para a URL de cadastro
cy.url().should('include', '/cadastrarusuarios')

// Validar presença dos campos e elementos na página de cadastro
cy.get('[data-testid="nome"]').should('exist') // Campo "Nome"
cy.get('[data-testid="email"]').should('exist') // Campo "Email"
cy.get('[data-testid="password"]').should('exist') // Campo "Senha"
cy.get('.form-check-label').should('contain', 'Cadastrar como administrador?') // Texto de checkbox
cy.get('[data-testid="cadastrar"]').should('exist') // Botão "Cadastrar"
})
})
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -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"
}
37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts 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) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
17 changes: 17 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example support/e2e.ts 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'
Loading