@letsrunit/playwright 0.21.1 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@letsrunit/playwright",
3
- "version": "0.21.1",
3
+ "version": "0.22.0",
4
4
  "description": "Playwright extensions and utilities for letsrunit",
5
5
  "keywords": [
6
6
  "testing",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "packageManager": "yarn@4.10.3",
44
44
  "dependencies": {
45
- "@letsrunit/utils": "0.21.1",
45
+ "@letsrunit/utils": "0.22.0",
46
46
  "@playwright/test": "1.58.2",
47
47
  "case": "^1.6.3",
48
48
  "diff": "^8.0.3",
@@ -2,6 +2,7 @@ import { Locator } from '@playwright/test';
2
2
 
3
3
  type LocatorMethod = (...args: any[]) => any;
4
4
  type ProxyProperty = string | symbol;
5
+ export const FALLBACK_LOCATOR_CANDIDATES = Symbol('letsrunit.playwright.fallback-locator-candidates');
5
6
 
6
7
  const ACTION_METHODS = new Set([
7
8
  'blur',
@@ -158,6 +159,7 @@ export function createFallbackLocator(candidates: Locator[]): Locator {
158
159
 
159
160
  const proxy = new Proxy(primary as unknown as object, {
160
161
  get(_target, prop: ProxyProperty) {
162
+ if (prop === FALLBACK_LOCATOR_CANDIDATES) return candidates;
161
163
  if (typeof prop !== 'string') return (primary as any)[prop];
162
164
  if (passthroughMetaProperties.has(prop)) return (primary as any)[prop];
163
165
 
@@ -195,3 +197,7 @@ export function createFallbackLocator(candidates: Locator[]): Locator {
195
197
 
196
198
  return proxy as Locator;
197
199
  }
200
+
201
+ export function getFallbackLocatorCandidates(locator: Locator): Locator[] | null {
202
+ return ((locator as any)[FALLBACK_LOCATOR_CANDIDATES] as Locator[] | undefined) ?? null;
203
+ }
@@ -55,9 +55,7 @@ export async function setFieldValue(el: Locator, value: Value, options?: SetOpti
55
55
  setFallback,
56
56
  );
57
57
 
58
- if ((await el.count()) > 1) {
59
- el = await pickFieldElement(el);
60
- }
58
+ el = await pickFieldElement(el);
61
59
 
62
60
  const tag = await el.evaluate((e) => e.tagName.toLowerCase(), options);
63
61
  const type = (
@@ -1,8 +1,24 @@
1
1
  import type { Locator } from '@playwright/test';
2
+ import { getFallbackLocatorCandidates } from '../fallback-locator';
3
+
4
+ async function resolveConcreteFieldLocator(elements: Locator): Promise<Locator> {
5
+ const fallbackCandidates = getFallbackLocatorCandidates(elements);
6
+ if (!fallbackCandidates) return elements;
7
+
8
+ for (const candidate of fallbackCandidates) {
9
+ try {
10
+ if ((await candidate.count()) > 0) return candidate;
11
+ } catch {}
12
+ }
13
+
14
+ return elements;
15
+ }
2
16
 
3
17
  export async function pickFieldElement(elements: Locator): Promise<Locator> {
18
+ elements = await resolveConcreteFieldLocator(elements);
19
+
4
20
  const count = await elements.count();
5
- if (count === 1) return elements;
21
+ if (count <= 1) return elements.first();
6
22
 
7
23
  const candidates: { el: Locator; tag: string; role: string | null; isVisible: boolean }[] = [];
8
24
 
@@ -43,6 +59,5 @@ export async function pickFieldElement(elements: Locator): Promise<Locator> {
43
59
  return elements.nth(isParent);
44
60
  }
45
61
 
46
- // Return both elements and let the error (on evaluate) occur.
47
- return elements;
62
+ return elements.first();
48
63
  }