@testnexus/locatai 1.7.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Divyarajsinh Dodia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,173 @@
1
+ import { Locator, Page } from 'playwright-core';
2
+ import { z } from 'zod';
3
+
4
+ /**
5
+ * Type definitions and Zod schemas for LocatAI
6
+ */
7
+
8
+ type LocatorOrEmpty = Locator | string;
9
+ declare const LocatAIPlan: z.ZodObject<{
10
+ candidates: z.ZodArray<z.ZodObject<{
11
+ strategy: z.ZodObject<{
12
+ type: z.ZodEnum<{
13
+ testid: "testid";
14
+ role: "role";
15
+ label: "label";
16
+ placeholder: "placeholder";
17
+ text: "text";
18
+ altText: "altText";
19
+ title: "title";
20
+ css: "css";
21
+ }>;
22
+ value: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ selector: z.ZodOptional<z.ZodNullable<z.ZodString>>;
24
+ role: z.ZodOptional<z.ZodNullable<z.ZodString>>;
25
+ name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
26
+ text: z.ZodOptional<z.ZodNullable<z.ZodString>>;
27
+ exact: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
28
+ }, z.core.$strip>;
29
+ confidence: z.ZodNumber;
30
+ why: z.ZodString;
31
+ }, z.core.$strip>>;
32
+ }, z.core.$strip>;
33
+ type LocatAIPlanT = z.infer<typeof LocatAIPlan>;
34
+ interface LocatAIErrorContext {
35
+ action: string;
36
+ contextName: string;
37
+ url: string;
38
+ candidatesAnalyzed: number;
39
+ strategiesTried: Array<{
40
+ type: string;
41
+ reason: string;
42
+ }>;
43
+ aiResponse?: string;
44
+ originalError?: string;
45
+ }
46
+ declare class LocatAIError extends Error {
47
+ readonly context: LocatAIErrorContext;
48
+ constructor(message: string, context: LocatAIErrorContext);
49
+ static formatMessage(message: string, ctx: LocatAIErrorContext): string;
50
+ }
51
+ interface ClickOptions {
52
+ /** Force click even if element is not visible (for hover-dependent buttons) */
53
+ force?: boolean;
54
+ }
55
+ /**
56
+ * A locator that automatically falls back to AI healing if the original selector fails.
57
+ * Provides a subset of Playwright Locator methods with self-healing capabilities.
58
+ */
59
+ interface LocatAILocator {
60
+ /** Click the element, with AI fallback if selector fails */
61
+ click(options?: ClickOptions): Promise<void>;
62
+ /** Fill the element with text, with AI fallback if selector fails */
63
+ fill(value: string): Promise<void>;
64
+ /** Double-click the element, with AI fallback if selector fails */
65
+ dblclick(): Promise<void>;
66
+ /** Check the checkbox/radio, with AI fallback if selector fails */
67
+ check(): Promise<void>;
68
+ /** Hover over the element, with AI fallback if selector fails */
69
+ hover(): Promise<void>;
70
+ /** Focus the element, with AI fallback if selector fails */
71
+ focus(): Promise<void>;
72
+ /** Uncheck a checkbox/radio, with AI fallback if selector fails */
73
+ uncheck(): Promise<void>;
74
+ /** Select an option in a dropdown, with AI fallback if selector fails */
75
+ selectOption(value: string): Promise<void>;
76
+ }
77
+ interface LocatAIMethods {
78
+ click(target: LocatorOrEmpty, contextName: string, options?: ClickOptions): Promise<void>;
79
+ fill(target: LocatorOrEmpty, contextName: string, value: string): Promise<void>;
80
+ selectOption(target: LocatorOrEmpty, contextName: string, value: string): Promise<void>;
81
+ dblclick(target: LocatorOrEmpty, contextName: string): Promise<void>;
82
+ check(target: LocatorOrEmpty, contextName: string): Promise<void>;
83
+ uncheck(target: LocatorOrEmpty, contextName: string): Promise<void>;
84
+ hover(target: LocatorOrEmpty, contextName: string): Promise<void>;
85
+ focus(target: LocatorOrEmpty, contextName: string): Promise<void>;
86
+ setTestName(name: string): void;
87
+ /**
88
+ * Create a self-healing locator with a semantic description fallback.
89
+ * @param selector CSS selector or Playwright locator string
90
+ * @param contextName Semantic description for AI fallback
91
+ * @returns A LocatAILocator that can be used like a regular locator
92
+ * @example
93
+ * await page.locatai.locator('.new-todo', 'Input field for new todos').fill('Buy milk');
94
+ */
95
+ locator(selector: string, contextName: string): LocatAILocator;
96
+ }
97
+ interface LocatAIPage extends Page {
98
+ locatai: LocatAIMethods;
99
+ }
100
+ interface LocatAIOptions {
101
+ enabled?: boolean;
102
+ provider?: "openai" | "gpt" | "anthropic" | "claude" | "google" | "gemini" | "local" | "ollama";
103
+ model?: string;
104
+ cacheFile?: string;
105
+ reportFile?: string;
106
+ maxAiTries?: number;
107
+ maxCandidates?: number;
108
+ timeout?: number;
109
+ testName?: string;
110
+ apiKey?: string;
111
+ }
112
+
113
+ /**
114
+ * LocatAI - AI-powered self-healing locators for Playwright
115
+ */
116
+
117
+ /**
118
+ * Enhance a Playwright Page with self-healing capabilities.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import { withLocatAI } from '@testnexus/locatai';
123
+ *
124
+ * const locataiPage = withLocatAI(page);
125
+ * await locataiPage.locatai.click(page.getByRole('button'), 'Submit button');
126
+ * await locataiPage.locatai.fill('', 'Email input', 'test@example.com');
127
+ * ```
128
+ */
129
+ declare function withLocatAI(page: Page, opts?: LocatAIOptions): LocatAIPage;
130
+ /**
131
+ * Create a Playwright test fixture with LocatAI capabilities.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * import { test as base } from '@playwright/test';
136
+ * import { createLocatAIFixture, LocatAIPage } from '@testnexus/locatai';
137
+ *
138
+ * export const test = base.extend<{ page: LocatAIPage }>(createLocatAIFixture());
139
+ * ```
140
+ */
141
+ declare function createLocatAIFixture(opts?: LocatAIOptions): {
142
+ page: ({ page }: {
143
+ page: Page;
144
+ }, use: (page: LocatAIPage) => Promise<void>) => Promise<void>;
145
+ };
146
+
147
+ /**
148
+ * AI Provider abstraction for LocatAI
149
+ * Supports multiple AI backends: OpenAI, Anthropic, Google, Local (Ollama)
150
+ */
151
+
152
+ type ProviderName = "openai" | "gpt" | "anthropic" | "claude" | "google" | "gemini" | "local" | "ollama";
153
+ interface GenerateLocatAIPlanInput {
154
+ systemPrompt: string;
155
+ userContent: string;
156
+ jsonSchema: object;
157
+ }
158
+ interface TokenUsage {
159
+ inputTokens: number;
160
+ outputTokens: number;
161
+ totalTokens: number;
162
+ }
163
+ interface LocatAIPlanResult {
164
+ plan: LocatAIPlanT | null;
165
+ tokenUsage: TokenUsage | null;
166
+ }
167
+ interface AIProvider {
168
+ readonly name: ProviderName;
169
+ generateLocatAIPlan(input: GenerateLocatAIPlanInput): Promise<LocatAIPlanResult>;
170
+ }
171
+ declare const DEFAULT_MODELS: Record<ProviderName, string>;
172
+
173
+ export { type AIProvider, type ClickOptions, DEFAULT_MODELS, LocatAIError, type LocatAIErrorContext, type LocatAILocator, type LocatAIMethods, type LocatAIOptions, type LocatAIPage, type ProviderName, createLocatAIFixture, withLocatAI };
@@ -0,0 +1,173 @@
1
+ import { Locator, Page } from 'playwright-core';
2
+ import { z } from 'zod';
3
+
4
+ /**
5
+ * Type definitions and Zod schemas for LocatAI
6
+ */
7
+
8
+ type LocatorOrEmpty = Locator | string;
9
+ declare const LocatAIPlan: z.ZodObject<{
10
+ candidates: z.ZodArray<z.ZodObject<{
11
+ strategy: z.ZodObject<{
12
+ type: z.ZodEnum<{
13
+ testid: "testid";
14
+ role: "role";
15
+ label: "label";
16
+ placeholder: "placeholder";
17
+ text: "text";
18
+ altText: "altText";
19
+ title: "title";
20
+ css: "css";
21
+ }>;
22
+ value: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ selector: z.ZodOptional<z.ZodNullable<z.ZodString>>;
24
+ role: z.ZodOptional<z.ZodNullable<z.ZodString>>;
25
+ name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
26
+ text: z.ZodOptional<z.ZodNullable<z.ZodString>>;
27
+ exact: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
28
+ }, z.core.$strip>;
29
+ confidence: z.ZodNumber;
30
+ why: z.ZodString;
31
+ }, z.core.$strip>>;
32
+ }, z.core.$strip>;
33
+ type LocatAIPlanT = z.infer<typeof LocatAIPlan>;
34
+ interface LocatAIErrorContext {
35
+ action: string;
36
+ contextName: string;
37
+ url: string;
38
+ candidatesAnalyzed: number;
39
+ strategiesTried: Array<{
40
+ type: string;
41
+ reason: string;
42
+ }>;
43
+ aiResponse?: string;
44
+ originalError?: string;
45
+ }
46
+ declare class LocatAIError extends Error {
47
+ readonly context: LocatAIErrorContext;
48
+ constructor(message: string, context: LocatAIErrorContext);
49
+ static formatMessage(message: string, ctx: LocatAIErrorContext): string;
50
+ }
51
+ interface ClickOptions {
52
+ /** Force click even if element is not visible (for hover-dependent buttons) */
53
+ force?: boolean;
54
+ }
55
+ /**
56
+ * A locator that automatically falls back to AI healing if the original selector fails.
57
+ * Provides a subset of Playwright Locator methods with self-healing capabilities.
58
+ */
59
+ interface LocatAILocator {
60
+ /** Click the element, with AI fallback if selector fails */
61
+ click(options?: ClickOptions): Promise<void>;
62
+ /** Fill the element with text, with AI fallback if selector fails */
63
+ fill(value: string): Promise<void>;
64
+ /** Double-click the element, with AI fallback if selector fails */
65
+ dblclick(): Promise<void>;
66
+ /** Check the checkbox/radio, with AI fallback if selector fails */
67
+ check(): Promise<void>;
68
+ /** Hover over the element, with AI fallback if selector fails */
69
+ hover(): Promise<void>;
70
+ /** Focus the element, with AI fallback if selector fails */
71
+ focus(): Promise<void>;
72
+ /** Uncheck a checkbox/radio, with AI fallback if selector fails */
73
+ uncheck(): Promise<void>;
74
+ /** Select an option in a dropdown, with AI fallback if selector fails */
75
+ selectOption(value: string): Promise<void>;
76
+ }
77
+ interface LocatAIMethods {
78
+ click(target: LocatorOrEmpty, contextName: string, options?: ClickOptions): Promise<void>;
79
+ fill(target: LocatorOrEmpty, contextName: string, value: string): Promise<void>;
80
+ selectOption(target: LocatorOrEmpty, contextName: string, value: string): Promise<void>;
81
+ dblclick(target: LocatorOrEmpty, contextName: string): Promise<void>;
82
+ check(target: LocatorOrEmpty, contextName: string): Promise<void>;
83
+ uncheck(target: LocatorOrEmpty, contextName: string): Promise<void>;
84
+ hover(target: LocatorOrEmpty, contextName: string): Promise<void>;
85
+ focus(target: LocatorOrEmpty, contextName: string): Promise<void>;
86
+ setTestName(name: string): void;
87
+ /**
88
+ * Create a self-healing locator with a semantic description fallback.
89
+ * @param selector CSS selector or Playwright locator string
90
+ * @param contextName Semantic description for AI fallback
91
+ * @returns A LocatAILocator that can be used like a regular locator
92
+ * @example
93
+ * await page.locatai.locator('.new-todo', 'Input field for new todos').fill('Buy milk');
94
+ */
95
+ locator(selector: string, contextName: string): LocatAILocator;
96
+ }
97
+ interface LocatAIPage extends Page {
98
+ locatai: LocatAIMethods;
99
+ }
100
+ interface LocatAIOptions {
101
+ enabled?: boolean;
102
+ provider?: "openai" | "gpt" | "anthropic" | "claude" | "google" | "gemini" | "local" | "ollama";
103
+ model?: string;
104
+ cacheFile?: string;
105
+ reportFile?: string;
106
+ maxAiTries?: number;
107
+ maxCandidates?: number;
108
+ timeout?: number;
109
+ testName?: string;
110
+ apiKey?: string;
111
+ }
112
+
113
+ /**
114
+ * LocatAI - AI-powered self-healing locators for Playwright
115
+ */
116
+
117
+ /**
118
+ * Enhance a Playwright Page with self-healing capabilities.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import { withLocatAI } from '@testnexus/locatai';
123
+ *
124
+ * const locataiPage = withLocatAI(page);
125
+ * await locataiPage.locatai.click(page.getByRole('button'), 'Submit button');
126
+ * await locataiPage.locatai.fill('', 'Email input', 'test@example.com');
127
+ * ```
128
+ */
129
+ declare function withLocatAI(page: Page, opts?: LocatAIOptions): LocatAIPage;
130
+ /**
131
+ * Create a Playwright test fixture with LocatAI capabilities.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * import { test as base } from '@playwright/test';
136
+ * import { createLocatAIFixture, LocatAIPage } from '@testnexus/locatai';
137
+ *
138
+ * export const test = base.extend<{ page: LocatAIPage }>(createLocatAIFixture());
139
+ * ```
140
+ */
141
+ declare function createLocatAIFixture(opts?: LocatAIOptions): {
142
+ page: ({ page }: {
143
+ page: Page;
144
+ }, use: (page: LocatAIPage) => Promise<void>) => Promise<void>;
145
+ };
146
+
147
+ /**
148
+ * AI Provider abstraction for LocatAI
149
+ * Supports multiple AI backends: OpenAI, Anthropic, Google, Local (Ollama)
150
+ */
151
+
152
+ type ProviderName = "openai" | "gpt" | "anthropic" | "claude" | "google" | "gemini" | "local" | "ollama";
153
+ interface GenerateLocatAIPlanInput {
154
+ systemPrompt: string;
155
+ userContent: string;
156
+ jsonSchema: object;
157
+ }
158
+ interface TokenUsage {
159
+ inputTokens: number;
160
+ outputTokens: number;
161
+ totalTokens: number;
162
+ }
163
+ interface LocatAIPlanResult {
164
+ plan: LocatAIPlanT | null;
165
+ tokenUsage: TokenUsage | null;
166
+ }
167
+ interface AIProvider {
168
+ readonly name: ProviderName;
169
+ generateLocatAIPlan(input: GenerateLocatAIPlanInput): Promise<LocatAIPlanResult>;
170
+ }
171
+ declare const DEFAULT_MODELS: Record<ProviderName, string>;
172
+
173
+ export { type AIProvider, type ClickOptions, DEFAULT_MODELS, LocatAIError, type LocatAIErrorContext, type LocatAILocator, type LocatAIMethods, type LocatAIOptions, type LocatAIPage, type ProviderName, createLocatAIFixture, withLocatAI };