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: Background:
Given my user account has the role "admin" Given my user account has the role "admin"
And we have a selection of categories And we have a selection of tags and categories as well as posts
And we have a selection of tags
And we have a selection of posts
And I am logged in And I am logged in
Scenario: See an overview of categories Scenario: See an overview of categories

View File

@ -20,37 +20,18 @@ const narratorParams = {
Given("I am logged in", () => { Given("I am logged in", () => {
cy.login(loginCredentials); cy.login(loginCredentials);
}); });
Given("we have a selection of categories", () => { Given("we have a selection of categories", () => {
cy.neode() cy.createCategories();
.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"
});
}); });
Given("we have a selection of tags", () => { Given("we have a selection of tags and categories as well as posts", () => {
cy.factory() cy.createCategories()
.factory()
.authenticateAs(loginCredentials) .authenticateAs(loginCredentials)
.create("Tag", { id: "Ecology" }) .create("Tag", { id: "Ecology" })
.create("Tag", { id: "Nature" }) .create("Tag", { id: "Nature" })
.create("Tag", { id: "Democracy" }); .create("Tag", { id: "Democracy" });
});
Given("we have a selection of posts", () => {
const someAuthor = { const someAuthor = {
id: "authorId", id: "authorId",
email: "author@example.org", email: "author@example.org",
@ -436,7 +417,8 @@ Given("I follow the user {string}", name => {
}); });
Given('"Spammy Spammer" wrote a post {string}', title => { Given('"Spammy Spammer" wrote a post {string}', title => {
cy.factory() cy.createCategories()
.factory()
.authenticateAs({ .authenticateAs({
email: "spammy-spammer@example.org", email: "spammy-spammer@example.org",
password: "1234" password: "1234"
@ -458,7 +440,8 @@ Then("nobody is following the user profile anymore", () => {
}); });
Given("I wrote a post {string}", title => { Given("I wrote a post {string}", title => {
cy.factory() cy.createCategories()
.factory()
.authenticateAs(loginCredentials) .authenticateAs(loginCredentials)
.create("Post", { title, categoryIds: ["cat2"] }); .create("Post", { title, categoryIds: ["cat2"] });
}); });

View File

@ -5,7 +5,7 @@ Feature: Notifications for a mentions
Background: Background:
Given we have a selection of categories 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 | | name | slug | email | password |
| Wolle aus Hamburg | wolle-aus-hamburg | wolle@example.org | 1234 | | Wolle aus Hamburg | wolle-aus-hamburg | wolle@example.org | 1234 |
| Matt Rider | matt-rider | matt@example.org | 4321 | | Matt Rider | matt-rider | matt@example.org | 4321 |

View File

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

View File

@ -13,55 +13,76 @@
// Cypress.Commands.add('login', (email, password) => { ... }) // Cypress.Commands.add('login', (email, password) => { ... })
/* globals Cypress cy */ /* globals Cypress cy */
import 'cypress-file-upload' import "cypress-file-upload";
import { getLangByName } from './helpers' import { getLangByName } from "./helpers";
import users from '../fixtures/users.json' import users from "../fixtures/users.json";
const switchLang = name => { const switchLang = name => {
cy.get('.locale-menu').click() cy.get(".locale-menu").click();
cy.contains('.locale-menu-popover a', name).click() cy.contains(".locale-menu-popover a", name).click();
} };
Cypress.Commands.add('switchLanguage', (name, force) => { Cypress.Commands.add("switchLanguage", (name, force) => {
const code = getLangByName(name).code const code = getLangByName(name).code;
if (force) { if (force) {
switchLang(name) switchLang(name);
} else { } else {
cy.get('html').then($html => { cy.get("html").then($html => {
if ($html && $html.attr('lang') !== code) { if ($html && $html.attr("lang") !== code) {
switchLang(name) 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"
}) })
} .create("Category", {
}) id: "cat2",
name: "Happiness & Values",
Cypress.Commands.add('login', ({ email, password }) => { slug: "happiness-values",
cy.visit(`/login`) icon: "heart-o"
cy.get('input[name=email]') })
.trigger('focus') .create("Category", {
.type(email) id: "cat3",
cy.get('input[name=password]') name: "Health & Wellbeing",
.trigger('focus') slug: "health-wellbeing",
.type(password) icon: "medkit"
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}`)
})
// //
// //
// -- This is a child command -- // -- This is a child command --