Skip to content

HP-2382 Migrate PartCreateCest.php Codeception Test to Playwright and remove Legacy Test #185

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

Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion src/views/part/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
return $model->partno . ' #' . $model->serial . ($label ?? '') ;
})(),
'icon' => 'fa-cubes',
'menu' => PartDetailMenu::widget(['model' => $model], ['linkTemplate' => '<a href="{url}" {linkOptions}><span class="pull-right">{icon}</span>&nbsp;{label}</a>']),
'menu' => PartDetailMenu::widget(['model' => $model], ['linkTemplate' => '<a href="{url}" {linkOptions}><span class="pull-right">{icon}</span>{label}</a>']),
]) ?>

<div class="row">
Expand Down
103 changes: 103 additions & 0 deletions tests/playwright/e2e/manager/part/part-create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { test } from "@hipanel-core/fixtures";
import {expect} from "@playwright/test";
import PartCreateView from "@hipanel-module-stock/page/PartCreateView";
import UniqueId from "@hipanel-core/helper/UniqueId";
import PartIndexView from "@hipanel-module-stock/page/PartIndexView";
import PartView from "@hipanel-module-stock/page/PartView";

function getPartData() {
return {
partno: 'CHASSIS EPYC 7402P',
src_id: 'TEST-DS-01',
dst_id: 'TEST-DS-02',
serials: UniqueId.generate(`MG_TEST_PART`),
move_descr: 'MG TEST MOVE',
price: 200,
currency: '$',
company_id: 'Other',
};
}

test.describe('Part Management', () => {
test('Ensure part management buttons work @hipanel-module-stock @manager', async ({ managerPage }) => {
const partView = new PartCreateView(managerPage);
await partView.navigate();

let n = await managerPage.locator('div.item').count();
expect(n).toBe(1);

await partView.addPart();
expect(await managerPage.locator('div.item').count()).toBe(++n);

await partView.addPart();
expect(await managerPage.locator('div.item').count()).toBe(++n);

await partView.copyPart();
expect(await managerPage.locator('div.item').count()).toBe(++n);

await partView.removePart();
expect(await managerPage.locator('div.item').count()).toBe(--n);

await partView.removePart();
expect(await managerPage.locator('div.item').count()).toBe(--n);

await partView.removePart();
expect(await managerPage.locator('div.item').count()).toBe(--n);
});

test('Ensure part cannot be created without data @hipanel-module-stock @manager', async ({ managerPage }) => {
const partView = new PartCreateView(managerPage);
await partView.navigate();
await partView.save();

const errorMessages = [
'Part No. cannot be blank.',
'Source cannot be blank.',
'Destination cannot be blank.',
'Serials cannot be blank.',
'Move description cannot be blank.',
'Purchase price cannot be blank.',
'Currency cannot be blank.',
];

for (const message of errorMessages) {
await expect(managerPage.locator(`text=${message}`)).toBeVisible();
}
});

test('Ensure a part can be created @hipanel-module-stock @manager', async ({ managerPage }) => {
const partView = new PartCreateView(managerPage);
const partIndexView = new PartIndexView(managerPage);
await partView.navigate();
await partView.fillPartFields(getPartData());
await partView.save();

await partIndexView.seePartWasCreated();
});

test('Ensure multiple parts can be created @hipanel-module-stock @manager', async ({ managerPage }) => {
const partView = new PartCreateView(managerPage);
const partIndexView = new PartIndexView(managerPage);
await partView.navigate();
await partView.fillPartFields(getPartData());
await partView.addPart();
await partView.fillPartFields(getPartData(), 1);
await partView.save();

await partIndexView.seePartWasCreated();
});

test('Ensure a part can be created and then deleted @hipanel-module-stock @manager', async ({ managerPage }) => {
const partCreateView = new PartCreateView(managerPage);
const partIndexView = new PartIndexView(managerPage);

await partCreateView.navigate();
await partCreateView.fillPartFields(getPartData());
await partCreateView.save();

await partIndexView.seePartWasCreated();

const partView = new PartView(managerPage);
await partView.deletePart();
});
});
37 changes: 37 additions & 0 deletions tests/playwright/e2e/manager/part/part-replace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { test } from "@hipanel-core/fixtures";
import PartIndexView from "@hipanel-module-stock/page/PartIndexView";
import PartReplaceView from "@hipanel-module-stock/page/PartReplaceView";
import UniqueId from "@hipanel-core/helper/UniqueId";

const data = {
filters: [
{
name: "move_descr_ilike",
value: "test description"
},
{
name: "model_types",
value: "cpu"
},
],
replaceData: [
{ serialno: UniqueId.generate(`test`) },
{ serialno: UniqueId.generate(`test`) }
],
};

