Skip to content

Commit

Permalink
Merge pull request PrestaShop#20802 from nesrineabdmouleh/functionalC…
Browse files Browse the repository at this point in the history
…RUDDiscount

Add test 'CRUD cart rule'
  • Loading branch information
PierreRambaud authored Sep 1, 2020
2 parents 708448b + 7741659 commit ca32e04
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tests/UI/campaigns/data/faker/cartRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ module.exports = class CartRule {
this.code = cartRuleToCreate.code;
this.customer = cartRuleToCreate.customer || faker.internet.email();
this.freeShipping = cartRuleToCreate.freeShipping === undefined ? 'on' : cartRuleToCreate.freeShipping;
this.percent = cartRuleToCreate.percent === undefined ? true : cartRuleToCreate.percent;
this.value = cartRuleToCreate.value || faker.random.number({min: 1, max: 100});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
require('module-alias/register');
// Using chai
const {expect} = require('chai');

// Import utils
const helper = require('@utils/helpers');
const loginCommon = require('@commonTests/loginBO');

// Import pages
const dashboardPage = require('@pages/BO/dashboard');
const cartRulesPage = require('@pages/BO/catalog/discounts');
const addCartRulePage = require('@pages/BO/catalog/discounts/add');
const foHomePage = require('@pages/FO/home');
const foLoginPage = require('@pages/FO/login');
const foProductPage = require('@pages/FO/product');
const cartPage = require('@pages/FO/cart');

// Import data
const CartRuleFaker = require('@data/faker/cartRule');
const ProductData = require('@data/FO/product');
const {DefaultAccount} = require('@data/demo/customer');
const {Products} = require('@data/demo/products');

// import test context
const testContext = require('@utils/testContext');

const baseContext = 'functional_BO_shopParameters_productSettings_CRUDCartRule';

let browserContext;
let page;

const newCartRuleData = new CartRuleFaker(
{
code: '4QABV6L3',
customer: DefaultAccount.email,
percent: true,
value: 20,
},
);

const editCartRuleData = new CartRuleFaker(
{
code: '3PAJA6B3',
customer: DefaultAccount.email,
percent: true,
value: 30,
},
);

describe('CRUD cart rule', async () => {
// before and after functions
before(async function () {
browserContext = await helper.createBrowserContext(this.browser);
page = await helper.newTab(browserContext);
});

after(async () => {
await helper.closeBrowserContext(browserContext);
});

it('should login in BO', async function () {
await loginCommon.loginBO(this, page);
});

it('should go to \'Catalog > Discounts\' page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToDiscountsPage', baseContext);

await dashboardPage.goToSubMenu(
page,
dashboardPage.catalogParentLink,
dashboardPage.discountsLink,
);

const pageTitle = await cartRulesPage.getPageTitle(page);
await expect(pageTitle).to.contains(cartRulesPage.pageTitle);
});

describe('Create cart rule', async () => {
it('should go to new cart rule page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToNewCartRulePage', baseContext);

await cartRulesPage.goToAddNewCartRulesPage(page);
const pageTitle = await addCartRulePage.getPageTitle(page);
await expect(pageTitle).to.contains(addCartRulePage.pageTitle);
});

it('should create new cart rule', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'createCartRule', baseContext);

const validationMessage = await addCartRulePage.createEditCartRules(page, newCartRuleData);
await expect(validationMessage).to.contains(addCartRulePage.successfulCreationMessage);
});
});

describe('Verify created cart rule in FO', async () => {
it('should view my shop', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'viewMyShopToCheckCreatedDiscount', baseContext);

// View my shop and init pages
page = await addCartRulePage.viewMyShop(page);

await foHomePage.changeLanguage(page, 'en');
const isHomePage = await foHomePage.isHomePage(page);
await expect(isHomePage, 'Fail to open FO home page').to.be.true;
});

it('should go to login page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToLoginPageFO_1', baseContext);

await foHomePage.goToLoginPage(page);
const pageTitle = await foLoginPage.getPageTitle(page);
await expect(pageTitle, 'Fail to open FO login page').to.contains(foLoginPage.pageTitle);
});

it('should sign in with default customer', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'sighInFO_1', baseContext);

