diff --git a/features/rope-record/rope-detail/rope-detail.feature b/features/rope-record/rope-detail/rope-detail.feature index 71e7f5e..e486c22 100644 --- a/features/rope-record/rope-detail/rope-detail.feature +++ b/features/rope-record/rope-detail/rope-detail.feature @@ -5,7 +5,7 @@ Feature: Rope Detail Given I navigate to the Rope record page Scenario: User switches between tabs on the Rope Detail Page - Given I navigate to the Rope Detail Page for Serial number '1234' + Given I navigate to the Rope Detail Page for Serial number 'CUCSNO168681' Then the following sections should be visible: | Breaking load test | | Weight | @@ -22,7 +22,7 @@ Feature: Rope Detail | Construction details | Scenario: User edits the Breaking Load information - Given I navigate to the Rope Detail Page for Serial number '1234' + Given I navigate to the Rope Detail Page for Serial number 'CUCSNO168681' And I provide the following information in the Breaking load form | Breaking load | "4" | Then I should be able to see the updated Breaking load data \ No newline at end of file diff --git a/src/pages/CreateRopeRecord.ts b/src/pages/CreateRopeRecord.ts index e4b5904..c22f6f4 100644 --- a/src/pages/CreateRopeRecord.ts +++ b/src/pages/CreateRopeRecord.ts @@ -4,14 +4,15 @@ import { FormFieldMetadata, FormHelper } from '../support/FormHelper'; export class CreateRopeRecord { private readonly page: Page; - private readonly formHelper; + private readonly formHelper: FormHelper; constructor(page: Page) { this.page = page; this.formHelper = new FormHelper(); } - private ropeMetadata: FormFieldMetadata[] = [ + // Parent metadata object + public ropeMetadata: FormFieldMetadata[] = [ { name: 'Rope type', type: 'select' }, { name: 'Hoist #', type: 'select' }, { name: 'Serial number', type: 'input' }, @@ -23,173 +24,36 @@ export class CreateRopeRecord { { name: 'Manufacturer name', type: 'input' }, { name: 'Manufacturer address', type: 'input' }, { name: 'Manufacture date', type: 'date' }, - ]; - - private breakingLoadMetadata: FormFieldMetadata[] = [ { name: 'Breaking load', type: 'input' }, { name: 'Test number', type: 'input' }, { name: 'Test date', type: 'date' }, - ]; - - private constructionInfoMetaData: FormFieldMetadata[] = [ - { - name: 'Class of core used in the rope', - type: 'input', - }, - { - name: 'Number of strands in the rope', - type: 'input', - }, - { - name: 'Number of wires in each strand', - type: 'input', - }, + { name: 'Class of core used in the rope', type: 'input' }, + { name: 'Number of strands in the rope', type: 'input' }, + { name: 'Number of wires in each strand', type: 'input' }, { name: 'Diameter of wires', type: 'input' }, { name: 'Breaking stress of steel', type: 'input' }, - { - name: 'Standard torsion test of the', - type: 'input', - }, + { name: 'Standard torsion test of the', type: 'input' }, { name: 'The percentage by mass of', type: 'input' }, { name: 'The trade name of the', type: 'input' }, ]; - private settersMap: Record Promise> = { - 'Rope type': this.setRopeType.bind(this), - 'Hoist #': this.setHoistNumber.bind(this), - 'Serial number': this.setSerialNumber.bind(this), - 'Diameter of rope (optional)': this.setDiameter.bind(this), - 'Weight of rope': this.setWeight.bind(this), - 'Construction of rope': this.setConstruction.bind(this), - 'Type of lay': this.setTypeOfLay.bind(this), - 'Grade of steel': this.setGradeOfSteel.bind(this), - 'Manufacturer name': this.setManufacturerName.bind(this), - 'Manufacturer address': this.setManufacturerAddress.bind(this), - 'Manufacture date': this.setManufactureDate.bind(this), - 'Breaking load': this.setBreakingLoad.bind(this), - 'Test number': this.setBreakingLoadTestNumber.bind(this), - 'Test date': this.setBreakingLoadTestDate.bind(this), - 'Class of core used in the rope': this.setClassOfCore.bind(this), - 'Number of strands in the rope': this.setNumberOfStrands.bind(this), - 'Number of wires in each strand': this.setNumberOfWires.bind(this), - 'Diameter of wires': this.setDiameterOfWires.bind(this), - 'Breaking stress of steel': this.setBreakingStress.bind(this), - 'Standard torsion test of the': this.setStandardTorsion.bind(this), - 'The percentage by mass of': this.setPercentageByMass.bind(this), - 'The trade name of the': this.setTradeName.bind(this), - }; - - // Method to dynamically call the appropriate setter - public async setFieldValue(name: string, value: string): Promise { - const setter = this.settersMap[name]; - - if (setter) { - await setter(value); // Call the setter dynamically - } else { - throw new Error(`${name}: not found`); + // Dynamic field value setter + public async setFieldValue(fieldName: string, value: string, metadata: FormFieldMetadata[]): Promise { + const fieldMeta = metadata.find(meta => meta.name === fieldName); + if (!fieldMeta) { + throw new Error(`Field metadata not found for "${fieldName}"`); } + await this.formHelper.setField(this.page, fieldName, value, metadata); } - // Setter methods for each field - public async setRopeType(value: string): Promise { - await this.setField('Rope type', value); - } - - public async setHoistNumber(value: string): Promise { - await this.setField('Hoist #', value); - } - - public async setSerialNumber(value: string): Promise { - await this.setField('Serial number', value); - } - - public async setDiameter(value: string): Promise { - await this.setField('Diameter of rope (optional)', value); - } - - public async setWeight(value: string): Promise { - await this.setField('Weight of rope', value); - } - - public async setConstruction(value: string): Promise { - await this.setField('Construction of rope', value); - } - - public async setTypeOfLay(value: string): Promise { - await this.setField('Type of lay', value); - } - - public async setGradeOfSteel(value: string): Promise { - await this.setField('Grade of steel', value); - } - - public async setManufacturerName(value: string): Promise { - await this.setField('Manufacturer name', value); - } - - public async setManufacturerAddress(value: string): Promise { - await this.setField('Manufacturer address', value); - } - - public async setManufactureDate(value: string): Promise { - await this.setField('Manufacture date', value); - } - - public async setBreakingLoad(value: string): Promise { - await this.setField('Breaking load', value); - } - - public async setBreakingLoadTestNumber(value: string): Promise { - await this.setField('Test number', value); - } - - public async setBreakingLoadTestDate(value: string): Promise { - await this.setField('Test date', value); - } - - public async setClassOfCore(value: string): Promise { - await this.setField('Class of core used in the rope', value); - } - - public async setNumberOfStrands(value: string): Promise { - await this.setField('Number of strands in the rope', value); - } - - public async setNumberOfWires(value: string): Promise { - await this.setField('Number of wires in each strand', value); - } - - public async setDiameterOfWires(value: string): Promise { - await this.setField('Diameter of wires', value); - } - - public async setBreakingStress(value: string): Promise { - await this.setField('Breaking stress of steel', value); - } - - public async setStandardTorsion(value: string): Promise { - await this.setField('Standard torsion test of the', value); - } - - public async setPercentageByMass(value: string): Promise { - await this.setField('The percentage by mass of', value); - } - - public async setTradeName(value: string): Promise { - await this.setField('The trade name of the', value); - } - - // Helper function to find metadata by field name and set the value - private async setField(fieldName: string, value: string): Promise { - const combinedRopeMetadata = [ - ...this.ropeMetadata, - ...this.breakingLoadMetadata, - ...this.constructionInfoMetaData, - ]; - - await this.formHelper.setField(this.page, fieldName, value, combinedRopeMetadata); + // Set multiple field values for a specific metadata group + public async setFieldValues(fields: Record, metadata: FormFieldMetadata[]): Promise { + for (const [fieldName, value] of Object.entries(fields)) { + await this.setFieldValue(fieldName, value, metadata); + } } + // Open modals and tabs async openAddRecordsModal() { await this.page.waitForSelector('text=Applied:', { state: 'visible' }); const text = this.page.getByText('Add records', { exact: true }); @@ -215,9 +79,15 @@ export class CreateRopeRecord { await this.openAddRecordsModal(); await this.selectAddNewRope(); - // open the accordions to render the DOM - await this.openAccordionTab('Rope information'); - await this.openAccordionTab('Construction info'); - await this.openAccordionTab('Breaking load test info'); + // Handling accordions + const accordionTabs = ['Rope information', 'Construction info', 'Breaking load test info']; + for (const tabName of accordionTabs) { + await this.openAccordionTab(tabName); + } + } + + // Set field values for specific metadata groups + public async setRopeInformation(fields: Record) { + await this.setFieldValues(fields, this.ropeMetadata); } } diff --git a/src/steps/rope-record/rope-detail/rope-detail.ts b/src/steps/rope-record/rope-detail/rope-detail.ts index 0040abf..6e7e32c 100644 --- a/src/steps/rope-record/rope-detail/rope-detail.ts +++ b/src/steps/rope-record/rope-detail/rope-detail.ts @@ -28,7 +28,7 @@ When( const ropeRecord = new CreateRopeRecord(this.page!); const breakingLoadData = breakingLoadInfo.rowsHash(); for (const [fieldName, value] of Object.entries(breakingLoadData)) { - await ropeRecord.setFieldValue(fieldName, value); + await ropeRecord.setFieldValue(fieldName, value, ropeRecord.ropeMetadata); } await this.page?.getByRole('button', { name: 'Save' }).click(); }, diff --git a/src/steps/rope-record/rope.record.page.ts b/src/steps/rope-record/rope.record.page.ts index 368b4dc..1be1601 100644 --- a/src/steps/rope-record/rope.record.page.ts +++ b/src/steps/rope-record/rope.record.page.ts @@ -19,15 +19,23 @@ Given( const ropeRecord = new CreateRopeRecord(page); // Create an instance of CreateRopeRecord for (const [fieldName, value] of Object.entries(ropeInfo)) { - // let cleanValue = value.replace(/"/g, '').trim().toLowerCase(); // Clean up value let cleanValue = value; + + // Handle specific fields like Serial number if (fieldName === 'Serial number') { cleanValue = uniqueGenerator.generateUniqueValue('CUCSNO', 6); // Generate unique serial number this.generatedSerialNumber = cleanValue; HoistWorld.sharedState.generatedSerialNumber = cleanValue; } - await ropeRecord.setFieldValue(fieldName, cleanValue); + + if (!ropeRecord.ropeMetadata) { + throw new Error(`No metadata found for field: ${fieldName}`); + } + + // Pass metadata wrapped in an array + await ropeRecord.setFieldValue(fieldName, cleanValue, ropeRecord.ropeMetadata); } + this.ropeRecord = ropeRecord; await executeWithDelay(); },