I am writing UI tests and Im struggling to break my test up into smaller tests instead of having one long test run. I use faker to generate all the data needed, so that I dont have to add fixtures. My current working test looks like this:
/// <reference types="cypress" />
import "cypress-audit/commands";
describe("Full system test", () => {
before(function () {
this.User = require("../../faker/user");
});
it("Registers and creates profile", function () {
cy.visit(Cypress.env("home_page"));
cy.Register({});
cy.UpdateProfile({});
cy.AddCompanyDetails({});
cy.AddTeamMember({});
cy.CreateVacancy({});
cy.AddCandidate({});
cy.AddAdditionalDocs({});
});
});
What I would like to do is have the tests like this:
/// <reference types="cypress" />
import "cypress-audit/commands";
describe("Full system test", () => {
before(function () {
this.User = require("../../faker/user");
});
it("Registers and creates profile", function () {
cy.visit(Cypress.env("home_page"));
cy.Register({});
cy.contains("Vacancies").click();
cy.UpdateProfile({});
cy.contains("Vacancies").click();
cy.AddCompanyDetails({});
cy.contains("Vacancies").click();
cy.AddTeamMember({});
});
it("Creates a vacancy and adds candidates", function () {
cy.CreateVacancy({});
cy.AddCandidate({});
cy.AddAdditionalDocs({});
});
The issue im having is that faker is going to generate new data if I break it up like I have in my second example. Also, I need to then sign back in every time I add a new test. Is there a way for me to continue where the last test ended ? The reason I want to do this is because I want to see the tests broken up in the testing tool so that its easier to see exactly whats failing instead of having to work it out every time. Is there maybe an easier way to do this ?
I would like it to look like this in the testing tool:
You can add the data that you want at the top of the test to make sure that the same data is being transferred in the test.
/// <reference types="cypress" />
import "cypress-audit/commands";
var faker = require('faker');
var randomName = faker.name.findName();
var randomEmail = faker.internet.email();
describe("Full system test", () => {
before(function() {
this.User = require("../../faker/user");
});
it("Registers and creates profile", function() {
cy.visit(Cypress.env("home_page"));
cy.Register({});
cy.contains("Vacancies").click();
cy.UpdateProfile({});
cy.contains("Vacancies").click();
cy.AddCompanyDetails({});
cy.contains("Vacancies").click();
cy.AddTeamMember({});
});
it("Creates a vacancy and adds candidates", function() {
cy.CreateVacancy({});
cy.AddCandidate({});
cy.AddAdditionalDocs({});
});
})
Another way could be to move the before()
hook with faker.js code to cypress/support/index.js
since this will only work for Run all specs cypress open
as the index file will be called once. But in the case of cypress run
, the index.js file is called once before every spec file so the data will change.