await foLoginPage.customerLogin(page, DefaultAccount);
const isCustomerConnected = await foLoginPage.isCustomerConnected(page);
await expect(isCustomerConnected, 'Customer is not connected').to.be.true;
});

it('should go to the first product page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToFirstProductPage_1', baseContext);

// Go to home page
await foLoginPage.goToHomePage(page);

await foHomePage.goToProductPage(page, 1);
const pageTitle = await foProductPage.getPageTitle(page);
await expect(pageTitle.toUpperCase()).to.contains(ProductData.firstProductData.name);
});

it('should add product to cart', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'addProductToCart_1', baseContext);

await foProductPage.addProductToTheCart(page);

// getNumberFromText is used to get the notifications number in the cart
const notificationsNumber = await cartPage.getNumberFromText(page, foProductPage.cartProductsCount);
await expect(notificationsNumber).to.be.equal(1);
});

it('should verify the total before discount', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'verifyTotalBeforeDiscount_1', baseContext);

const priceTTC = await cartPage.getTTCPrice(page);
await expect(priceTTC).to.equal(Products.demo_1.finalPrice);
});

it('should set the promo code', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'setPromoCode_1', baseContext);

await cartPage.setPromoCode(page, newCartRuleData.code);
});

it('should verify the total after discount', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'verifyTotalAfterDiscount_1', baseContext);

const discountedPrice = Products.demo_1.finalPrice - (Products.demo_1.finalPrice * newCartRuleData.value / 100);

const priceTTC = await cartPage.getTTCPrice(page);
await expect(priceTTC).to.equal(parseFloat(discountedPrice.toFixed(2)));
});

it('should sign out from FO', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'sighOutFO_1', baseContext);

await cartPage.logout(page);
const isCustomerConnected = await cartPage.isCustomerConnected(page);
await expect(isCustomerConnected, 'Customer is connected').to.be.false;
});
});

describe('Update cart rule', async () => {
it('should go back to BO', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goBackToBo_1', baseContext);

// Close tab and init other page objects with new current tab
page = await foHomePage.closePage(browserContext, page, 0);

const pageTitle = await cartRulesPage.getPageTitle(page);
await expect(pageTitle).to.contains(cartRulesPage.pageTitle);
});

it('should go to edit cart rule page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToEditCartRulePage', baseContext);

await cartRulesPage.goToEditCartRulePage(page);

const pageTitle = await addCartRulePage.getPageTitle(page);
await expect(pageTitle).to.contains(addCartRulePage.editPageTitle);
});

it('should update cart rule', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'updateCartRule', baseContext);

const validationMessage = await addCartRulePage.createEditCartRules(page, editCartRuleData);
await expect(validationMessage).to.contains(addCartRulePage.successfulUpdateMessage);
});
});

describe('Verify updated cart rule in FO', async () => {
it('should view my shop', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'viewMyShopToCheckEditedDiscount', baseContext);

// View my shop and init pages
page = await addCartRulePage.viewMyShop(page);

await foHomePage.changeLanguage(page, 'en');
const isHomePage = await foHomePage.isHomePage(page);
await expect(isHomePage, 'Fail to open FO home page').to.be.true;
});

it('should go to login page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToLoginPageFO_2', baseContext);

await foHomePage.goToLoginPage(page);
const pageTitle = await foLoginPage.getPageTitle(page);
await expect(pageTitle, 'Fail to open FO login page').to.contains(foLoginPage.pageTitle);
});

it('should sign in with default customer', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'sighInFO_2', baseContext);

await foLoginPage.customerLogin(page, DefaultAccount);
const isCustomerConnected = await foLoginPage.isCustomerConnected(page);
await expect(isCustomerConnected, 'Customer is not connected').to.be.true;
});

it('should go to the first product page', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goToFirstProductPage_2', baseContext);

// Go to home page
await foLoginPage.goToHomePage(page);

await foHomePage.goToProductPage(page, 1);
const pageTitle = await foProductPage.getPageTitle(page);
await expect(pageTitle.toUpperCase()).to.contains(ProductData.firstProductData.name);
});

it('should add product to cart', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'addProductToCart_2', baseContext);

await foProductPage.addProductToTheCart(page);

// getNumberFromText is used to get the notifications number in the cart
const notificationsNumber = await cartPage.getNumberFromText(page, foProductPage.cartProductsCount);
await expect(notificationsNumber).to.be.equal(1);
});

