Skip to content

Commit

Permalink
Added dummy MP test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiseev committed Jan 17, 2025
1 parent 9e4054b commit a317ee8
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "proprietary",
"main": "index.js",
"scripts": {
"build:one": "webpack --env entryPattern=./src/tests/checkout/SAPI9_checkout_70.test.js",
"build:one": "webpack --env entryPattern=./src/tests/dashboard/M8_view_dashboard.test.js",
"build:all": "webpack --env entryPattern=./src/tests/**/*.test.js",
"build:S": "webpack --env entryPattern=./src/tests/**/S*.test.js",
"build:SAPI": "webpack --env entryPattern=./src/tests/**/SAPI*.test.js",
Expand Down
79 changes: 79 additions & 0 deletions src/fixtures/merchant-user.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { AbstractFixture } from './abstract.fixture';

const DEFAULT_PASSWORD = 'change123';

export class MerchantUserFixture extends AbstractFixture {
constructor({ idMerchant, merchantUserCount = 1 }) {
super();
this.idMerchant = idMerchant;
this.merchantUserCount = merchantUserCount;
}

getData() {
const response = this.runDynamicFixture(this._getMerchantUsersPayload());
const responseData = JSON.parse(response.body).data;

return responseData
.filter((item) => /^merchantUser\d+$/.test(item.attributes.key))
.map((item) => {
const { id_user, username, first_name, last_name, status } = item.attributes.data;

return {
id: id_user,
username,
password: DEFAULT_PASSWORD,
firstName: first_name,
lastName: last_name,
status,
};
});
}

static iterateData(data, vus = __VU) {
const merchantUserIndex = (vus - 1) % data.length;

return data[merchantUserIndex];
}

_getMerchantUsersPayload() {
const baseOperations = [
{
type: 'transfer',
name: 'MerchantTransfer',
key: 'merchant',
arguments: {
idMerchant: this.idMerchant,
},
},
];

const merchantUsers = Array.from({ length: this.merchantUserCount }, (_, i) => this._createMerchantPayload(i)).flat();

return JSON.stringify({
data: {
type: 'dynamic-fixtures',
attributes: {
synchronize: false,
operations: [...baseOperations, ...merchantUsers],
},
},
});
}

_createMerchantPayload(index) {
const merchantUserKey = `merchantUser${index + 1}`;
return [
{
type: "helper",
name: "haveUser",
key: merchantUserKey,
arguments: [{ "password": DEFAULT_PASSWORD }]
},
{
type: "helper",
name: "haveMerchantUserWithAclEntities",
"arguments": ["#merchant", `#${merchantUserKey}`]
},
];
}
}
104 changes: 104 additions & 0 deletions src/tests/dashboard/M8_view_dashboard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {group} from 'k6';
import OptionsUtil from '../../utils/options.util';
import {createMetrics} from '../../utils/metric.util';
import {check} from 'k6';
import EnvironmentUtil from '../../utils/environment.util';
import {MerchantUserFixture} from "../../fixtures/merchant-user.fixture";
import {browser} from 'k6/browser';

const testConfiguration = {
...EnvironmentUtil.getDefaultTestConfiguration(),
id: 'M8',
group: 'Dashboard',
vus: 1,
iterations: 10,
metrics: ['M8_view_dashboard'],
thresholds: {
M8_view_dashboard: {
smoke: ['avg<475'],
load: ['avg<475'],
},
},
};

const {metrics, metricThresholds} = createMetrics(testConfiguration);
export const options = OptionsUtil.loadOptions(testConfiguration, metricThresholds);

export function setup() {
const dynamicFixture = new MerchantUserFixture({
idMerchant: 1,
merchantUserCount: testConfiguration.vus,
});

return dynamicFixture.getData();
}

async function login(merchantUser) {
const browserContext = await browser.newContext(); // Create browser context
const page = await browserContext.newPage(); // Create a new page in the context

try {
// Navigate to login page
await page.goto(`${EnvironmentUtil.getMerchantPortalUrl()}/security-merchant-portal-gui/login`);

// Fill in login credentials
await page.locator('#security-merchant-portal-gui_username').type(merchantUser.username);
await page.locator('#security-merchant-portal-gui_password').type(merchantUser.password);

// Submit the form
const submitButton = page.locator('[name="security-merchant-portal-gui"] button[type="submit"]');
await Promise.all([
page.waitForNavigation(), // Wait for navigation to complete
submitButton.click(), // Click the submit button
]);

// Ensure the header is loaded
const header = page.locator('h1');
await header.waitFor(); // Wait for the header to appear

// Validate the header text
const headerText = await header.textContent();
check(headerText, {
'Header text is correct': (text) => text === 'Dashboard',
});

return browserContext; // Return the browser context for reuse
} catch (error) {
console.error('Error during login:', error);
throw error; // Rethrow error for debugging
} finally {
await page.close(); // Always close the page
}
}

export default async function (data) {
const merchantUser = MerchantUserFixture.iterateData(data);
const context = await login(merchantUser);

group(testConfiguration.group, async () => {
const page = await context.newPage();

try {
await page.goto(`${EnvironmentUtil.getMerchantPortalUrl()}/dashboard-merchant-portal-gui/dashboard`);
// Wait for the page to load completely
await page.waitForLoadState('load');

// Record a performance mark
await page.evaluate(() => window.performance.mark('page-visit'));

// Retrieve performance marks
const marks = await page.evaluate(() =>
JSON.parse(JSON.stringify(window.performance.getEntriesByType('mark')))
);

if (marks.length > 0) {
const totalActionTime = marks[0].startTime; // Use startTime of the first mark
metrics[testConfiguration.metrics[0]].add(totalActionTime);
}
} finally {
await page.close(); // Close the page after use
}
});

await context.close(); // Close context after the test
}
13 changes: 12 additions & 1 deletion src/utils/environment.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export default class EnvironmentUtil {
}
}

static getMerchantPortalUrl() {
switch (__ENV.K6_HOSTENV) {
case 'local':
return 'http://mp.eu.spryker.local';
case 'staging':
return 'https://mp.eu.spryker-suiteperformance.cloud.spryker.toys';
default:
console.error('Url or env not defined');
}
}

static getStorefrontApiUrl() {
switch (__ENV.K6_HOSTENV) {
case 'local':
Expand Down Expand Up @@ -88,7 +99,7 @@ export default class EnvironmentUtil {
return {
vus: EnvironmentUtil.getVus(),
iterations: EnvironmentUtil.getIterations(),
exec: EnvironmentUtil.getExecutor(),
executor: EnvironmentUtil.getExecutor(),
};
}
}
5 changes: 5 additions & 0 deletions src/utils/scenario.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ export function createDefaultScenario(testConfiguration) {
executor: testConfiguration.executor,
vus: testConfiguration.vus,
iterations: testConfiguration.iterations,
options: {
browser: {
type: 'chromium',
},
},
};
}

0 comments on commit a317ee8

Please sign in to comment.