@@ -48,8 +48,6 @@ export class MatEndDateHarness extends MatDatepickerInputHarnessBase {
4848export class MatDateRangeInputHarness extends DatepickerTriggerHarnessBase {
4949 static hostSelector = '.mat-date-range-input' ;
5050
51- private readonly _floatingLabelSelector = '.mdc-floating-label' ;
52-
5351 /**
5452 * Gets a `HarnessPredicate` that can be used to search for a `MatDateRangeInputHarness`
5553 * that meets certain criteria.
@@ -91,26 +89,28 @@ export class MatDateRangeInputHarness extends DatepickerTriggerHarnessBase {
9189 return this . locatorFor ( MatEndDateHarness ) ( ) ;
9290 }
9391
94- /** Gets the floating label text for the range input, if it exists. */
92+ /**
93+ * Gets the label for the range input, if it exists. This might be provided by a label element or
94+ * by the `aria-label` attribute.
95+ */
9596 async getLabel ( ) : Promise < string | null > {
96- // Copied from MatFormFieldControlHarnessBase since this class cannot extend two classes
97+ // Directly copied from MatFormFieldControlHarnessBase. This class already has a parent so it
98+ // cannot extend MatFormFieldControlHarnessBase for the functionality.
9799 const documentRootLocator = this . documentRootLocatorFactory ( ) ;
98100 const labelId = await ( await this . host ( ) ) . getAttribute ( 'aria-labelledby' ) ;
101+ const labelText = await ( await this . host ( ) ) . getAttribute ( 'aria-label' ) ;
99102 const hostId = await ( await this . host ( ) ) . getAttribute ( 'id' ) ;
100103
101104 if ( labelId ) {
102- // First option, try to fetch the label using the `aria-labelledby`
103- // attribute.
104- const labelEl = await documentRootLocator . locatorForOptional (
105- `${ this . _floatingLabelSelector } [id="${ labelId } "]` ,
106- ) ( ) ;
105+ // First, try to find the label by following [aria-labelledby]
106+ const labelEl = await documentRootLocator . locatorForOptional ( `[id="${ labelId } "]` ) ( ) ;
107107 return labelEl ? labelEl . text ( ) : null ;
108+ } else if ( labelText ) {
109+ // If that doesn't work, return [aria-label] if it exists
110+ return labelText ;
108111 } else if ( hostId ) {
109- // Fallback option, try to match the id of the input with the `for`
110- // attribute of the label.
111- const labelEl = await documentRootLocator . locatorForOptional (
112- `${ this . _floatingLabelSelector } [for="${ hostId } "]` ,
113- ) ( ) ;
112+ // Finally, search the DOM for a label that points to the host element
113+ const labelEl = await documentRootLocator . locatorForOptional ( `[for="${ hostId } "]` ) ( ) ;
114114 return labelEl ? labelEl . text ( ) : null ;
115115 }
116116 return null ;
0 commit comments