Skip to content

Commit

Permalink
feat: expose getSelectorType (#376)
Browse files Browse the repository at this point in the history
Exposes an enum of SelectorTypes we support and
allows getting a type from a string.
  • Loading branch information
OrKoN committed Nov 9, 2022
1 parent 2d4f7b7 commit 11eec45
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export type Pattern = string;
export type Selector = string | string[];
export type FrameSelector = number[];

export enum SelectorType {
CSS = 'css',
ARIA = 'aria',
Text = 'text',
XPath = 'xpath',
}

export enum StepType {
Change = 'change',
Click = 'click',
Expand Down
19 changes: 18 additions & 1 deletion src/SchemaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import type {
import {
AssertedEvent,
BaseStep,
ChangeStep,
Expand All @@ -32,6 +32,7 @@ import type {
NavigateStep,
ScrollStep,
Selector,
SelectorType,
SetViewportStep,
Step,
StepWithFrame,
Expand Down Expand Up @@ -569,3 +570,19 @@ export function parse(data: unknown): UserFlow {
steps: parseSteps(data.steps),
});
}

/**
* Detects what type of a selector the string contains. For example,
* `aria/Label` is a SelectorType.ARIA.
*
* Note that CSS selectors are special and usually don't require a prefix,
* therefore, SelectorType.CSS is the default type if other types didn't match.
*/
export function getSelectorType(selector: string): SelectorType {
for (const value of Object.values(SelectorType)) {
if (selector.startsWith(`${value}/`)) {
return value;
}
}
return SelectorType.CSS;
}
16 changes: 13 additions & 3 deletions test/SchemaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
limitations under the License.
*/

import { parse } from '../src/SchemaUtils.js';
import { AssertedEventType, StepType } from '../src/Schema.js';
import { parse, getSelectorType } from '../src/SchemaUtils.js';
import { AssertedEventType, SelectorType, StepType } from '../src/Schema.js';
import { assert } from 'chai';

describe('SchemaUtils', () => {
Expand Down Expand Up @@ -722,7 +722,7 @@ describe('SchemaUtils', () => {
);
});

it('should handle wrong input with erros', () => {
it('should handle wrong input with errors', () => {
const testCases = [
{
input: {},
Expand Down Expand Up @@ -768,4 +768,14 @@ describe('SchemaUtils', () => {
}
});
});

describe('Selectors', () => {
it('should detect selector types', () => {
assert.strictEqual(getSelectorType('css/.cls'), SelectorType.CSS);
assert.strictEqual(getSelectorType('.cls'), SelectorType.CSS);
assert.strictEqual(getSelectorType('aria/Text'), SelectorType.ARIA);
assert.strictEqual(getSelectorType('text/Text'), SelectorType.Text);
assert.strictEqual(getSelectorType('xpath///div'), SelectorType.XPath);
});
});
});

0 comments on commit 11eec45

Please sign in to comment.