From a8e7e61e1e6edcf934436ae0d7c360827de44cf9 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 16 Mar 2023 10:55:50 +0100 Subject: [PATCH] MOBILE-4270 behat: Support setting fields by label --- src/testing/services/behat-dom.ts | 28 +++++++++++++++++++++++++++ src/testing/services/behat-runtime.ts | 17 ++-------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/testing/services/behat-dom.ts b/src/testing/services/behat-dom.ts index 270ba3fc8..e2cadf34e 100644 --- a/src/testing/services/behat-dom.ts +++ b/src/testing/services/behat-dom.ts @@ -384,6 +384,34 @@ export class TestingBehatDomUtilsService { return topContainers; } + /** + * Find a field. + * + * @param field Field name. + * @returns Field element. + */ + findField(field: string): HTMLElement | HTMLInputElement | undefined { + const input = this.findElementBasedOnText( + { text: field, selector: 'input, textarea, [contenteditable="true"], ion-select, ion-datetime' }, + { onlyClickable: false, containerName: '' }, + ); + + if (input) { + return input; + } + + const label = this.findElementBasedOnText( + { text: field, selector: 'label' }, + { onlyClickable: false, containerName: '' }, + ); + + if (label) { + const inputId = label.getAttribute('for'); + + return (inputId && document.getElementById(inputId)) || undefined; + } + } + /** * Function to find element based on their text or Aria label. * diff --git a/src/testing/services/behat-runtime.ts b/src/testing/services/behat-runtime.ts index 4970daaed..46e47c906 100644 --- a/src/testing/services/behat-runtime.ts +++ b/src/testing/services/behat-runtime.ts @@ -422,7 +422,7 @@ export class TestingBehatRuntimeService { async setField(field: string, value: string): Promise { this.log('Action - Set field ' + field + ' to: ' + value); - const found = this.findField(field); + const found = TestingBehatDomUtils.findField(field); if (!found) { return 'ERROR: No element matches field to set.'; @@ -445,7 +445,7 @@ export class TestingBehatRuntimeService { async fieldMatches(field: string, value: string): Promise { this.log('Action - Field ' + field + ' matches value: ' + value); - const found = this.findField(field); + const found = TestingBehatDomUtils.findField(field); if (!found) { return 'ERROR: No element matches field to set.'; @@ -459,19 +459,6 @@ export class TestingBehatRuntimeService { return 'OK'; } - /** - * Find a field. - * - * @param field Field name. - * @returns Field element. - */ - protected findField(field: string): HTMLElement | HTMLInputElement | undefined { - return TestingBehatDomUtils.findElementBasedOnText( - { text: field, selector: 'input, textarea, [contenteditable="true"], ion-select, ion-datetime' }, - { onlyClickable: false, containerName: '' }, - ); - } - /** * Get the value of a certain field. *