-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { expect, MatcherReturnType } from '@playwright/test'; | ||
import { getMessage } from './utils'; | ||
import { ContainTextOptions } from '../types'; | ||
|
||
import { Select } from '../models/components/Select'; | ||
|
||
export async function toHaveOption( | ||
select: Select, | ||
value: string | RegExp, | ||
options?: ContainTextOptions | ||
): Promise<MatcherReturnType> { | ||
let actual = ''; | ||
|
||
try { | ||
actual = await select | ||
.locator('div[class*="-grafana-select-value-container"]') | ||
.locator('div[class*="-singleValue"]') | ||
.innerText(options); | ||
|
||
expect(actual).toMatch(value); | ||
|
||
return { | ||
pass: true, | ||
actual: actual, | ||
expected: value, | ||
message: () => `Value successfully selected`, | ||
}; | ||
} catch (err: unknown) { | ||
return { | ||
message: () => getMessage(value.toString(), err instanceof Error ? err.toString() : 'Unknown error'), | ||
pass: false, | ||
actual, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect, MatcherReturnType } from '@playwright/test'; | ||
import { getMessage } from './utils'; | ||
|
||
import { MultiSelect } from '../models/components/MultiSelect'; | ||
|
||
export async function toHaveOptions(select: MultiSelect, values: Array<string | RegExp>): Promise<MatcherReturnType> { | ||
let actual = ''; | ||
|
||
try { | ||
const actual = await select | ||
.locator('div[class*="-grafana-select-multi-value-container"]') | ||
.locator('div[class*="-grafana-select-multi-value-container"] > div') | ||
.allInnerTexts(); | ||
|
||
expect(actual).toMatchObject(values); | ||
|
||
return { | ||
pass: true, | ||
actual: actual, | ||
expected: values, | ||
message: () => `Values successfully selected`, | ||
}; | ||
} catch (err: unknown) { | ||
return { | ||
message: () => getMessage(values.join(', '), err instanceof Error ? err.toString() : 'Unknown error'), | ||
pass: false, | ||
actual, | ||
expected: values, | ||
}; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/plugin-e2e/src/models/components/ComponentBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Locator } from '@playwright/test'; | ||
|
||
type LocatorParams = Parameters<Locator['locator']>; | ||
|
||
export abstract class ComponentBase { | ||
constructor(protected readonly element: Locator) {} | ||
|
||
locator(selectorOrLocator: LocatorParams[0], options?: LocatorParams[1]): Locator { | ||
return this.element.locator(selectorOrLocator, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Locator } from '@playwright/test'; | ||
import { PluginTestCtx } from '../../types'; | ||
import { openSelect, selectByValueOrLabel } from './Select'; | ||
import { ComponentBase } from './ComponentBase'; | ||
|
||
type OptionsType = Parameters<Locator['selectOption']>[1]; | ||
|
||
export class MultiSelect extends ComponentBase { | ||
constructor(private ctx: PluginTestCtx, element: Locator) { | ||
super(element); | ||
} | ||
|
||
async selectOptions(values: string[], options?: OptionsType): Promise<string[]> { | ||
const menu = await openSelect(this.element, options); | ||
|
||
return Promise.all( | ||
values.map((value) => { | ||
return selectByValueOrLabel(value, menu, options); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Locator } from '@playwright/test'; | ||
import { PluginTestCtx } from '../../types'; | ||
import { ComponentBase } from './ComponentBase'; | ||
|
||
type OptionsType = Parameters<Locator['selectOption']>[1]; | ||
|
||
export class Select extends ComponentBase { | ||
constructor(private ctx: PluginTestCtx, element: Locator) { | ||
super(element); | ||
} | ||
|
||
async selectOption(values: string, options?: OptionsType): Promise<string> { | ||
const menu = await openSelect(this.element, options); | ||
return selectByValueOrLabel(values, menu, options); | ||
} | ||
} | ||
|
||
export async function openSelect(select: Locator, options?: OptionsType): Promise<Locator> { | ||
await select.getByRole('combobox').click(options); | ||
return select.page().getByLabel('Select options menu', { | ||
exact: true, | ||
}); | ||
} | ||
|
||
export async function selectByValueOrLabel( | ||
labelOrValue: string, | ||
menu: Locator, | ||
options?: OptionsType | ||
): Promise<string> { | ||
if (!labelOrValue) { | ||
throw new Error(`Could not select option: "${labelOrValue}"`); | ||
} | ||
|
||
const option = menu.getByRole('option', { name: labelOrValue, exact: true }); | ||
await option.click(options); | ||
const value = await option.locator('span').textContent(options); | ||
|
||
if (!value) { | ||
throw new Error(`Could not select option: "${labelOrValue}"`); | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters