@browserbasehq/sdk 1.4.1 → 1.4.3-next.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/dist/index.d.ts +114 -0
- package/dist/index.js +3 -1
- package/dist/integrations/ai-sdk.d.ts +18 -0
- package/dist/integrations/ai-sdk.js +19 -0
- package/dist/recorder/call-metadata.d.ts +66 -0
- package/dist/recorder/call-metadata.js +2 -0
- package/dist/recorder/crypto.d.ts +1 -0
- package/dist/recorder/crypto.js +10 -0
- package/dist/recorder/css-utils.d.ts +2 -0
- package/dist/recorder/css-utils.js +34 -0
- package/dist/recorder/generator.d.ts +31 -0
- package/dist/recorder/generator.js +154 -0
- package/dist/recorder/javascript-generator.d.ts +29 -0
- package/dist/recorder/javascript-generator.js +256 -0
- package/dist/recorder/language.d.ts +127 -0
- package/dist/recorder/language.js +21 -0
- package/dist/recorder/recorder-types.d.ts +108 -0
- package/dist/recorder/recorder-types.js +2 -0
- package/dist/recorder/recorder.d.ts +35 -0
- package/dist/recorder/recorder.js +333 -0
- package/dist/recorder/time.d.ts +1 -0
- package/dist/recorder/time.js +42 -0
- package/dist/recorder/timeout-runner.d.ts +6 -0
- package/dist/recorder/timeout-runner.js +19 -0
- package/dist/recorder/utils/isomorphic/css-parser.d.ts +41 -0
- package/dist/recorder/utils/isomorphic/css-parser.js +291 -0
- package/dist/recorder/utils/isomorphic/css-tokenizer.d.ts +171 -0
- package/dist/recorder/utils/isomorphic/css-tokenizer.js +986 -0
- package/dist/recorder/utils/isomorphic/locator-generators.d.ts +79 -0
- package/dist/recorder/utils/isomorphic/locator-generators.js +666 -0
- package/dist/recorder/utils/isomorphic/locator-parser.d.ts +17 -0
- package/dist/recorder/utils/isomorphic/locator-parser.js +244 -0
- package/dist/recorder/utils/isomorphic/locator-utils.d.ts +43 -0
- package/dist/recorder/utils/isomorphic/locator-utils.js +69 -0
- package/dist/recorder/utils/isomorphic/selector-parser.d.ts +48 -0
- package/dist/recorder/utils/isomorphic/selector-parser.js +483 -0
- package/dist/recorder/utils/isomorphic/string-utils.d.ts +28 -0
- package/dist/recorder/utils/isomorphic/string-utils.js +123 -0
- package/dist/recorder/utils.d.ts +20 -0
- package/dist/recorder/utils.js +32 -0
- package/package.json +7 -3
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.JsonlLocatorFactory = exports.CSharpLocatorFactory = exports.JavaLocatorFactory = exports.PythonLocatorFactory = exports.JavaScriptLocatorFactory = void 0;
|
|
19
|
+
exports.asLocator = asLocator;
|
|
20
|
+
exports.asLocators = asLocators;
|
|
21
|
+
const string_utils_1 = require("./string-utils");
|
|
22
|
+
const selector_parser_1 = require("./selector-parser");
|
|
23
|
+
function asLocator(lang, selector, isFrameLocator = false) {
|
|
24
|
+
return asLocators(lang, selector, isFrameLocator)[0];
|
|
25
|
+
}
|
|
26
|
+
function asLocators(lang, selector, isFrameLocator = false, maxOutputSize = 20, preferredQuote) {
|
|
27
|
+
try {
|
|
28
|
+
return innerAsLocators(new generators[lang](preferredQuote), (0, selector_parser_1.parseSelector)(selector), isFrameLocator, maxOutputSize);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
// Tolerate invalid input.
|
|
32
|
+
return [selector];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function innerAsLocators(factory, parsed, isFrameLocator = false, maxOutputSize = 20) {
|
|
36
|
+
const parts = [...parsed.parts];
|
|
37
|
+
// frameLocator('iframe').first is actually "iframe >> nth=0 >> internal:control=enter-frame"
|
|
38
|
+
// To make it easier to parse, we turn it into "iframe >> internal:control=enter-frame >> nth=0"
|
|
39
|
+
for (let index = 0; index < parts.length - 1; index++) {
|
|
40
|
+
if (parts[index].name === 'nth' &&
|
|
41
|
+
parts[index + 1].name === 'internal:control' &&
|
|
42
|
+
parts[index + 1].body === 'enter-frame') {
|
|
43
|
+
// Swap nth and enter-frame.
|
|
44
|
+
const [nth] = parts.splice(index, 1);
|
|
45
|
+
parts.splice(index + 1, 0, nth);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const tokens = [];
|
|
49
|
+
let nextBase = isFrameLocator ? 'frame-locator' : 'page';
|
|
50
|
+
for (let index = 0; index < parts.length; index++) {
|
|
51
|
+
const part = parts[index];
|
|
52
|
+
const base = nextBase;
|
|
53
|
+
nextBase = 'locator';
|
|
54
|
+
if (part.name === 'nth') {
|
|
55
|
+
if (part.body === '0')
|
|
56
|
+
tokens.push([
|
|
57
|
+
factory.generateLocator(base, 'first', ''),
|
|
58
|
+
factory.generateLocator(base, 'nth', '0'),
|
|
59
|
+
]);
|
|
60
|
+
else if (part.body === '-1')
|
|
61
|
+
tokens.push([
|
|
62
|
+
factory.generateLocator(base, 'last', ''),
|
|
63
|
+
factory.generateLocator(base, 'nth', '-1'),
|
|
64
|
+
]);
|
|
65
|
+
else
|
|
66
|
+
tokens.push([factory.generateLocator(base, 'nth', part.body)]);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (part.name === 'internal:text') {
|
|
70
|
+
const { exact, text } = detectExact(part.body);
|
|
71
|
+
tokens.push([factory.generateLocator(base, 'text', text, { exact })]);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (part.name === 'internal:has-text') {
|
|
75
|
+
const { exact, text } = detectExact(part.body);
|
|
76
|
+
// There is no locator equivalent for strict has-text, leave it as is.
|
|
77
|
+
if (!exact) {
|
|
78
|
+
tokens.push([
|
|
79
|
+
factory.generateLocator(base, 'has-text', text, { exact }),
|
|
80
|
+
]);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (part.name === 'internal:has-not-text') {
|
|
85
|
+
const { exact, text } = detectExact(part.body);
|
|
86
|
+
// There is no locator equivalent for strict has-not-text, leave it as is.
|
|
87
|
+
if (!exact) {
|
|
88
|
+
tokens.push([
|
|
89
|
+
factory.generateLocator(base, 'has-not-text', text, { exact }),
|
|
90
|
+
]);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (part.name === 'internal:has') {
|
|
95
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
96
|
+
tokens.push(inners.map((inner) => factory.generateLocator(base, 'has', inner)));
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (part.name === 'internal:has-not') {
|
|
100
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
101
|
+
tokens.push(inners.map((inner) => factory.generateLocator(base, 'hasNot', inner)));
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (part.name === 'internal:and') {
|
|
105
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
106
|
+
tokens.push(inners.map((inner) => factory.generateLocator(base, 'and', inner)));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (part.name === 'internal:or') {
|
|
110
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
111
|
+
tokens.push(inners.map((inner) => factory.generateLocator(base, 'or', inner)));
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (part.name === 'internal:chain') {
|
|
115
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
116
|
+
tokens.push(inners.map((inner) => factory.generateLocator(base, 'chain', inner)));
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (part.name === 'internal:label') {
|
|
120
|
+
const { exact, text } = detectExact(part.body);
|
|
121
|
+
tokens.push([factory.generateLocator(base, 'label', text, { exact })]);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (part.name === 'internal:role') {
|
|
125
|
+
const attrSelector = (0, selector_parser_1.parseAttributeSelector)(part.body, true);
|
|
126
|
+
const options = { attrs: [] };
|
|
127
|
+
for (const attr of attrSelector.attributes) {
|
|
128
|
+
if (attr.name === 'name') {
|
|
129
|
+
options.exact = attr.caseSensitive;
|
|
130
|
+
options.name = attr.value;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
if (attr.name === 'level' && typeof attr.value === 'string')
|
|
134
|
+
attr.value = +attr.value;
|
|
135
|
+
options.attrs.push({
|
|
136
|
+
name: attr.name === 'include-hidden' ? 'includeHidden' : attr.name,
|
|
137
|
+
value: attr.value,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
tokens.push([
|
|
142
|
+
factory.generateLocator(base, 'role', attrSelector.name, options),
|
|
143
|
+
]);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (part.name === 'internal:testid') {
|
|
147
|
+
const attrSelector = (0, selector_parser_1.parseAttributeSelector)(part.body, true);
|
|
148
|
+
const { value } = attrSelector.attributes[0];
|
|
149
|
+
tokens.push([factory.generateLocator(base, 'test-id', value)]);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (part.name === 'internal:attr') {
|
|
153
|
+
const attrSelector = (0, selector_parser_1.parseAttributeSelector)(part.body, true);
|
|
154
|
+
const { name, value, caseSensitive } = attrSelector.attributes[0];
|
|
155
|
+
const text = value;
|
|
156
|
+
const exact = !!caseSensitive;
|
|
157
|
+
if (name === 'placeholder') {
|
|
158
|
+
tokens.push([
|
|
159
|
+
factory.generateLocator(base, 'placeholder', text, { exact }),
|
|
160
|
+
]);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (name === 'alt') {
|
|
164
|
+
tokens.push([factory.generateLocator(base, 'alt', text, { exact })]);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (name === 'title') {
|
|
168
|
+
tokens.push([factory.generateLocator(base, 'title', text, { exact })]);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
let locatorType = 'default';
|
|
173
|
+
const nextPart = parts[index + 1];
|
|
174
|
+
if (nextPart &&
|
|
175
|
+
nextPart.name === 'internal:control' &&
|
|
176
|
+
nextPart.body === 'enter-frame') {
|
|
177
|
+
locatorType = 'frame';
|
|
178
|
+
nextBase = 'frame-locator';
|
|
179
|
+
index++;
|
|
180
|
+
}
|
|
181
|
+
const selectorPart = (0, selector_parser_1.stringifySelector)({ parts: [part] });
|
|
182
|
+
const locatorPart = factory.generateLocator(base, locatorType, selectorPart);
|
|
183
|
+
if (locatorType === 'default' &&
|
|
184
|
+
nextPart &&
|
|
185
|
+
['internal:has-text', 'internal:has-not-text'].includes(nextPart.name)) {
|
|
186
|
+
const { exact, text } = detectExact(nextPart.body);
|
|
187
|
+
// There is no locator equivalent for strict has-text and has-not-text, leave it as is.
|
|
188
|
+
if (!exact) {
|
|
189
|
+
const nextLocatorPart = factory.generateLocator('locator', nextPart.name === 'internal:has-text' ? 'has-text' : 'has-not-text', text, { exact });
|
|
190
|
+
const options = {};
|
|
191
|
+
if (nextPart.name === 'internal:has-text')
|
|
192
|
+
options.hasText = text;
|
|
193
|
+
else
|
|
194
|
+
options.hasNotText = text;
|
|
195
|
+
const combinedPart = factory.generateLocator(base, 'default', selectorPart, options);
|
|
196
|
+
// Two options:
|
|
197
|
+
// - locator('div').filter({ hasText: 'foo' })
|
|
198
|
+
// - locator('div', { hasText: 'foo' })
|
|
199
|
+
tokens.push([
|
|
200
|
+
factory.chainLocators([locatorPart, nextLocatorPart]),
|
|
201
|
+
combinedPart,
|
|
202
|
+
]);
|
|
203
|
+
index++;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Selectors can be prefixed with engine name, e.g. xpath=//foo
|
|
208
|
+
let locatorPartWithEngine;
|
|
209
|
+
if (['xpath', 'css'].includes(part.name)) {
|
|
210
|
+
const selectorPart = (0, selector_parser_1.stringifySelector)({ parts: [part] },
|
|
211
|
+
/* forceEngineName */ true);
|
|
212
|
+
locatorPartWithEngine = factory.generateLocator(base, locatorType, selectorPart);
|
|
213
|
+
}
|
|
214
|
+
tokens.push([locatorPart, locatorPartWithEngine].filter(Boolean));
|
|
215
|
+
}
|
|
216
|
+
return combineTokens(factory, tokens, maxOutputSize);
|
|
217
|
+
}
|
|
218
|
+
function combineTokens(factory, tokens, maxOutputSize) {
|
|
219
|
+
const currentTokens = tokens.map(() => '');
|
|
220
|
+
const result = [];
|
|
221
|
+
const visit = (index) => {
|
|
222
|
+
if (index === tokens.length) {
|
|
223
|
+
result.push(factory.chainLocators(currentTokens));
|
|
224
|
+
return currentTokens.length < maxOutputSize;
|
|
225
|
+
}
|
|
226
|
+
for (const taken of tokens[index]) {
|
|
227
|
+
currentTokens[index] = taken;
|
|
228
|
+
if (!visit(index + 1))
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
};
|
|
233
|
+
visit(0);
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
function detectExact(text) {
|
|
237
|
+
let exact = false;
|
|
238
|
+
const match = text.match(/^\/(.*)\/([igm]*)$/);
|
|
239
|
+
if (match)
|
|
240
|
+
return { text: new RegExp(match[1], match[2]) };
|
|
241
|
+
if (text.endsWith('"')) {
|
|
242
|
+
text = JSON.parse(text);
|
|
243
|
+
exact = true;
|
|
244
|
+
}
|
|
245
|
+
else if (text.endsWith('"s')) {
|
|
246
|
+
text = JSON.parse(text.substring(0, text.length - 1));
|
|
247
|
+
exact = true;
|
|
248
|
+
}
|
|
249
|
+
else if (text.endsWith('"i')) {
|
|
250
|
+
text = JSON.parse(text.substring(0, text.length - 1));
|
|
251
|
+
exact = false;
|
|
252
|
+
}
|
|
253
|
+
return { exact, text };
|
|
254
|
+
}
|
|
255
|
+
class JavaScriptLocatorFactory {
|
|
256
|
+
preferredQuote;
|
|
257
|
+
constructor(preferredQuote) {
|
|
258
|
+
this.preferredQuote = preferredQuote;
|
|
259
|
+
}
|
|
260
|
+
generateLocator(base, kind, body, options = {}) {
|
|
261
|
+
switch (kind) {
|
|
262
|
+
case 'default':
|
|
263
|
+
if (options.hasText !== undefined)
|
|
264
|
+
return `locator(${this.quote(body)}, { hasText: ${this.toHasText(options.hasText)} })`;
|
|
265
|
+
if (options.hasNotText !== undefined)
|
|
266
|
+
return `locator(${this.quote(body)}, { hasNotText: ${this.toHasText(options.hasNotText)} })`;
|
|
267
|
+
return `locator(${this.quote(body)})`;
|
|
268
|
+
case 'frame':
|
|
269
|
+
return `frameLocator(${this.quote(body)})`;
|
|
270
|
+
case 'nth':
|
|
271
|
+
return `nth(${body})`;
|
|
272
|
+
case 'first':
|
|
273
|
+
return `first()`;
|
|
274
|
+
case 'last':
|
|
275
|
+
return `last()`;
|
|
276
|
+
case 'role':
|
|
277
|
+
const attrs = [];
|
|
278
|
+
if (isRegExp(options.name)) {
|
|
279
|
+
attrs.push(`name: ${this.regexToSourceString(options.name)}`);
|
|
280
|
+
}
|
|
281
|
+
else if (typeof options.name === 'string') {
|
|
282
|
+
attrs.push(`name: ${this.quote(options.name)}`);
|
|
283
|
+
if (options.exact)
|
|
284
|
+
attrs.push(`exact: true`);
|
|
285
|
+
}
|
|
286
|
+
for (const { name, value } of options.attrs)
|
|
287
|
+
attrs.push(`${name}: ${typeof value === 'string' ? this.quote(value) : value}`);
|
|
288
|
+
const attrString = attrs.length ? `, { ${attrs.join(', ')} }` : '';
|
|
289
|
+
return `getByRole(${this.quote(body)}${attrString})`;
|
|
290
|
+
case 'has-text':
|
|
291
|
+
return `filter({ hasText: ${this.toHasText(body)} })`;
|
|
292
|
+
case 'has-not-text':
|
|
293
|
+
return `filter({ hasNotText: ${this.toHasText(body)} })`;
|
|
294
|
+
case 'has':
|
|
295
|
+
return `filter({ has: ${body} })`;
|
|
296
|
+
case 'hasNot':
|
|
297
|
+
return `filter({ hasNot: ${body} })`;
|
|
298
|
+
case 'and':
|
|
299
|
+
return `and(${body})`;
|
|
300
|
+
case 'or':
|
|
301
|
+
return `or(${body})`;
|
|
302
|
+
case 'chain':
|
|
303
|
+
return `locator(${body})`;
|
|
304
|
+
case 'test-id':
|
|
305
|
+
return `getByTestId(${this.toTestIdValue(body)})`;
|
|
306
|
+
case 'text':
|
|
307
|
+
return this.toCallWithExact('getByText', body, !!options.exact);
|
|
308
|
+
case 'alt':
|
|
309
|
+
return this.toCallWithExact('getByAltText', body, !!options.exact);
|
|
310
|
+
case 'placeholder':
|
|
311
|
+
return this.toCallWithExact('getByPlaceholder', body, !!options.exact);
|
|
312
|
+
case 'label':
|
|
313
|
+
return this.toCallWithExact('getByLabel', body, !!options.exact);
|
|
314
|
+
case 'title':
|
|
315
|
+
return this.toCallWithExact('getByTitle', body, !!options.exact);
|
|
316
|
+
default:
|
|
317
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
chainLocators(locators) {
|
|
321
|
+
return locators.join('.');
|
|
322
|
+
}
|
|
323
|
+
regexToSourceString(re) {
|
|
324
|
+
return (0, string_utils_1.normalizeEscapedRegexQuotes)(String(re));
|
|
325
|
+
}
|
|
326
|
+
toCallWithExact(method, body, exact) {
|
|
327
|
+
if (isRegExp(body))
|
|
328
|
+
return `${method}(${this.regexToSourceString(body)})`;
|
|
329
|
+
return exact
|
|
330
|
+
? `${method}(${this.quote(body)}, { exact: true })`
|
|
331
|
+
: `${method}(${this.quote(body)})`;
|
|
332
|
+
}
|
|
333
|
+
toHasText(body) {
|
|
334
|
+
if (isRegExp(body))
|
|
335
|
+
return this.regexToSourceString(body);
|
|
336
|
+
return this.quote(body);
|
|
337
|
+
}
|
|
338
|
+
toTestIdValue(value) {
|
|
339
|
+
if (isRegExp(value))
|
|
340
|
+
return this.regexToSourceString(value);
|
|
341
|
+
return this.quote(value);
|
|
342
|
+
}
|
|
343
|
+
quote(text) {
|
|
344
|
+
return (0, string_utils_1.escapeWithQuotes)(text, this.preferredQuote ?? "'");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
exports.JavaScriptLocatorFactory = JavaScriptLocatorFactory;
|
|
348
|
+
class PythonLocatorFactory {
|
|
349
|
+
generateLocator(base, kind, body, options = {}) {
|
|
350
|
+
switch (kind) {
|
|
351
|
+
case 'default':
|
|
352
|
+
if (options.hasText !== undefined)
|
|
353
|
+
return `locator(${this.quote(body)}, has_text=${this.toHasText(options.hasText)})`;
|
|
354
|
+
if (options.hasNotText !== undefined)
|
|
355
|
+
return `locator(${this.quote(body)}, has_not_text=${this.toHasText(options.hasNotText)})`;
|
|
356
|
+
return `locator(${this.quote(body)})`;
|
|
357
|
+
case 'frame':
|
|
358
|
+
return `frame_locator(${this.quote(body)})`;
|
|
359
|
+
case 'nth':
|
|
360
|
+
return `nth(${body})`;
|
|
361
|
+
case 'first':
|
|
362
|
+
return `first`;
|
|
363
|
+
case 'last':
|
|
364
|
+
return `last`;
|
|
365
|
+
case 'role':
|
|
366
|
+
const attrs = [];
|
|
367
|
+
if (isRegExp(options.name)) {
|
|
368
|
+
attrs.push(`name=${this.regexToString(options.name)}`);
|
|
369
|
+
}
|
|
370
|
+
else if (typeof options.name === 'string') {
|
|
371
|
+
attrs.push(`name=${this.quote(options.name)}`);
|
|
372
|
+
if (options.exact)
|
|
373
|
+
attrs.push(`exact=True`);
|
|
374
|
+
}
|
|
375
|
+
for (const { name, value } of options.attrs) {
|
|
376
|
+
let valueString = typeof value === 'string' ? this.quote(value) : value;
|
|
377
|
+
if (typeof value === 'boolean')
|
|
378
|
+
valueString = value ? 'True' : 'False';
|
|
379
|
+
attrs.push(`${(0, string_utils_1.toSnakeCase)(name)}=${valueString}`);
|
|
380
|
+
}
|
|
381
|
+
const attrString = attrs.length ? `, ${attrs.join(', ')}` : '';
|
|
382
|
+
return `get_by_role(${this.quote(body)}${attrString})`;
|
|
383
|
+
case 'has-text':
|
|
384
|
+
return `filter(has_text=${this.toHasText(body)})`;
|
|
385
|
+
case 'has-not-text':
|
|
386
|
+
return `filter(has_not_text=${this.toHasText(body)})`;
|
|
387
|
+
case 'has':
|
|
388
|
+
return `filter(has=${body})`;
|
|
389
|
+
case 'hasNot':
|
|
390
|
+
return `filter(has_not=${body})`;
|
|
391
|
+
case 'and':
|
|
392
|
+
return `and_(${body})`;
|
|
393
|
+
case 'or':
|
|
394
|
+
return `or_(${body})`;
|
|
395
|
+
case 'chain':
|
|
396
|
+
return `locator(${body})`;
|
|
397
|
+
case 'test-id':
|
|
398
|
+
return `get_by_test_id(${this.toTestIdValue(body)})`;
|
|
399
|
+
case 'text':
|
|
400
|
+
return this.toCallWithExact('get_by_text', body, !!options.exact);
|
|
401
|
+
case 'alt':
|
|
402
|
+
return this.toCallWithExact('get_by_alt_text', body, !!options.exact);
|
|
403
|
+
case 'placeholder':
|
|
404
|
+
return this.toCallWithExact('get_by_placeholder', body, !!options.exact);
|
|
405
|
+
case 'label':
|
|
406
|
+
return this.toCallWithExact('get_by_label', body, !!options.exact);
|
|
407
|
+
case 'title':
|
|
408
|
+
return this.toCallWithExact('get_by_title', body, !!options.exact);
|
|
409
|
+
default:
|
|
410
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
chainLocators(locators) {
|
|
414
|
+
return locators.join('.');
|
|
415
|
+
}
|
|
416
|
+
regexToString(body) {
|
|
417
|
+
const suffix = body.flags.includes('i') ? ', re.IGNORECASE' : '';
|
|
418
|
+
return `re.compile(r"${(0, string_utils_1.normalizeEscapedRegexQuotes)(body.source).replace(/\\\//, '/').replace(/"/g, '\\"')}"${suffix})`;
|
|
419
|
+
}
|
|
420
|
+
toCallWithExact(method, body, exact) {
|
|
421
|
+
if (isRegExp(body))
|
|
422
|
+
return `${method}(${this.regexToString(body)})`;
|
|
423
|
+
if (exact)
|
|
424
|
+
return `${method}(${this.quote(body)}, exact=True)`;
|
|
425
|
+
return `${method}(${this.quote(body)})`;
|
|
426
|
+
}
|
|
427
|
+
toHasText(body) {
|
|
428
|
+
if (isRegExp(body))
|
|
429
|
+
return this.regexToString(body);
|
|
430
|
+
return `${this.quote(body)}`;
|
|
431
|
+
}
|
|
432
|
+
toTestIdValue(value) {
|
|
433
|
+
if (isRegExp(value))
|
|
434
|
+
return this.regexToString(value);
|
|
435
|
+
return this.quote(value);
|
|
436
|
+
}
|
|
437
|
+
quote(text) {
|
|
438
|
+
return (0, string_utils_1.escapeWithQuotes)(text, '"');
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
exports.PythonLocatorFactory = PythonLocatorFactory;
|
|
442
|
+
class JavaLocatorFactory {
|
|
443
|
+
generateLocator(base, kind, body, options = {}) {
|
|
444
|
+
let clazz;
|
|
445
|
+
switch (base) {
|
|
446
|
+
case 'page':
|
|
447
|
+
clazz = 'Page';
|
|
448
|
+
break;
|
|
449
|
+
case 'frame-locator':
|
|
450
|
+
clazz = 'FrameLocator';
|
|
451
|
+
break;
|
|
452
|
+
case 'locator':
|
|
453
|
+
clazz = 'Locator';
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
switch (kind) {
|
|
457
|
+
case 'default':
|
|
458
|
+
if (options.hasText !== undefined)
|
|
459
|
+
return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasText(${this.toHasText(options.hasText)}))`;
|
|
460
|
+
if (options.hasNotText !== undefined)
|
|
461
|
+
return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasNotText(${this.toHasText(options.hasNotText)}))`;
|
|
462
|
+
return `locator(${this.quote(body)})`;
|
|
463
|
+
case 'frame':
|
|
464
|
+
return `frameLocator(${this.quote(body)})`;
|
|
465
|
+
case 'nth':
|
|
466
|
+
return `nth(${body})`;
|
|
467
|
+
case 'first':
|
|
468
|
+
return `first()`;
|
|
469
|
+
case 'last':
|
|
470
|
+
return `last()`;
|
|
471
|
+
case 'role':
|
|
472
|
+
const attrs = [];
|
|
473
|
+
if (isRegExp(options.name)) {
|
|
474
|
+
attrs.push(`.setName(${this.regexToString(options.name)})`);
|
|
475
|
+
}
|
|
476
|
+
else if (typeof options.name === 'string') {
|
|
477
|
+
attrs.push(`.setName(${this.quote(options.name)})`);
|
|
478
|
+
if (options.exact)
|
|
479
|
+
attrs.push(`.setExact(true)`);
|
|
480
|
+
}
|
|
481
|
+
for (const { name, value } of options.attrs)
|
|
482
|
+
attrs.push(`.set${(0, string_utils_1.toTitleCase)(name)}(${typeof value === 'string' ? this.quote(value) : value})`);
|
|
483
|
+
const attrString = attrs.length
|
|
484
|
+
? `, new ${clazz}.GetByRoleOptions()${attrs.join('')}`
|
|
485
|
+
: '';
|
|
486
|
+
return `getByRole(AriaRole.${(0, string_utils_1.toSnakeCase)(body).toUpperCase()}${attrString})`;
|
|
487
|
+
case 'has-text':
|
|
488
|
+
return `filter(new ${clazz}.FilterOptions().setHasText(${this.toHasText(body)}))`;
|
|
489
|
+
case 'has-not-text':
|
|
490
|
+
return `filter(new ${clazz}.FilterOptions().setHasNotText(${this.toHasText(body)}))`;
|
|
491
|
+
case 'has':
|
|
492
|
+
return `filter(new ${clazz}.FilterOptions().setHas(${body}))`;
|
|
493
|
+
case 'hasNot':
|
|
494
|
+
return `filter(new ${clazz}.FilterOptions().setHasNot(${body}))`;
|
|
495
|
+
case 'and':
|
|
496
|
+
return `and(${body})`;
|
|
497
|
+
case 'or':
|
|
498
|
+
return `or(${body})`;
|
|
499
|
+
case 'chain':
|
|
500
|
+
return `locator(${body})`;
|
|
501
|
+
case 'test-id':
|
|
502
|
+
return `getByTestId(${this.toTestIdValue(body)})`;
|
|
503
|
+
case 'text':
|
|
504
|
+
return this.toCallWithExact(clazz, 'getByText', body, !!options.exact);
|
|
505
|
+
case 'alt':
|
|
506
|
+
return this.toCallWithExact(clazz, 'getByAltText', body, !!options.exact);
|
|
507
|
+
case 'placeholder':
|
|
508
|
+
return this.toCallWithExact(clazz, 'getByPlaceholder', body, !!options.exact);
|
|
509
|
+
case 'label':
|
|
510
|
+
return this.toCallWithExact(clazz, 'getByLabel', body, !!options.exact);
|
|
511
|
+
case 'title':
|
|
512
|
+
return this.toCallWithExact(clazz, 'getByTitle', body, !!options.exact);
|
|
513
|
+
default:
|
|
514
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
chainLocators(locators) {
|
|
518
|
+
return locators.join('.');
|
|
519
|
+
}
|
|
520
|
+
regexToString(body) {
|
|
521
|
+
const suffix = body.flags.includes('i') ? ', Pattern.CASE_INSENSITIVE' : '';
|
|
522
|
+
return `Pattern.compile(${this.quote((0, string_utils_1.normalizeEscapedRegexQuotes)(body.source))}${suffix})`;
|
|
523
|
+
}
|
|
524
|
+
toCallWithExact(clazz, method, body, exact) {
|
|
525
|
+
if (isRegExp(body))
|
|
526
|
+
return `${method}(${this.regexToString(body)})`;
|
|
527
|
+
if (exact)
|
|
528
|
+
return `${method}(${this.quote(body)}, new ${clazz}.${(0, string_utils_1.toTitleCase)(method)}Options().setExact(true))`;
|
|
529
|
+
return `${method}(${this.quote(body)})`;
|
|
530
|
+
}
|
|
531
|
+
toHasText(body) {
|
|
532
|
+
if (isRegExp(body))
|
|
533
|
+
return this.regexToString(body);
|
|
534
|
+
return this.quote(body);
|
|
535
|
+
}
|
|
536
|
+
toTestIdValue(value) {
|
|
537
|
+
if (isRegExp(value))
|
|
538
|
+
return this.regexToString(value);
|
|
539
|
+
return this.quote(value);
|
|
540
|
+
}
|
|
541
|
+
quote(text) {
|
|
542
|
+
return (0, string_utils_1.escapeWithQuotes)(text, '"');
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
exports.JavaLocatorFactory = JavaLocatorFactory;
|
|
546
|
+
class CSharpLocatorFactory {
|
|
547
|
+
generateLocator(base, kind, body, options = {}) {
|
|
548
|
+
switch (kind) {
|
|
549
|
+
case 'default':
|
|
550
|
+
if (options.hasText !== undefined)
|
|
551
|
+
return `Locator(${this.quote(body)}, new() { ${this.toHasText(options.hasText)} })`;
|
|
552
|
+
if (options.hasNotText !== undefined)
|
|
553
|
+
return `Locator(${this.quote(body)}, new() { ${this.toHasNotText(options.hasNotText)} })`;
|
|
554
|
+
return `Locator(${this.quote(body)})`;
|
|
555
|
+
case 'frame':
|
|
556
|
+
return `FrameLocator(${this.quote(body)})`;
|
|
557
|
+
case 'nth':
|
|
558
|
+
return `Nth(${body})`;
|
|
559
|
+
case 'first':
|
|
560
|
+
return `First`;
|
|
561
|
+
case 'last':
|
|
562
|
+
return `Last`;
|
|
563
|
+
case 'role':
|
|
564
|
+
const attrs = [];
|
|
565
|
+
if (isRegExp(options.name)) {
|
|
566
|
+
attrs.push(`NameRegex = ${this.regexToString(options.name)}`);
|
|
567
|
+
}
|
|
568
|
+
else if (typeof options.name === 'string') {
|
|
569
|
+
attrs.push(`Name = ${this.quote(options.name)}`);
|
|
570
|
+
if (options.exact)
|
|
571
|
+
attrs.push(`Exact = true`);
|
|
572
|
+
}
|
|
573
|
+
for (const { name, value } of options.attrs)
|
|
574
|
+
attrs.push(`${(0, string_utils_1.toTitleCase)(name)} = ${typeof value === 'string' ? this.quote(value) : value}`);
|
|
575
|
+
const attrString = attrs.length ? `, new() { ${attrs.join(', ')} }` : '';
|
|
576
|
+
return `GetByRole(AriaRole.${(0, string_utils_1.toTitleCase)(body)}${attrString})`;
|
|
577
|
+
case 'has-text':
|
|
578
|
+
return `Filter(new() { ${this.toHasText(body)} })`;
|
|
579
|
+
case 'has-not-text':
|
|
580
|
+
return `Filter(new() { ${this.toHasNotText(body)} })`;
|
|
581
|
+
case 'has':
|
|
582
|
+
return `Filter(new() { Has = ${body} })`;
|
|
583
|
+
case 'hasNot':
|
|
584
|
+
return `Filter(new() { HasNot = ${body} })`;
|
|
585
|
+
case 'and':
|
|
586
|
+
return `And(${body})`;
|
|
587
|
+
case 'or':
|
|
588
|
+
return `Or(${body})`;
|
|
589
|
+
case 'chain':
|
|
590
|
+
return `Locator(${body})`;
|
|
591
|
+
case 'test-id':
|
|
592
|
+
return `GetByTestId(${this.toTestIdValue(body)})`;
|
|
593
|
+
case 'text':
|
|
594
|
+
return this.toCallWithExact('GetByText', body, !!options.exact);
|
|
595
|
+
case 'alt':
|
|
596
|
+
return this.toCallWithExact('GetByAltText', body, !!options.exact);
|
|
597
|
+
case 'placeholder':
|
|
598
|
+
return this.toCallWithExact('GetByPlaceholder', body, !!options.exact);
|
|
599
|
+
case 'label':
|
|
600
|
+
return this.toCallWithExact('GetByLabel', body, !!options.exact);
|
|
601
|
+
case 'title':
|
|
602
|
+
return this.toCallWithExact('GetByTitle', body, !!options.exact);
|
|
603
|
+
default:
|
|
604
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
chainLocators(locators) {
|
|
608
|
+
return locators.join('.');
|
|
609
|
+
}
|
|
610
|
+
regexToString(body) {
|
|
611
|
+
const suffix = body.flags.includes('i') ? ', RegexOptions.IgnoreCase' : '';
|
|
612
|
+
return `new Regex(${this.quote((0, string_utils_1.normalizeEscapedRegexQuotes)(body.source))}${suffix})`;
|
|
613
|
+
}
|
|
614
|
+
toCallWithExact(method, body, exact) {
|
|
615
|
+
if (isRegExp(body))
|
|
616
|
+
return `${method}(${this.regexToString(body)})`;
|
|
617
|
+
if (exact)
|
|
618
|
+
return `${method}(${this.quote(body)}, new() { Exact = true })`;
|
|
619
|
+
return `${method}(${this.quote(body)})`;
|
|
620
|
+
}
|
|
621
|
+
toHasText(body) {
|
|
622
|
+
if (isRegExp(body))
|
|
623
|
+
return `HasTextRegex = ${this.regexToString(body)}`;
|
|
624
|
+
return `HasText = ${this.quote(body)}`;
|
|
625
|
+
}
|
|
626
|
+
toTestIdValue(value) {
|
|
627
|
+
if (isRegExp(value))
|
|
628
|
+
return this.regexToString(value);
|
|
629
|
+
return this.quote(value);
|
|
630
|
+
}
|
|
631
|
+
toHasNotText(body) {
|
|
632
|
+
if (isRegExp(body))
|
|
633
|
+
return `HasNotTextRegex = ${this.regexToString(body)}`;
|
|
634
|
+
return `HasNotText = ${this.quote(body)}`;
|
|
635
|
+
}
|
|
636
|
+
quote(text) {
|
|
637
|
+
return (0, string_utils_1.escapeWithQuotes)(text, '"');
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
exports.CSharpLocatorFactory = CSharpLocatorFactory;
|
|
641
|
+
class JsonlLocatorFactory {
|
|
642
|
+
generateLocator(base, kind, body, options = {}) {
|
|
643
|
+
return JSON.stringify({
|
|
644
|
+
kind,
|
|
645
|
+
body,
|
|
646
|
+
options,
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
chainLocators(locators) {
|
|
650
|
+
const objects = locators.map((l) => JSON.parse(l));
|
|
651
|
+
for (let i = 0; i < objects.length - 1; ++i)
|
|
652
|
+
objects[i].next = objects[i + 1];
|
|
653
|
+
return JSON.stringify(objects[0]);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
exports.JsonlLocatorFactory = JsonlLocatorFactory;
|
|
657
|
+
const generators = {
|
|
658
|
+
javascript: JavaScriptLocatorFactory,
|
|
659
|
+
python: PythonLocatorFactory,
|
|
660
|
+
java: JavaLocatorFactory,
|
|
661
|
+
csharp: CSharpLocatorFactory,
|
|
662
|
+
jsonl: JsonlLocatorFactory,
|
|
663
|
+
};
|
|
664
|
+
function isRegExp(obj) {
|
|
665
|
+
return obj instanceof RegExp;
|
|
666
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { Language } from './locator-generators';
|
|
17
|
+
export declare function locatorOrSelectorAsSelector(language: Language, locator: string, testIdAttributeName: string): string;
|