test.describe("Part Replacement", () => {
test("Ensure parts can be replaced @hipanel-module-stock @manager", async ({ managerPage }) => {
const partIndexView = new PartIndexView(managerPage);
const partReplaceView = new PartReplaceView(managerPage);

await partIndexView.navigate();
await partIndexView.applyFilters(data.filters);
await partIndexView.selectPartsToReplace(data.replaceData.length);

await partReplaceView.fillReplaceForm(data.replaceData);
await partReplaceView.save();

await partIndexView.confirmReplacement();
});
});
31 changes: 31 additions & 0 deletions tests/playwright/e2e/manager/part/part-sell.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test } from "@hipanel-core/fixtures";
import PartIndexView from "@hipanel-module-stock/page/PartIndexView";
import SellModal from "@hipanel-module-stock/page/SellModal";
import UniqueId from "@hipanel-core/helper/UniqueId";
import BillIndexView from "@hipanel-module-stock/page/BillIndexView";

test("Ensure I can sell parts @hipanel-module-stock @manager", async ({managerPage }) => {
const partIndexView = new PartIndexView(managerPage);
const sellModal = new SellModal(managerPage);
const billIndexView = new BillIndexView(managerPage);

const sellData = {
contact_id: "Test Manager",
currency: "eur",
descr: UniqueId.generate(`test description`),
type: "HW purchase",
prices: [250, 300, 442],
time: new Date(Date.now() - 86400000).toISOString().slice(0, 16).replace('T', ' ')
};

await partIndexView.navigate();
await partIndexView.filterBySerial("MG_TEST_PART");
await partIndexView.selectRows(sellData.prices.length);
await partIndexView.openSellModal();

await sellModal.fillSellWindowFields(sellData);
await sellModal.confirmSale();
await sellModal.seePartsWereSold();

await billIndexView.ensureSellingBillWasCreated(sellData);
});
43 changes: 43 additions & 0 deletions tests/playwright/helper/PartForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Page} from "@playwright/test";
import Select2 from "@hipanel-core/input/Select2";
import PriceWithCurrency from "@hipanel-module-stock/input/PriceWithCurrency";

export default class PartForm {
private page: Page;

public constructor(page: Page) {
this.page = page;
}

public async fillPartFields(partData: any, index: number = 0) {
await Select2.field(this.page, this.selector('select', 'partno', index)).setValue(partData.partno);
await Select2.field(this.page, this.selector('select', 'src_id', index)).setValue(partData.src_id);
await Select2.field(this.page, this.selector('select', 'dst_ids', index)).setValue(partData.dst_id);

await this.fillSerials(partData.serials, index);
await this.page.fill(this.selector('input', 'move_descr', index), partData.move_descr);

await PriceWithCurrency.field(this.page, 'part', index).setSumAndCurrency(partData.price, partData.currency);

await this.page.selectOption(this.selector('select', 'company_id', index), partData.company_id);
}

private selector(type: string, name: string, index: number = 0): string {
return `${type}[id=part-${index}-${name}]`;
}

public async fillSerials(serial: string, index: number = 0) {
await this.page.fill(this.selector('input', 'serials', index), serial);
}

/**
* It is strange, but in the same form on the /stock/part/replace page "serials" input has "serial" name
*/
public async fillSerial(serial: string, index: number = 0) {
await this.page.fill(this.selector('input', 'serial', index), serial);
}

public async save() {
await this.page.click('button:has-text("Save")');
}
}
8 changes: 8 additions & 0 deletions tests/playwright/input/PriceWithCurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import InputWithCurrency from "@hipanel-core/input/InputWithCurrency";
import {Page} from "@playwright/test";

export default class PriceWithCurrency extends InputWithCurrency{
static field(page: Page, formId: string, k: number): InputWithCurrency {
return new PriceWithCurrency(page, `${formId}-${k}-price`);
}
}
29 changes: 29 additions & 0 deletions tests/playwright/page/BillIndexView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {expect, Page} from "@playwright/test";
import Index from "@hipanel-core/page/Index";

export default class BillIndexView {
private page: Page;
private index: Index;

constructor(page: Page) {
this.page = page;
this.index = new Index(page);
}

public async navigate() {
await this.page.goto('/finance/bill/index');
}

public async ensureSellingBillWasCreated(sellData: { descr: string; prices: number[] }) {
await this.navigate();

// Apply filter for the description
await this.index.applyFilter('descr', sellData.descr);

// Open the row menu of the first bill and select "View"
await this.index.clickPopoverMenu(1, 'View');

// Ensure the correct number of parts were sold
await expect(this.page.locator('tr table tr[data-key]')).toHaveCount(sellData.prices.length);
}
}
36 changes: 36 additions & 0 deletions tests/playwright/page/PartCreateView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {Page} from "@playwright/test";
import PartForm from "@hipanel-module-stock/helper/PartForm";

