From f4a24fe2324255c2eb9875501444483fde5f1cb1 Mon Sep 17 00:00:00 2001 From: ChrisZie Date: Sat, 17 Oct 2020 14:08:56 +0800 Subject: [PATCH] move sendMessage method into a command --- cypress/integration/contactForm.spec.js | 27 +++----------- cypress/support/commands.js | 48 ++++++++++++------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/cypress/integration/contactForm.spec.js b/cypress/integration/contactForm.spec.js index 0305a0a..1c0c041 100644 --- a/cypress/integration/contactForm.spec.js +++ b/cypress/integration/contactForm.spec.js @@ -1,5 +1,6 @@ /* Beginner Challenge -Create an automated test that completes the contact us form on the homepage, submits it, and asserts that the form was completed successfully. +Create an automated test that completes the contact us form on the homepage, + submits it, and asserts that the form was completed successfully. */ describe('/contactForm', () => { @@ -42,31 +43,13 @@ describe('/contactForm', () => { .should('be.visible') }) - it('Submit a contact request', () => { + it.only('Submit a contact request', () => { //Save name and subject in variables to check in the end if submit of the contact form was successfull and response message contains these the name and subject let name = cy.faker.name.findName() let subject = 'CypressTest '+ cy.faker.lorem.word() + let email = cy.faker.internet.email() - //Fill in the contact form input fields with generated fake Data by faker.js - cy.get('input#name') - .should('be.visible') - .type(name) - cy.get('input#email') - .type(cy.faker.internet.email()) - cy.get('input#phone') - .type(cy.faker.phone.phoneNumber()) - cy.get('input#subject') - .type(subject) - cy.get('textarea#description') - .type(cy.faker.lorem.text()) - - //Click on the 'submit' button - cy.get('button#submitContact') - .click() - - //Check if response message contains name and subject from the contact form - cy.contains('h2', name) - cy.contains('p', subject) + cy.sendMessage(name, subject, email) }) }) \ No newline at end of file diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ca4d256..96c048e 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,25 +1,23 @@ -// *********************************************** -// 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) => { ... }) +Cypress.Commands.add("sendMessage", (name, subject,email) => { + //Fill in the contact form input fields with generated fake Data by faker.js and the parameters + cy.get('input#name') + .should('be.visible') + .type(name) + cy.get('input#email') + .type(email) + cy.get('input#phone') + .type(cy.faker.phone.phoneNumber()) + cy.get('input#subject') + .type(subject) + cy.get('textarea#description') + .type(cy.faker.lorem.lines()) + + //Click on the 'submit' button + cy.get('button#submitContact') + .click() + + //Check if response message contains name and subject from the contact form + cy.contains('h2', name) + cy.contains('p', subject) + }) +