Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions cypress/e2e/article.cy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
describe('', () => {
before(() => {
import ArticlePageObject from '../support/pages/article.pageObject';

});
const ArticlePage = new ArticlePageObject();

it('', () => {
describe('Article', () => {
let user;

beforeEach(() => {
cy.task('generateUser').then((generatedUser) => {
user = generatedUser;
cy.login(user.email, user.username, user.password);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the cy.login command is correctly defined in your Cypress commands. If it's a custom command, verify its implementation to ensure it logs in the user as expected.

cy.visit('/editor');
});
});

it('', () => {
it('should fill in article and create it', () => {
const article = ArticlePage.generateArticle();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method generateArticle should be defined in the ArticlePageObject. Ensure it returns an article object with all necessary fields for creation.

ArticlePage.createArticle(article);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method createArticle should be defined in the ArticlePageObject. Ensure it correctly interacts with the page to fill in the article details.

ArticlePage.onClickSubmitArticle();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method onClickSubmitArticle should be defined in the ArticlePageObject. Ensure it triggers the article submission correctly.

ArticlePage.assertArticleTitle(article.title);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method assertArticleTitle should be defined in the ArticlePageObject. Ensure it correctly asserts that the article title is displayed as expected.

});

it('should be able to delete an article', () => {
const article = ArticlePage.generateArticle();
cy.createArticle(article).then((response) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the cy.createArticle command is correctly defined and returns a response with the article slug. This is crucial for navigating to the article page.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cy.createArticle command is used here, but it is not defined within this file. Ensure that this command is implemented elsewhere in your Cypress commands or imported correctly.

cy.visit(`/article/${response.body.article.slug}`);
ArticlePage.assertArticleTitle(article.title);
cy.get('.btn.btn-outline-danger.btn-sm').eq(0).click();
cy.assertPageURL('/');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cy.assertPageURL command should be defined to check the current URL. Make sure it correctly verifies that the user is redirected to the homepage after deleting an article.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cy.assertPageURL command is used here, but it is not defined within this file. Ensure that this command is implemented elsewhere in your Cypress commands or imported correctly.

});
});
});
7 changes: 7 additions & 0 deletions cypress/support/PageObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PageObject {
visit(url) {
cy.visit(url || this.url);
}
}

export default PageObject;
45 changes: 28 additions & 17 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,35 @@ Cypress.Commands.add('login', (email, username, password) => {
});
});

Cypress.Commands.add('createArticle', (title, description, body) => {
cy.getCookie('auth').then((token) => {
const authToken = token.value;
Cypress.Commands.add('findByPlaceholder', (placeholder) => {
return cy.get(`[placeholder="${placeholder}"]`);
});

Cypress.Commands.add(
'createArticle',
({ title, description, body, tag }) => {
cy.getCookie('auth').then((token) => {
const authToken = token.value;

cy.request({
method: 'POST',
url: '/api/articles',
body: {
article: {
title,
description,
body,
tagList: []
cy.request({
method: 'POST',
url: '/api/articles',
body: {
article: {
title,
description,
body,
tagList: [tag]
}
},
headers: {
Authorization: `Token ${authToken}`
}
},
headers: {
Authorization: `Token ${authToken}`
}
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the createArticle command handles the response from the cy.request if you need to use the article data (e.g., slug) in subsequent test steps.

});
});
}
);

Cypress.Commands.add('assertPageURL', (url) => {
cy.url().should('equal', Cypress.config().baseUrl + url);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertPageURL command assumes that the baseUrl is set in the Cypress configuration. Ensure that this configuration is correctly set to avoid assertion failures.

});
52 changes: 52 additions & 0 deletions cypress/support/pages/article.pageObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { faker } from '@faker-js/faker';
import PageObject from '../PageObject';

class ArticlePageObject extends PageObject {
get articleTitleField() {
return cy.findByPlaceholder('Article Title');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the findByPlaceholder command returns the element to allow chaining. This is important for the getter methods to work as intended.

}

get articleAboutField() {
return cy.findByPlaceholder(`What's this article about?`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the findByPlaceholder command returns the element to allow chaining. This is important for the getter methods to work as intended.

}

get articleBodyField() {
return cy.findByPlaceholder('Write your article (in markdown)');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the findByPlaceholder command returns the element to allow chaining. This is important for the getter methods to work as intended.

}

get articleTagsField() {
return cy.findByPlaceholder('Enter tags');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the findByPlaceholder command returns the element to allow chaining. This is important for the getter methods to work as intended.

}

get submitButton() {
return cy.get('.btn.btn-lg.btn-primary');
}

onClickSubmitArticle() {
this.submitButton.click();
}

generateArticle() {
return {
title: faker.lorem.word(),
description: faker.lorem.words(),
body: faker.lorem.words(),
tag: faker.lorem.word()
};
}

createArticle(article) {
const { title, description, tag, body } = article;

this.articleTitleField.type(title);
this.articleAboutField.type(description);
this.articleBodyField.type(body);
this.articleTagsField.type(`${tag}{Enter}`);
}

assertArticleTitle(title) {
cy.contains(title).should('be.visible');
}
};

export default ArticlePageObject;