export default class PartCreateView {
private page: Page;
private partForm: PartForm;

public constructor(page: Page) {
this.page = page;
this.partForm = new PartForm(page);
}

public async navigate() {
await this.page.goto('/stock/part/create');
}

public async fillPartFields(partData: any, index: number = 0) {
await this.partForm.fillPartFields(partData, index);
}

public async save() {
await this.partForm.save();
}

public async addPart() {
await this.page.click("div.item:last-child button.add-item");
}

public async removePart() {
await this.page.click("div.item:last-child button.remove-item");
}

public async copyPart() {
await this.page.click("div.item:last-child button.copy");
}
}
74 changes: 74 additions & 0 deletions tests/playwright/page/PartIndexView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {expect, Page} from "@playwright/test";
import Index from "@hipanel-core/page/Index";

export default class PartIndexView {
private page: Page;
private index: Index;

constructor(page: Page) {
this.page = page;
this.index = new Index(page);
}

public async navigate() {
await this.page.goto("/stock/part/index?representation=common");
}

public async applyFilters(filters: Array<{ name: string; value: string }>) {
for (const filter of filters) {
await this.index.setFilter(filter.name, filter.value);
}

await this.index.submitSearchButton();
}

public async filterBySerial(serial: string) {
await this.index.applyFilter('serial_ilike', serial);
}

public async selectPartsToReplace(count: number) {
await this.selectRows(count);
await this.index.clickDropdownBulkButton('Bulk actions', 'Replace');
}

public async selectRows(count: number) {
await this.index.chooseRangeOfRowsOnTable(1, count);
}

public async confirmReplacement() {
await this.index.hasNotification('Part has been replaced');
}
Comment on lines +38 to +40
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Consider handling single vs. multiple part replacements.

The notification message only checks for a singular form ("Part has been replaced"). If multiple parts are replaced, the message might use a plural form.


🏁 Script executed:

#!/bin/bash
# Description: Check if there are different notification messages for single vs multiple part replacements

# Look for notification strings in the codebase
echo "Searching for part replacement notification strings in the codebase..."
rg -i "part(s?)( have| has) been replaced" --type php --type js --type ts

Length of output: 642


Action Required: Update Notification Handling for Multiple Part Replacements

After searching the codebase, it’s confirmed that all part replacement notifications currently use the singular message ("Part has been replaced")—across controllers, messages, and tests. If the application supports replacing multiple parts at once, please consider implementing conditional logic (or pluralized messages) in both the notification generation and corresponding tests. If single replacements are the only supported scenario, adding a clarifying comment regarding this design decision would be beneficial.


public async deleteItemOnTable(number: number) {
await this.chooseNumberRowOnTable(number);

await this.page.getByRole('button', { name: 'Delete' }).click();

this.page.on('dialog', async dialog => await dialog.accept());
await this.index.hasNotification('Part has been deleted');
}
Comment on lines +42 to +49
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix event handler registration timing in deleteItemOnTable method.

The dialog event handler is registered after clicking the delete button, which could lead to race conditions. The event handler should be registered before triggering the action.

 public async deleteItemOnTable(number: number) {
     await this.chooseNumberRowOnTable(number);
 
+    this.page.on('dialog', async dialog => await dialog.accept());
     await this.page.getByRole('button', { name: 'Delete' }).click();
-
-    this.page.on('dialog', async dialog => await dialog.accept());
     await this.index.hasNotification('Part has been deleted');
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public async deleteItemOnTable(number: number) {
await this.chooseNumberRowOnTable(number);
await this.page.getByRole('button', { name: 'Delete' }).click();
this.page.on('dialog', async dialog => await dialog.accept());
await this.index.hasNotification('Part has been deleted');
}
public async deleteItemOnTable(number: number) {
await this.chooseNumberRowOnTable(number);
this.page.on('dialog', async dialog => await dialog.accept());
await this.page.getByRole('button', { name: 'Delete' }).click();
await this.index.hasNotification('Part has been deleted');
}


public async chooseNumberRowOnTable(number: number) {
await this.index.chooseNumberRowOnTable(number);
}

public async seePartWasCreated() {
const rowNumber = 1;
await this.index.hasNotification('Part has been created');
await this.index.closeNotification();

// Ensure the current URL matches expected Move index URL
await expect(this.page).toHaveURL(/\/stock\/move\/index\?MoveSearch%5Bid%5D=/);

// Get first row move ID from the index table
const moveId = await this.index.getRowDataKeyByNumber(rowNumber);
expect(moveId).not.toBeNull();

// Wait /stock/part/view page to load
await this.index.clickColumnOnTable('Parts', rowNumber);
}

public async openSellModal() {
await this.index.clickDropdownBulkButton('Sell parts', 'Sell parts');
}
}
Loading