Refactor cypress tests

- add cypress command to createCategories to DRY out the steps
- remove step from background when isn't directly related to the setup
This commit is contained in:
Matt Rider 2019-08-21 10:00:44 +02:00
parent c43ed33b7d
commit e8871f51d4
5 changed files with 74 additions and 73 deletions

View File

@ -15,9 +15,7 @@ Feature: Tags and Categories
Background:
Given my user account has the role "admin"
And we have a selection of categories
And we have a selection of tags
And we have a selection of posts
And we have a selection of tags and categories as well as posts
And I am logged in
Scenario: See an overview of categories

View File

@ -20,37 +20,18 @@ const narratorParams = {
Given("I am logged in", () => {
cy.login(loginCredentials);
});
Given("we have a selection of categories", () => {
cy.neode()
.create("Category", {
id: "cat1",
name: "Just For Fun",
slug: "just-for-fun",
icon: "smile"
})
.create("Category", {
id: "cat2",
name: "Happiness & Values",
slug: "happiness-values",
icon: "heart-o"
})
.create("Category", {
id: "cat3",
name: "Health & Wellbeing",
slug: "health-wellbeing",
icon: "medkit"
});
cy.createCategories();
});
Given("we have a selection of tags", () => {
cy.factory()
Given("we have a selection of tags and categories as well as posts", () => {
cy.createCategories()
.factory()
.authenticateAs(loginCredentials)
.create("Tag", { id: "Ecology" })
.create("Tag", { id: "Nature" })
.create("Tag", { id: "Democracy" });
});
Given("we have a selection of posts", () => {
const someAuthor = {
id: "authorId",
email: "author@example.org",
@ -436,7 +417,8 @@ Given("I follow the user {string}", name => {
});
Given('"Spammy Spammer" wrote a post {string}', title => {
cy.factory()
cy.createCategories()
.factory()
.authenticateAs({
email: "spammy-spammer@example.org",
password: "1234"
@ -458,7 +440,8 @@ Then("nobody is following the user profile anymore", () => {
});
Given("I wrote a post {string}", title => {
cy.factory()
cy.createCategories()
.factory()
.authenticateAs(loginCredentials)
.create("Post", { title, categoryIds: ["cat2"] });
});

View File

@ -5,7 +5,7 @@ Feature: Notifications for a mentions
Background:
Given we have a selection of categories
Given we have the following user accounts:
And we have the following user accounts:
| name | slug | email | password |
| Wolle aus Hamburg | wolle-aus-hamburg | wolle@example.org | 1234 |
| Matt Rider | matt-rider | matt@example.org | 4321 |

View File

@ -5,7 +5,6 @@ Feature: Block a User
Background:
Given I have a user account
And we have a selection of categories
And there is an annoying user called "Spammy Spammer"
Scenario Outline: Blocked users cannot see each others posts

View File

@ -13,55 +13,76 @@
// Cypress.Commands.add('login', (email, password) => { ... })
/* globals Cypress cy */
import 'cypress-file-upload'
import { getLangByName } from './helpers'
import users from '../fixtures/users.json'
import "cypress-file-upload";
import { getLangByName } from "./helpers";
import users from "../fixtures/users.json";
const switchLang = name => {
cy.get('.locale-menu').click()
cy.contains('.locale-menu-popover a', name).click()
}
cy.get(".locale-menu").click();
cy.contains(".locale-menu-popover a", name).click();
};
Cypress.Commands.add('switchLanguage', (name, force) => {
const code = getLangByName(name).code
Cypress.Commands.add("switchLanguage", (name, force) => {
const code = getLangByName(name).code;
if (force) {
switchLang(name)
switchLang(name);
} else {
cy.get('html').then($html => {
if ($html && $html.attr('lang') !== code) {
switchLang(name)
cy.get("html").then($html => {
if ($html && $html.attr("lang") !== code) {
switchLang(name);
}
});
}
});
Cypress.Commands.add("login", ({ email, password }) => {
cy.visit(`/login`);
cy.get("input[name=email]")
.trigger("focus")
.type(email);
cy.get("input[name=password]")
.trigger("focus")
.type(password);
cy.get("button[name=submit]")
.as("submitButton")
.click();
cy.get(".iziToast-message").should("contain", "You are logged in!");
cy.get(".iziToast-close").click();
});
Cypress.Commands.add("logout", (email, password) => {
cy.visit(`/logout`);
cy.location("pathname").should("contain", "/login"); // we're out
});
Cypress.Commands.add("openPage", page => {
if (page === "landing") {
page = "";
}
cy.visit(`/${page}`);
});
Cypress.Commands.add("createCategories", () => {
cy.neode()
.create("Category", {
id: "cat1",
name: "Just For Fun",
slug: "just-for-fun",
icon: "smile"
})
}
})
Cypress.Commands.add('login', ({ email, password }) => {
cy.visit(`/login`)
cy.get('input[name=email]')
.trigger('focus')
.type(email)
cy.get('input[name=password]')
.trigger('focus')
.type(password)
cy.get('button[name=submit]')
.as('submitButton')
.click()
cy.get('.iziToast-message').should('contain', 'You are logged in!')
cy.get('.iziToast-close').click()
})
Cypress.Commands.add('logout', (email, password) => {
cy.visit(`/logout`)
cy.location('pathname').should('contain', '/login') // we're out
})
Cypress.Commands.add('openPage', page => {
if (page === 'landing') {
page = ''
}
cy.visit(`/${page}`)
})
.create("Category", {
id: "cat2",
name: "Happiness & Values",
slug: "happiness-values",
icon: "heart-o"
})
.create("Category", {
id: "cat3",
name: "Health & Wellbeing",
slug: "health-wellbeing",
icon: "medkit"
});
});
//
//
// -- This is a child command --