Link | Cypress | Test & QA
- Link | Cypress | Test & QA
Código usado
describe("Dealing with links that opens a new tab", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("demoQA")}/links`);
});
it("First approach, not click on the link", () => {
cy.get("#simpleLink").should("have.attr", "href", "https://demoqa.com");
cy.get("#simpleLink").should("have.attr", "target", "_blank");
});
it("Second approach, remove the target", () => {
cy.get("#simpleLink").invoke("removeAttr", "target").click();
cy.url().then((url) => {
expect(url).to.be.equal("https://demoqa.com/");
});
});
});
describe("Incercepting API(SPYING) requests after clicking on a button", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("demoQA")}/links`);
cy.intercept("GET", `${Cypress.env("demoQA")}/created`).as("linkStatus");
});
it("First approach, not click on the link", () => {
cy.get("a#created").click();
cy.wait("@linkStatus").then((request) => {
cy.log("This is the request intercepted", request);
expect(request.response.statusCode).to.be.equal(201);
expect(request.response.statusMessage).to.be.equal("Created");
});
});
});
describe("Basics using the internet app", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("theInternet")}/redirector`);
});
it("Internal Link redirect validation", () => {
cy.get("#redirect").click();
cy.url().then((url) => {
expect(url).to.be.equal(`${Cypress.env("theInternet")}/status_codes`);
});
});
});
1. Links que Abrem em Nova Aba (target="_blank")
describe("Dealing with links that opens a new tab", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("demoQA")}/links`);
});
O que acontece aqui:
describe: agrupa testes relacionados a links que abrem nova ababeforeEach: antes de cada teste, acessa a página/linksCypress.env(): lê variáveis de ambiente definidas no Cypress
Teste 1 – Validando o link sem clicar
it("First approach, not click on the link", () => {
cy.get("#simpleLink").should("have.attr", "href", "https://demoqa.com");
cy.get("#simpleLink").should("have.attr", "target", "_blank");
});
Explicação:
- Seleciona o link pelo ID
- Valida os atributos
hrefetarget - Não executa navegação, apenas valida o HTML
Teste 2 – Removendo o target para permitir o clique
it("Second approach, remove the target", () => {
cy.get("#simpleLink").invoke("removeAttr", "target").click();
cy.url().then((url) => {
expect(url).to.be.equal("https://demoqa.com/");
});
});
Explicação:
- Remove o atributo
targetdinamicamente - Permite que o Cypress navegue na mesma aba
- Valida a URL final após o clique
2. Interceptando Requisições de API (Spy)
describe("Incercepting API(SPYING) requests after clicking on a button", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("demoQA")}/links`);
cy.intercept("GET", `${Cypress.env("demoQA")}/created`).as("linkStatus");
});
Explicação:
- Intercepta chamadas HTTP do tipo GET
- Cria um alias para reutilizar a requisição nos testes
Teste – Validação da resposta da API
it("First approach, not click on the link", () => {
cy.get("a#created").click();
cy.wait("@linkStatus").then((request) => {
cy.log("This is the request intercepted", request);
expect(request.response.statusCode).to.be.equal(201);
expect(request.response.statusMessage).to.be.equal("Created");
});
});
Explicação:
- O clique dispara a requisição
- O Cypress aguarda a resposta interceptada
- Valida status code e status message
3. Validação de Redirecionamento Interno
describe("Basics using the internet app", () => {
beforeEach(() => {
cy.visit(`${Cypress.env("theInternet")}/redirector`);
});
Explicação:
- A página de redirecionamento é carregada antes de cada teste
Teste – Validação do redirecionamento
it("Internal Link redirect validation", () => {
cy.get("#redirect").click();
cy.url().then((url) => {
expect(url).to.be.equal(`${Cypress.env("theInternet")}/status_codes`);
});
});
Explicação:
- Executa o clique
- Captura a URL final
- Confirma que o redirecionamento ocorreu corretamente