it('should verify the total before discount', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'verifyTotalBeforeDiscount_2', baseContext);

const priceTTC = await cartPage.getTTCPrice(page);
await expect(priceTTC).to.equal(Products.demo_1.finalPrice);
});

it('should set the promo code', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'setPromoCode_2', baseContext);

await cartPage.setPromoCode(page, editCartRuleData.code);
});

it('should verify the total after discount', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'verifyTotalAfterDiscount_2', baseContext);

const discountedPrice = Products.demo_1.finalPrice - (Products.demo_1.finalPrice * editCartRuleData.value / 100);

const priceTTC = await cartPage.getTTCPrice(page);
await expect(priceTTC).to.equal(parseFloat(discountedPrice.toFixed(2)));
});
});

describe('Delete the created cart rule', async () => {
it('should go back to BO', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'goBackToBo_2', baseContext);

// Close tab and init other page objects with new current tab
page = await foHomePage.closePage(browserContext, page, 0);

const pageTitle = await cartRulesPage.getPageTitle(page);
await expect(pageTitle).to.contains(cartRulesPage.pageTitle);
});

it('should delete cart rule', async function () {
await testContext.addContextItem(this, 'testIdentifier', 'deleteCartRule', baseContext);

const validationMessage = await cartRulesPage.deleteCartRule(page);
await expect(validationMessage).to.contains(cartRulesPage.successfulDeleteMessage);
});
});
});
22 changes: 18 additions & 4 deletions tests/UI/pages/BO/catalog/discounts/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ class AddCartRule extends BOBasePage {
super();

this.pageTitle = 'Cart Rules > Add new •';
this.editPageTitle = 'Cart Rules > Edit';

// Selectors
this.cartRuleForm = '#cart_rule_form';
// Information tab
this.nameInput = ID => `#name_${ID}`;
this.descriptionTextArea = `${this.cartRuleForm} textarea[name='description']`;
this.codeInput = '#code';
this.generateButton = '#cart_rule_informations a.btn-default';
// Conditions tab
this.conditionsTabLink = '#cart_rule_link_conditions';
this.singleCustomerInput = '#customerFilter';
Expand All @@ -21,7 +23,9 @@ class AddCartRule extends BOBasePage {
// Actions tab
this.actionsTabLink = '#cart_rule_link_actions';
this.freeShippingInput = TOGGLE => `${this.cartRuleForm} label[for='free_shipping_${TOGGLE}']`;
this.saveAndStayButton = '#desc-cart_rule-save-and-stay';
this.applyDiscountRadioButton = TOGGLE => `${this.cartRuleForm} label[for='apply_discount_${TOGGLE}']`;
this.reductionPercentInput = '#reduction_percent';
this.saveButton = '#desc-cart_rule-save';
}

/* Methods */
Expand All @@ -35,17 +39,27 @@ class AddCartRule extends BOBasePage {
// Fill information form
await this.setValue(page, this.nameInput(1), cartRuleData.name);
await this.setValue(page, this.descriptionTextArea, cartRuleData.description);
await this.setValue(page, this.codeInput, cartRuleData.code);

if (cartRuleData.code) {
await this.setValue(page, this.codeInput, cartRuleData.code);
} else {
await page.click(this.generateButton);
}
// Fill conditions form
await page.click(this.conditionsTabLink);
await this.setValue(page, this.singleCustomerInput, cartRuleData.customer);
await this.waitForVisibleSelector(page, `${this.singleCustomerResultBlock}:not([style*='display: none;'])`);
await page.click(this.singleCustomerResultItem);
await page.waitForSelector(`${this.singleCustomerResultBlock}[style*='display: none;']`);
// Fill actions form
await page.click(this.actionsTabLink);
await page.click(this.freeShippingInput(cartRuleData.freeShipping));
await this.clickAndWaitForNavigation(page, this.saveAndStayButton);

if (cartRuleData.percent) {
await page.click(this.applyDiscountRadioButton('percent'));
}
await this.setValue(page, this.reductionPercentInput, cartRuleData.value.toString());

await this.clickAndWaitForNavigation(page, this.saveButton);
return this.getTextContent(page, this.alertSuccessBlock);
}
}
Expand Down
Loading

0 comments on commit ca32e04

Please sign in to comment.