{"record":{"id":"6bee3738acdd1b5a","repo":"angular/components","slug":"no-selected-tab-could-be-found","errorCode":null,"errorMessage":"No selected tab could be found.","messagePattern":"No selected tab could be found\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material/tabs/testing/tab-group-harness.ts","lineNumber":59,"sourceCode":"\n  /**\n   * Gets the list of tabs in the tab group.\n   * @param filter Optionally filters which tabs are included.\n   */\n  async getTabs(filter: TabHarnessFilters = {}): Promise<MatTabHarness[]> {\n    return this.locatorForAll(MatTabHarness.with(filter))();\n  }\n\n  /** Gets the selected tab of the tab group. */\n  async getSelectedTab(): Promise<MatTabHarness> {\n    const tabs = await this.getTabs();\n    const isSelected = await parallel(() => tabs.map(t => t.isSelected()));\n    for (let i = 0; i < tabs.length; i++) {\n      if (isSelected[i]) {\n        return tabs[i];\n      }\n    }\n    throw new Error('No selected tab could be found.');\n  }\n\n  /**\n   * Selects a tab in this tab group.\n   * @param filter An optional filter to apply to the child tabs. The first tab matching the filter\n   *     will be selected.\n   */\n  async selectTab(filter: TabHarnessFilters = {}): Promise<void> {\n    const tabs = await this.getTabs(filter);\n    if (!tabs.length) {\n      throw Error(`Cannot find mat-tab matching filter ${JSON.stringify(filter)}`);\n    }\n    await tabs[0].select();\n  }\n}\n","sourceCodeStart":41,"sourceCodeEnd":75,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material/tabs/testing/tab-group-harness.ts#L41-L75","documentation":"MatTabGroupHarness.getSelectedTab iterates all tabs and returns the first whose isSelected() is true. If no tab reports selected, it throws 'No selected tab could be found.', indicating the harness found tabs but none was marked selected.","triggerScenarios":"Calling harness.getSelectedTab() on a MatTabGroupHarness when the tab group has no selected tab (e.g. no selectedIndex bound/defaulted, or selection removed programmatically before the harness call).","commonSituations":"Test setups where selectedIndex is undefined or the group renders tabs lazily before selection is initialized; race conditions where the test asserts selection before change detection marks the tab selected; custom grouping where `mat-tab-group` has `dynamicHeight`/no preselected index and the app never sets one.","solutions":["Ensure a tab is selected before calling getSelectedTab (set selectedIndex input or call harness.selectTab first)","Await Angular change detection / harness stability (use await harness.host() or fixture.detectChanges + whenStable) before the assertion","Check that getTabs()/filter arguments are not unintentionally excluding the selected tab"],"exampleFix":"// before\nconst selected = await harness.getSelectedTab(); // throws if nothing selected\n\n// after\nawait harness.selectTab({label: 'First'});\nconst selected = await harness.getSelectedTab();","handlingStrategy":"validation","validationCode":"// Guard before calling\nconst tabs = await harness.getTabs();\nconst anySelected = (await parallel(() => tabs.map(t => t.isSelected()))).some(Boolean);\nif (!anySelected) await harness.selectTab({label: 'First'});","typeGuard":null,"tryCatchPattern":"try {\n  const selected = await harness.getSelectedTab();\n} catch (e) {\n  if (String(e?.message).includes('No selected tab')) {\n    await harness.selectTab({label: 'First'});\n    selected = await harness.getSelectedTab();\n  }\n}","preventionTips":["Ensure mat-tab-group always has an initial selectedIndex","Select a tab before asserting on selection in harness tests","Await fixture.whenStable()/harness stability before harness queries","Avoid filters that could exclude the selected tab"],"tags":["angular-material","component-harness","testing","tabs"],"backgroundTag":"no-selected-element-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}