{"record":{"id":"fd09639a7cabc0f0","repo":"angular/components","slug":"failed-to-find-element-matching-one-of-the-followi","errorCode":null,"errorMessage":"Failed to find element matching one of the following queries:\n${queryDescriptions.map(desc => `(${desc})`).join(',\n')}","messagePattern":"Failed to find element matching one of the following queries:\n(.+?)\\)`\\)\\.join\\(',\n'\\)\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cdk/testing/harness-environment.ts","lineNumber":500,"sourceCode":"        matchedHarnessTypes.add(result.constructor);\n        dedupedMatches.push(result);\n      }\n    } else if (!testElementMatched) {\n      testElementMatched = true;\n      dedupedMatches.push(result);\n    }\n  }\n  return dedupedMatches as T;\n}\n\n/** Verifies that there is at least one result in an array. */\nasync function _assertResultFound<T>(\n  results: Promise<T[]>,\n  queryDescriptions: string[],\n): Promise<T> {\n  const result = (await results)[0];\n  if (result == undefined) {\n    throw Error(\n      `Failed to find element matching one of the following queries:\\n` +\n        queryDescriptions.map(desc => `(${desc})`).join(',\\n'),\n    );\n  }\n  return result;\n}\n\n/** Gets a list of description strings from a list of queries. */\nfunction _getDescriptionForLocatorForQueries(queries: (string | HarnessQuery<any>)[]) {\n  return queries.map(query =>\n    typeof query === 'string'\n      ? _getDescriptionForTestElementQuery(query)\n      : _getDescriptionForComponentHarnessQuery(query),\n  );\n}\n\n/** Gets a description string for a `ComponentHarness` query. */\nfunction _getDescriptionForComponentHarnessQuery(query: HarnessQuery<any>) {","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/testing/harness-environment.ts#L482-L518","documentation":"The standard 'no match found' error of the CDK harness system, thrown by _assertResultFound when the first result of a locator/harnessLoader/getChildLoader query is undefined. locatorFor and harnessLoaderFor are strict by design — unlike locatorForOptional, they must find a match and throw otherwise. The message lists every query description that was tried.","triggerScenarios":"Calling locatorFor(query)(), harnessLoaderFor(query)(), or loader.getChildLoader(selector) when no element matches: wrong selector string, wrong harness type, element not yet rendered, or element outside the loader root.","commonSituations":"Typos in CSS selectors, querying components rendered conditionally (ngIf) that are absent in that test, missing fixture.detectChanges()/whenStable before querying, or harness from TestBed vs. manual environment mismatch.","solutions":["Use locatorForOptional/getHarnessOrNull instead to handle 'may not exist' explicitly.","Call fixture.detectChanges()/await fixture.whenStable() before querying so the element is rendered.","Verify the selector/harness type matches what the template actually renders (check the query descriptions in the message).","Ensure the query runs under the right root loader (e.g. getChildLoader on the correct parent)."],"exampleFix":"// before\nconst btn = await loader.locatorFor('.save-button')();\n// after\nconst btn = await loader.locatorForOptional('.save-button')();\nif (!btn) throw new Error('Save button not rendered');","handlingStrategy":"fallback","validationCode":"const count = (await loader.getAllHarnesses(ChildHarness)).length;\nif (count === 0) { fixture.detectChanges(); await fixture.whenStable(); }","typeGuard":"const isFound = <T>(r: T | undefined): r is T => r !== undefined;","tryCatchPattern":"try { return await loader.locatorFor('.item')(); } catch (e) { if ((e as Error).message.includes('Failed to find element matching')) return locatorForOptional('.item')(); throw e; }","preventionTips":["Prefer locatorForOptional when absence is expected","Always stabilize the fixture (whenStable/detectChanges) before locating","Check selectors against the rendered template, not the spec assumptions"],"tags":["angular-cdk","testing","component-harness","element-not-found"],"backgroundTag":"element-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}