@xylentis/testgen 0.0.0-stage → 1.1.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/CHANGELOG.md +11 -0
- package/LICENSE +661 -0
- package/NOTICE +29 -0
- package/README.md +195 -2
- package/dist/cli.js +158 -0
- package/dist/generate.js +47 -0
- package/dist/generators/index.js +12 -0
- package/dist/generators/javascript-body.js +102 -0
- package/dist/generators/playwright-go.js +388 -0
- package/dist/generators/playwright-ts.js +50 -0
- package/dist/generators/runtime.js +16 -0
- package/dist/generators/selenium-common.js +38 -0
- package/dist/generators/selenium-java.js +257 -0
- package/dist/generators/selenium-js.js +57 -0
- package/dist/generators/types.js +2 -0
- package/dist/generators/writer.js +38 -0
- package/dist/locator.js +49 -0
- package/dist/naming.js +31 -0
- package/dist/record.js +23 -0
- package/dist/recorder-process.js +102 -0
- package/dist/recording.js +82 -0
- package/dist/ui/server.js +255 -0
- package/dist/vendor/playwright/LICENSE +202 -0
- package/dist/vendor/playwright/cssParser.js +241 -0
- package/dist/vendor/playwright/cssTokenizer.js +950 -0
- package/dist/vendor/playwright/locatorGenerators.js +727 -0
- package/dist/vendor/playwright/selectorParser.js +427 -0
- package/dist/vendor/playwright/stringUtils.js +195 -0
- package/docs/testgen-ui.png +0 -0
- package/package.json +50 -4
- package/runtime/selenium/engine.js +426 -0
- package/runtime/selenium-java/TestgenSupport.java +941 -0
- package/runtime/selenium-js/testgen-support.js +618 -0
- package/ui/app.js +259 -0
- package/ui/index.html +65 -0
- package/ui/style.css +189 -0
|
@@ -0,0 +1,727 @@
|
|
|
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 { parseAttributeSelector, parseSelector, stringifySelector } from './selectorParser.js';
|
|
17
|
+
import { escapeWithQuotes, normalizeEscapedRegexQuotes, toSnakeCase, toTitleCase } from './stringUtils.js';
|
|
18
|
+
export function asLocatorDescription(lang, selector) {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = parseSelector(selector);
|
|
21
|
+
const customDescription = parseCustomDescription(parsed);
|
|
22
|
+
if (customDescription)
|
|
23
|
+
return customDescription;
|
|
24
|
+
return innerAsLocators(new generators[lang](), parsed, false, 1)[0];
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
// Tolerate invalid input.
|
|
28
|
+
return selector;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function locatorCustomDescription(selector) {
|
|
32
|
+
try {
|
|
33
|
+
const parsed = parseSelector(selector);
|
|
34
|
+
return parseCustomDescription(parsed);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function parseCustomDescription(parsed) {
|
|
41
|
+
const lastPart = parsed.parts[parsed.parts.length - 1];
|
|
42
|
+
if (lastPart?.name === 'internal:describe') {
|
|
43
|
+
const description = JSON.parse(lastPart.body);
|
|
44
|
+
if (typeof description === 'string')
|
|
45
|
+
return description;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
export function asLocator(lang, selector, isFrameLocator = false) {
|
|
50
|
+
return asLocators(lang, selector, isFrameLocator, 1)[0];
|
|
51
|
+
}
|
|
52
|
+
export function asLocators(lang, selector, isFrameLocator = false, maxOutputSize = 20, preferredQuote) {
|
|
53
|
+
try {
|
|
54
|
+
return innerAsLocators(new generators[lang](preferredQuote), parseSelector(selector), isFrameLocator, maxOutputSize);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
// Tolerate invalid input.
|
|
58
|
+
return [selector];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function innerAsLocators(factory, parsed, isFrameLocator = false, maxOutputSize = 20) {
|
|
62
|
+
const parts = [...parsed.parts];
|
|
63
|
+
const tokens = [];
|
|
64
|
+
let nextBase = isFrameLocator ? 'frame-locator' : 'page';
|
|
65
|
+
for (let index = 0; index < parts.length; index++) {
|
|
66
|
+
const part = parts[index];
|
|
67
|
+
const base = nextBase;
|
|
68
|
+
nextBase = 'locator';
|
|
69
|
+
if (part.name === 'internal:describe')
|
|
70
|
+
continue;
|
|
71
|
+
if (part.name === 'nth') {
|
|
72
|
+
if (part.body === '0')
|
|
73
|
+
tokens.push([factory.generateLocator(base, 'first', ''), factory.generateLocator(base, 'nth', '0')]);
|
|
74
|
+
else if (part.body === '-1')
|
|
75
|
+
tokens.push([factory.generateLocator(base, 'last', ''), factory.generateLocator(base, 'nth', '-1')]);
|
|
76
|
+
else
|
|
77
|
+
tokens.push([factory.generateLocator(base, 'nth', part.body)]);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (part.name === 'visible') {
|
|
81
|
+
const tokenList = [];
|
|
82
|
+
if (part.body === 'true')
|
|
83
|
+
tokenList.push(factory.generateLocator(base, 'visible', ''));
|
|
84
|
+
tokenList.push(factory.generateLocator(base, 'filter-visible', part.body), factory.generateLocator(base, 'default', `visible=${part.body}`));
|
|
85
|
+
tokens.push(tokenList);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (part.name === 'internal:text') {
|
|
89
|
+
const { exact, text } = detectExact(part.body);
|
|
90
|
+
tokens.push([factory.generateLocator(base, 'text', text, { exact })]);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (part.name === 'internal:has-text') {
|
|
94
|
+
const { exact, text } = detectExact(part.body);
|
|
95
|
+
// There is no locator equivalent for strict has-text, leave it as is.
|
|
96
|
+
if (!exact) {
|
|
97
|
+
tokens.push([factory.generateLocator(base, 'has-text', text, { exact })]);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (part.name === 'internal:has-not-text') {
|
|
102
|
+
const { exact, text } = detectExact(part.body);
|
|
103
|
+
// There is no locator equivalent for strict has-not-text, leave it as is.
|
|
104
|
+
if (!exact) {
|
|
105
|
+
tokens.push([factory.generateLocator(base, 'has-not-text', text, { exact })]);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (part.name === 'internal:has') {
|
|
110
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
111
|
+
tokens.push(inners.map(inner => factory.generateLocator(base, 'has', inner)));
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (part.name === 'internal:has-not') {
|
|
115
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
116
|
+
tokens.push(inners.map(inner => factory.generateLocator(base, 'hasNot', inner)));
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (part.name === 'internal:and') {
|
|
120
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
121
|
+
tokens.push(inners.map(inner => factory.generateLocator(base, 'and', inner)));
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (part.name === 'internal:or') {
|
|
125
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
126
|
+
tokens.push(inners.map(inner => factory.generateLocator(base, 'or', inner)));
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (part.name === 'internal:chain') {
|
|
130
|
+
const inners = innerAsLocators(factory, part.body.parsed, false, maxOutputSize);
|
|
131
|
+
tokens.push(inners.map(inner => factory.generateLocator(base, 'chain', inner)));
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (part.name === 'internal:label') {
|
|
135
|
+
const { exact, text } = detectExact(part.body);
|
|
136
|
+
tokens.push([factory.generateLocator(base, 'label', text, { exact })]);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (part.name === 'internal:role') {
|
|
140
|
+
const attrSelector = parseAttributeSelector(part.body, true);
|
|
141
|
+
const options = { attrs: [] };
|
|
142
|
+
for (const attr of attrSelector.attributes) {
|
|
143
|
+
if (attr.name === 'name') {
|
|
144
|
+
if (options.exact !== undefined && options.exact !== attr.caseSensitive)
|
|
145
|
+
throw new Error(`Conflicting exactness in internal:role selector: ${stringifySelector({ parts: [part] })}`);
|
|
146
|
+
options.exact = attr.caseSensitive;
|
|
147
|
+
options.name = attr.value;
|
|
148
|
+
}
|
|
149
|
+
else if (attr.name === 'description') {
|
|
150
|
+
if (options.exact !== undefined && options.exact !== attr.caseSensitive)
|
|
151
|
+
throw new Error(`Conflicting exactness in internal:role selector: ${stringifySelector({ parts: [part] })}`);
|
|
152
|
+
options.exact = attr.caseSensitive;
|
|
153
|
+
options.description = attr.value;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
if (attr.name === 'level' && typeof attr.value === 'string')
|
|
157
|
+
attr.value = +attr.value;
|
|
158
|
+
options.attrs.push({ name: attr.name === 'include-hidden' ? 'includeHidden' : attr.name, value: attr.value });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
tokens.push([factory.generateLocator(base, 'role', attrSelector.name, options)]);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (part.name === 'internal:testid') {
|
|
165
|
+
const attrSelector = parseAttributeSelector(part.body, true);
|
|
166
|
+
const { value } = attrSelector.attributes[0];
|
|
167
|
+
tokens.push([factory.generateLocator(base, 'test-id', value)]);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (part.name === 'internal:attr') {
|
|
171
|
+
const attrSelector = parseAttributeSelector(part.body, true);
|
|
172
|
+
const { name, value, caseSensitive } = attrSelector.attributes[0];
|
|
173
|
+
const text = value;
|
|
174
|
+
const exact = !!caseSensitive;
|
|
175
|
+
if (name === 'placeholder') {
|
|
176
|
+
tokens.push([factory.generateLocator(base, 'placeholder', text, { exact })]);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (name === 'alt') {
|
|
180
|
+
tokens.push([factory.generateLocator(base, 'alt', text, { exact })]);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (name === 'title') {
|
|
184
|
+
tokens.push([factory.generateLocator(base, 'title', text, { exact })]);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (part.name === 'internal:control' && part.body === 'any-frame') {
|
|
189
|
+
tokens.push([factory.generateLocator(base, 'any-frame', '')]);
|
|
190
|
+
nextBase = 'frame-locator';
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (part.name === 'internal:control' && part.body === 'enter-frame') {
|
|
194
|
+
// transform last tokens from `${selector}` into `${selector}.contentFrame()` and `frameLocator(${selector})`
|
|
195
|
+
const lastTokens = tokens[tokens.length - 1];
|
|
196
|
+
const lastPart = parts[index - 1];
|
|
197
|
+
const transformed = lastTokens.map(token => factory.chainLocators([token, factory.generateLocator(base, 'frame', '')]));
|
|
198
|
+
if (['xpath', 'css'].includes(lastPart.name)) {
|
|
199
|
+
transformed.push(factory.generateLocator(base, 'frame-locator', stringifySelector({ parts: [lastPart] })), factory.generateLocator(base, 'frame-locator', stringifySelector({ parts: [lastPart] }, true)));
|
|
200
|
+
}
|
|
201
|
+
lastTokens.splice(0, lastTokens.length, ...transformed);
|
|
202
|
+
nextBase = 'frame-locator';
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const nextPart = parts[index + 1];
|
|
206
|
+
const selectorPart = stringifySelector({ parts: [part] });
|
|
207
|
+
const locatorPart = factory.generateLocator(base, 'default', selectorPart);
|
|
208
|
+
if (nextPart && ['internal:has-text', 'internal:has-not-text'].includes(nextPart.name)) {
|
|
209
|
+
const { exact, text } = detectExact(nextPart.body);
|
|
210
|
+
// There is no locator equivalent for strict has-text and has-not-text, leave it as is.
|
|
211
|
+
if (!exact) {
|
|
212
|
+
const nextLocatorPart = factory.generateLocator('locator', nextPart.name === 'internal:has-text' ? 'has-text' : 'has-not-text', text, { exact });
|
|
213
|
+
const options = {};
|
|
214
|
+
if (nextPart.name === 'internal:has-text')
|
|
215
|
+
options.hasText = text;
|
|
216
|
+
else
|
|
217
|
+
options.hasNotText = text;
|
|
218
|
+
const combinedPart = factory.generateLocator(base, 'default', selectorPart, options);
|
|
219
|
+
// Two options:
|
|
220
|
+
// - locator('div').filter({ hasText: 'foo' })
|
|
221
|
+
// - locator('div', { hasText: 'foo' })
|
|
222
|
+
tokens.push([factory.chainLocators([locatorPart, nextLocatorPart]), combinedPart]);
|
|
223
|
+
index++;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Selectors can be prefixed with engine name, e.g. xpath=//foo
|
|
228
|
+
let locatorPartWithEngine;
|
|
229
|
+
if (['xpath', 'css'].includes(part.name)) {
|
|
230
|
+
const selectorPart = stringifySelector({ parts: [part] }, /* forceEngineName */ true);
|
|
231
|
+
locatorPartWithEngine = factory.generateLocator(base, 'default', selectorPart);
|
|
232
|
+
}
|
|
233
|
+
tokens.push([locatorPart, locatorPartWithEngine].filter(Boolean));
|
|
234
|
+
}
|
|
235
|
+
return combineTokens(factory, tokens, maxOutputSize);
|
|
236
|
+
}
|
|
237
|
+
function combineTokens(factory, tokens, maxOutputSize) {
|
|
238
|
+
const currentTokens = tokens.map(() => '');
|
|
239
|
+
const result = [];
|
|
240
|
+
const visit = (index) => {
|
|
241
|
+
if (index === tokens.length) {
|
|
242
|
+
result.push(factory.chainLocators(currentTokens));
|
|
243
|
+
return result.length < maxOutputSize;
|
|
244
|
+
}
|
|
245
|
+
for (const taken of tokens[index]) {
|
|
246
|
+
currentTokens[index] = taken;
|
|
247
|
+
if (!visit(index + 1))
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
};
|
|
252
|
+
visit(0);
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
function detectExact(text) {
|
|
256
|
+
let exact = false;
|
|
257
|
+
const match = text.match(/^\/(.*)\/([igm]*)$/);
|
|
258
|
+
if (match)
|
|
259
|
+
return { text: new RegExp(match[1], match[2]) };
|
|
260
|
+
if (text.endsWith('"')) {
|
|
261
|
+
text = JSON.parse(text);
|
|
262
|
+
exact = true;
|
|
263
|
+
}
|
|
264
|
+
else if (text.endsWith('"s')) {
|
|
265
|
+
text = JSON.parse(text.substring(0, text.length - 1));
|
|
266
|
+
exact = true;
|
|
267
|
+
}
|
|
268
|
+
else if (text.endsWith('"i')) {
|
|
269
|
+
text = JSON.parse(text.substring(0, text.length - 1));
|
|
270
|
+
exact = false;
|
|
271
|
+
}
|
|
272
|
+
return { exact, text };
|
|
273
|
+
}
|
|
274
|
+
export class JavaScriptLocatorFactory {
|
|
275
|
+
preferredQuote;
|
|
276
|
+
constructor(preferredQuote) {
|
|
277
|
+
this.preferredQuote = preferredQuote;
|
|
278
|
+
}
|
|
279
|
+
generateLocator(base, kind, body, options = {}) {
|
|
280
|
+
switch (kind) {
|
|
281
|
+
case 'default':
|
|
282
|
+
if (options.hasText !== undefined)
|
|
283
|
+
return `locator(${this.quote(body)}, { hasText: ${this.toHasText(options.hasText)} })`;
|
|
284
|
+
if (options.hasNotText !== undefined)
|
|
285
|
+
return `locator(${this.quote(body)}, { hasNotText: ${this.toHasText(options.hasNotText)} })`;
|
|
286
|
+
return `locator(${this.quote(body)})`;
|
|
287
|
+
case 'frame-locator':
|
|
288
|
+
return `frameLocator(${this.quote(body)})`;
|
|
289
|
+
case 'frame':
|
|
290
|
+
return `contentFrame()`;
|
|
291
|
+
case 'any-frame':
|
|
292
|
+
return `frameLocator()`;
|
|
293
|
+
case 'nth':
|
|
294
|
+
return `nth(${body})`;
|
|
295
|
+
case 'first':
|
|
296
|
+
return `first()`;
|
|
297
|
+
case 'last':
|
|
298
|
+
return `last()`;
|
|
299
|
+
case 'visible':
|
|
300
|
+
return `visible()`;
|
|
301
|
+
case 'filter-visible':
|
|
302
|
+
return `filter({ visible: ${body === 'true' ? 'true' : 'false'} })`;
|
|
303
|
+
case 'role':
|
|
304
|
+
const attrs = [];
|
|
305
|
+
if (isRegExp(options.name))
|
|
306
|
+
attrs.push(`name: ${this.regexToSourceString(options.name)}`);
|
|
307
|
+
else if (typeof options.name === 'string')
|
|
308
|
+
attrs.push(`name: ${this.quote(options.name)}`);
|
|
309
|
+
if (isRegExp(options.description))
|
|
310
|
+
attrs.push(`description: ${this.regexToSourceString(options.description)}`);
|
|
311
|
+
else if (typeof options.description === 'string')
|
|
312
|
+
attrs.push(`description: ${this.quote(options.description)}`);
|
|
313
|
+
if (options.exact && (typeof options.name === 'string' || typeof options.description === 'string'))
|
|
314
|
+
attrs.push(`exact: true`);
|
|
315
|
+
for (const { name, value } of options.attrs)
|
|
316
|
+
attrs.push(`${name}: ${typeof value === 'string' ? this.quote(value) : value}`);
|
|
317
|
+
const attrString = attrs.length ? `, { ${attrs.join(', ')} }` : '';
|
|
318
|
+
return `getByRole(${this.quote(body)}${attrString})`;
|
|
319
|
+
case 'has-text':
|
|
320
|
+
return `filter({ hasText: ${this.toHasText(body)} })`;
|
|
321
|
+
case 'has-not-text':
|
|
322
|
+
return `filter({ hasNotText: ${this.toHasText(body)} })`;
|
|
323
|
+
case 'has':
|
|
324
|
+
return `filter({ has: ${body} })`;
|
|
325
|
+
case 'hasNot':
|
|
326
|
+
return `filter({ hasNot: ${body} })`;
|
|
327
|
+
case 'and':
|
|
328
|
+
return `and(${body})`;
|
|
329
|
+
case 'or':
|
|
330
|
+
return `or(${body})`;
|
|
331
|
+
case 'chain':
|
|
332
|
+
return `locator(${body})`;
|
|
333
|
+
case 'test-id':
|
|
334
|
+
return `getByTestId(${this.toTestIdValue(body)})`;
|
|
335
|
+
case 'text':
|
|
336
|
+
return this.toCallWithExact('getByText', body, !!options.exact);
|
|
337
|
+
case 'alt':
|
|
338
|
+
return this.toCallWithExact('getByAltText', body, !!options.exact);
|
|
339
|
+
case 'placeholder':
|
|
340
|
+
return this.toCallWithExact('getByPlaceholder', body, !!options.exact);
|
|
341
|
+
case 'label':
|
|
342
|
+
return this.toCallWithExact('getByLabel', body, !!options.exact);
|
|
343
|
+
case 'title':
|
|
344
|
+
return this.toCallWithExact('getByTitle', body, !!options.exact);
|
|
345
|
+
default:
|
|
346
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
chainLocators(locators) {
|
|
350
|
+
return locators.join('.');
|
|
351
|
+
}
|
|
352
|
+
regexToSourceString(re) {
|
|
353
|
+
return normalizeEscapedRegexQuotes(String(re));
|
|
354
|
+
}
|
|
355
|
+
toCallWithExact(method, body, exact) {
|
|
356
|
+
if (isRegExp(body))
|
|
357
|
+
return `${method}(${this.regexToSourceString(body)})`;
|
|
358
|
+
return exact ? `${method}(${this.quote(body)}, { exact: true })` : `${method}(${this.quote(body)})`;
|
|
359
|
+
}
|
|
360
|
+
toHasText(body) {
|
|
361
|
+
if (isRegExp(body))
|
|
362
|
+
return this.regexToSourceString(body);
|
|
363
|
+
return this.quote(body);
|
|
364
|
+
}
|
|
365
|
+
toTestIdValue(value) {
|
|
366
|
+
if (isRegExp(value))
|
|
367
|
+
return this.regexToSourceString(value);
|
|
368
|
+
return this.quote(value);
|
|
369
|
+
}
|
|
370
|
+
quote(text) {
|
|
371
|
+
return escapeWithQuotes(text, this.preferredQuote ?? '\'');
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
export class PythonLocatorFactory {
|
|
375
|
+
generateLocator(base, kind, body, options = {}) {
|
|
376
|
+
switch (kind) {
|
|
377
|
+
case 'default':
|
|
378
|
+
if (options.hasText !== undefined)
|
|
379
|
+
return `locator(${this.quote(body)}, has_text=${this.toHasText(options.hasText)})`;
|
|
380
|
+
if (options.hasNotText !== undefined)
|
|
381
|
+
return `locator(${this.quote(body)}, has_not_text=${this.toHasText(options.hasNotText)})`;
|
|
382
|
+
return `locator(${this.quote(body)})`;
|
|
383
|
+
case 'frame-locator':
|
|
384
|
+
return `frame_locator(${this.quote(body)})`;
|
|
385
|
+
case 'frame':
|
|
386
|
+
return `content_frame`;
|
|
387
|
+
case 'any-frame':
|
|
388
|
+
return `frame_locator()`;
|
|
389
|
+
case 'nth':
|
|
390
|
+
return `nth(${body})`;
|
|
391
|
+
case 'first':
|
|
392
|
+
return `first`;
|
|
393
|
+
case 'last':
|
|
394
|
+
return `last`;
|
|
395
|
+
case 'visible':
|
|
396
|
+
return `visible`;
|
|
397
|
+
case 'filter-visible':
|
|
398
|
+
return `filter(visible=${body === 'true' ? 'True' : 'False'})`;
|
|
399
|
+
case 'role':
|
|
400
|
+
const attrs = [];
|
|
401
|
+
if (isRegExp(options.name))
|
|
402
|
+
attrs.push(`name=${this.regexToString(options.name)}`);
|
|
403
|
+
else if (typeof options.name === 'string')
|
|
404
|
+
attrs.push(`name=${this.quote(options.name)}`);
|
|
405
|
+
if (isRegExp(options.description))
|
|
406
|
+
attrs.push(`description=${this.regexToString(options.description)}`);
|
|
407
|
+
else if (typeof options.description === 'string')
|
|
408
|
+
attrs.push(`description=${this.quote(options.description)}`);
|
|
409
|
+
if (options.exact && (typeof options.name === 'string' || typeof options.description === 'string'))
|
|
410
|
+
attrs.push(`exact=True`);
|
|
411
|
+
for (const { name, value } of options.attrs) {
|
|
412
|
+
let valueString = typeof value === 'string' ? this.quote(value) : value;
|
|
413
|
+
if (typeof value === 'boolean')
|
|
414
|
+
valueString = value ? 'True' : 'False';
|
|
415
|
+
attrs.push(`${toSnakeCase(name)}=${valueString}`);
|
|
416
|
+
}
|
|
417
|
+
const attrString = attrs.length ? `, ${attrs.join(', ')}` : '';
|
|
418
|
+
return `get_by_role(${this.quote(body)}${attrString})`;
|
|
419
|
+
case 'has-text':
|
|
420
|
+
return `filter(has_text=${this.toHasText(body)})`;
|
|
421
|
+
case 'has-not-text':
|
|
422
|
+
return `filter(has_not_text=${this.toHasText(body)})`;
|
|
423
|
+
case 'has':
|
|
424
|
+
return `filter(has=${body})`;
|
|
425
|
+
case 'hasNot':
|
|
426
|
+
return `filter(has_not=${body})`;
|
|
427
|
+
case 'and':
|
|
428
|
+
return `and_(${body})`;
|
|
429
|
+
case 'or':
|
|
430
|
+
return `or_(${body})`;
|
|
431
|
+
case 'chain':
|
|
432
|
+
return `locator(${body})`;
|
|
433
|
+
case 'test-id':
|
|
434
|
+
return `get_by_test_id(${this.toTestIdValue(body)})`;
|
|
435
|
+
case 'text':
|
|
436
|
+
return this.toCallWithExact('get_by_text', body, !!options.exact);
|
|
437
|
+
case 'alt':
|
|
438
|
+
return this.toCallWithExact('get_by_alt_text', body, !!options.exact);
|
|
439
|
+
case 'placeholder':
|
|
440
|
+
return this.toCallWithExact('get_by_placeholder', body, !!options.exact);
|
|
441
|
+
case 'label':
|
|
442
|
+
return this.toCallWithExact('get_by_label', body, !!options.exact);
|
|
443
|
+
case 'title':
|
|
444
|
+
return this.toCallWithExact('get_by_title', body, !!options.exact);
|
|
445
|
+
default:
|
|
446
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
chainLocators(locators) {
|
|
450
|
+
return locators.join('.');
|
|
451
|
+
}
|
|
452
|
+
regexToString(body) {
|
|
453
|
+
const suffix = body.flags.includes('i') ? ', re.IGNORECASE' : '';
|
|
454
|
+
return `re.compile(r"${normalizeEscapedRegexQuotes(body.source).replace(/\\\//, '/').replace(/"/g, '\\"')}"${suffix})`;
|
|
455
|
+
}
|
|
456
|
+
toCallWithExact(method, body, exact) {
|
|
457
|
+
if (isRegExp(body))
|
|
458
|
+
return `${method}(${this.regexToString(body)})`;
|
|
459
|
+
if (exact)
|
|
460
|
+
return `${method}(${this.quote(body)}, exact=True)`;
|
|
461
|
+
return `${method}(${this.quote(body)})`;
|
|
462
|
+
}
|
|
463
|
+
toHasText(body) {
|
|
464
|
+
if (isRegExp(body))
|
|
465
|
+
return this.regexToString(body);
|
|
466
|
+
return `${this.quote(body)}`;
|
|
467
|
+
}
|
|
468
|
+
toTestIdValue(value) {
|
|
469
|
+
if (isRegExp(value))
|
|
470
|
+
return this.regexToString(value);
|
|
471
|
+
return this.quote(value);
|
|
472
|
+
}
|
|
473
|
+
quote(text) {
|
|
474
|
+
return escapeWithQuotes(text, '\"');
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
export class JavaLocatorFactory {
|
|
478
|
+
generateLocator(base, kind, body, options = {}) {
|
|
479
|
+
let clazz;
|
|
480
|
+
switch (base) {
|
|
481
|
+
case 'page':
|
|
482
|
+
clazz = 'Page';
|
|
483
|
+
break;
|
|
484
|
+
case 'frame-locator':
|
|
485
|
+
clazz = 'FrameLocator';
|
|
486
|
+
break;
|
|
487
|
+
case 'locator':
|
|
488
|
+
clazz = 'Locator';
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
switch (kind) {
|
|
492
|
+
case 'default':
|
|
493
|
+
if (options.hasText !== undefined)
|
|
494
|
+
return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasText(${this.toHasText(options.hasText)}))`;
|
|
495
|
+
if (options.hasNotText !== undefined)
|
|
496
|
+
return `locator(${this.quote(body)}, new ${clazz}.LocatorOptions().setHasNotText(${this.toHasText(options.hasNotText)}))`;
|
|
497
|
+
return `locator(${this.quote(body)})`;
|
|
498
|
+
case 'frame-locator':
|
|
499
|
+
return `frameLocator(${this.quote(body)})`;
|
|
500
|
+
case 'frame':
|
|
501
|
+
return `contentFrame()`;
|
|
502
|
+
case 'any-frame':
|
|
503
|
+
return `frameLocator()`;
|
|
504
|
+
case 'nth':
|
|
505
|
+
return `nth(${body})`;
|
|
506
|
+
case 'first':
|
|
507
|
+
return `first()`;
|
|
508
|
+
case 'last':
|
|
509
|
+
return `last()`;
|
|
510
|
+
case 'visible':
|
|
511
|
+
return `visible()`;
|
|
512
|
+
case 'filter-visible':
|
|
513
|
+
return `filter(new ${clazz}.FilterOptions().setVisible(${body === 'true' ? 'true' : 'false'}))`;
|
|
514
|
+
case 'role':
|
|
515
|
+
const attrs = [];
|
|
516
|
+
if (isRegExp(options.name))
|
|
517
|
+
attrs.push(`.setName(${this.regexToString(options.name)})`);
|
|
518
|
+
else if (typeof options.name === 'string')
|
|
519
|
+
attrs.push(`.setName(${this.quote(options.name)})`);
|
|
520
|
+
if (isRegExp(options.description))
|
|
521
|
+
attrs.push(`.setDescription(${this.regexToString(options.description)})`);
|
|
522
|
+
else if (typeof options.description === 'string')
|
|
523
|
+
attrs.push(`.setDescription(${this.quote(options.description)})`);
|
|
524
|
+
if (options.exact && (typeof options.name === 'string' || typeof options.description === 'string'))
|
|
525
|
+
attrs.push(`.setExact(true)`);
|
|
526
|
+
for (const { name, value } of options.attrs)
|
|
527
|
+
attrs.push(`.set${toTitleCase(name)}(${typeof value === 'string' ? this.quote(value) : value})`);
|
|
528
|
+
const attrString = attrs.length ? `, new ${clazz}.GetByRoleOptions()${attrs.join('')}` : '';
|
|
529
|
+
return `getByRole(AriaRole.${toSnakeCase(body).toUpperCase()}${attrString})`;
|
|
530
|
+
case 'has-text':
|
|
531
|
+
return `filter(new ${clazz}.FilterOptions().setHasText(${this.toHasText(body)}))`;
|
|
532
|
+
case 'has-not-text':
|
|
533
|
+
return `filter(new ${clazz}.FilterOptions().setHasNotText(${this.toHasText(body)}))`;
|
|
534
|
+
case 'has':
|
|
535
|
+
return `filter(new ${clazz}.FilterOptions().setHas(${body}))`;
|
|
536
|
+
case 'hasNot':
|
|
537
|
+
return `filter(new ${clazz}.FilterOptions().setHasNot(${body}))`;
|
|
538
|
+
case 'and':
|
|
539
|
+
return `and(${body})`;
|
|
540
|
+
case 'or':
|
|
541
|
+
return `or(${body})`;
|
|
542
|
+
case 'chain':
|
|
543
|
+
return `locator(${body})`;
|
|
544
|
+
case 'test-id':
|
|
545
|
+
return `getByTestId(${this.toTestIdValue(body)})`;
|
|
546
|
+
case 'text':
|
|
547
|
+
return this.toCallWithExact(clazz, 'getByText', body, !!options.exact);
|
|
548
|
+
case 'alt':
|
|
549
|
+
return this.toCallWithExact(clazz, 'getByAltText', body, !!options.exact);
|
|
550
|
+
case 'placeholder':
|
|
551
|
+
return this.toCallWithExact(clazz, 'getByPlaceholder', body, !!options.exact);
|
|
552
|
+
case 'label':
|
|
553
|
+
return this.toCallWithExact(clazz, 'getByLabel', body, !!options.exact);
|
|
554
|
+
case 'title':
|
|
555
|
+
return this.toCallWithExact(clazz, 'getByTitle', body, !!options.exact);
|
|
556
|
+
default:
|
|
557
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
chainLocators(locators) {
|
|
561
|
+
return locators.join('.');
|
|
562
|
+
}
|
|
563
|
+
regexToString(body) {
|
|
564
|
+
const suffix = body.flags.includes('i') ? ', Pattern.CASE_INSENSITIVE' : '';
|
|
565
|
+
return `Pattern.compile(${this.quote(normalizeEscapedRegexQuotes(body.source))}${suffix})`;
|
|
566
|
+
}
|
|
567
|
+
toCallWithExact(clazz, method, body, exact) {
|
|
568
|
+
if (isRegExp(body))
|
|
569
|
+
return `${method}(${this.regexToString(body)})`;
|
|
570
|
+
if (exact)
|
|
571
|
+
return `${method}(${this.quote(body)}, new ${clazz}.${toTitleCase(method)}Options().setExact(true))`;
|
|
572
|
+
return `${method}(${this.quote(body)})`;
|
|
573
|
+
}
|
|
574
|
+
toHasText(body) {
|
|
575
|
+
if (isRegExp(body))
|
|
576
|
+
return this.regexToString(body);
|
|
577
|
+
return this.quote(body);
|
|
578
|
+
}
|
|
579
|
+
toTestIdValue(value) {
|
|
580
|
+
if (isRegExp(value))
|
|
581
|
+
return this.regexToString(value);
|
|
582
|
+
return this.quote(value);
|
|
583
|
+
}
|
|
584
|
+
quote(text) {
|
|
585
|
+
return escapeWithQuotes(text, '\"');
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
export class CSharpLocatorFactory {
|
|
589
|
+
generateLocator(base, kind, body, options = {}) {
|
|
590
|
+
switch (kind) {
|
|
591
|
+
case 'default':
|
|
592
|
+
if (options.hasText !== undefined)
|
|
593
|
+
return `Locator(${this.quote(body)}, new() { ${this.toHasText(options.hasText)} })`;
|
|
594
|
+
if (options.hasNotText !== undefined)
|
|
595
|
+
return `Locator(${this.quote(body)}, new() { ${this.toHasNotText(options.hasNotText)} })`;
|
|
596
|
+
return `Locator(${this.quote(body)})`;
|
|
597
|
+
case 'frame-locator':
|
|
598
|
+
return `FrameLocator(${this.quote(body)})`;
|
|
599
|
+
case 'frame':
|
|
600
|
+
return `ContentFrame`;
|
|
601
|
+
case 'any-frame':
|
|
602
|
+
return `FrameLocator()`;
|
|
603
|
+
case 'nth':
|
|
604
|
+
return `Nth(${body})`;
|
|
605
|
+
case 'first':
|
|
606
|
+
return `First`;
|
|
607
|
+
case 'last':
|
|
608
|
+
return `Last`;
|
|
609
|
+
case 'visible':
|
|
610
|
+
return `Visible`;
|
|
611
|
+
case 'filter-visible':
|
|
612
|
+
return `Filter(new() { Visible = ${body === 'true' ? 'true' : 'false'} })`;
|
|
613
|
+
case 'role':
|
|
614
|
+
const attrs = [];
|
|
615
|
+
if (isRegExp(options.name))
|
|
616
|
+
attrs.push(`NameRegex = ${this.regexToString(options.name)}`);
|
|
617
|
+
else if (typeof options.name === 'string')
|
|
618
|
+
attrs.push(`Name = ${this.quote(options.name)}`);
|
|
619
|
+
if (isRegExp(options.description))
|
|
620
|
+
attrs.push(`DescriptionRegex = ${this.regexToString(options.description)}`);
|
|
621
|
+
else if (typeof options.description === 'string')
|
|
622
|
+
attrs.push(`Description = ${this.quote(options.description)}`);
|
|
623
|
+
if (options.exact && (typeof options.name === 'string' || typeof options.description === 'string'))
|
|
624
|
+
attrs.push(`Exact = true`);
|
|
625
|
+
for (const { name, value } of options.attrs)
|
|
626
|
+
attrs.push(`${toTitleCase(name)} = ${typeof value === 'string' ? this.quote(value) : value}`);
|
|
627
|
+
const attrString = attrs.length ? `, new() { ${attrs.join(', ')} }` : '';
|
|
628
|
+
return `GetByRole(AriaRole.${toTitleCase(body)}${attrString})`;
|
|
629
|
+
case 'has-text':
|
|
630
|
+
return `Filter(new() { ${this.toHasText(body)} })`;
|
|
631
|
+
case 'has-not-text':
|
|
632
|
+
return `Filter(new() { ${this.toHasNotText(body)} })`;
|
|
633
|
+
case 'has':
|
|
634
|
+
return `Filter(new() { Has = ${body} })`;
|
|
635
|
+
case 'hasNot':
|
|
636
|
+
return `Filter(new() { HasNot = ${body} })`;
|
|
637
|
+
case 'and':
|
|
638
|
+
return `And(${body})`;
|
|
639
|
+
case 'or':
|
|
640
|
+
return `Or(${body})`;
|
|
641
|
+
case 'chain':
|
|
642
|
+
return `Locator(${body})`;
|
|
643
|
+
case 'test-id':
|
|
644
|
+
return `GetByTestId(${this.toTestIdValue(body)})`;
|
|
645
|
+
case 'text':
|
|
646
|
+
return this.toCallWithExact('GetByText', body, !!options.exact);
|
|
647
|
+
case 'alt':
|
|
648
|
+
return this.toCallWithExact('GetByAltText', body, !!options.exact);
|
|
649
|
+
case 'placeholder':
|
|
650
|
+
return this.toCallWithExact('GetByPlaceholder', body, !!options.exact);
|
|
651
|
+
case 'label':
|
|
652
|
+
return this.toCallWithExact('GetByLabel', body, !!options.exact);
|
|
653
|
+
case 'title':
|
|
654
|
+
return this.toCallWithExact('GetByTitle', body, !!options.exact);
|
|
655
|
+
default:
|
|
656
|
+
throw new Error('Unknown selector kind ' + kind);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
chainLocators(locators) {
|
|
660
|
+
return locators.join('.');
|
|
661
|
+
}
|
|
662
|
+
regexToString(body) {
|
|
663
|
+
const suffix = body.flags.includes('i') ? ', RegexOptions.IgnoreCase' : '';
|
|
664
|
+
return `new Regex(${this.quote(normalizeEscapedRegexQuotes(body.source))}${suffix})`;
|
|
665
|
+
}
|
|
666
|
+
toCallWithExact(method, body, exact) {
|
|
667
|
+
if (isRegExp(body))
|
|
668
|
+
return `${method}(${this.regexToString(body)})`;
|
|
669
|
+
if (exact)
|
|
670
|
+
return `${method}(${this.quote(body)}, new() { Exact = true })`;
|
|
671
|
+
return `${method}(${this.quote(body)})`;
|
|
672
|
+
}
|
|
673
|
+
toHasText(body) {
|
|
674
|
+
if (isRegExp(body))
|
|
675
|
+
return `HasTextRegex = ${this.regexToString(body)}`;
|
|
676
|
+
return `HasText = ${this.quote(body)}`;
|
|
677
|
+
}
|
|
678
|
+
toTestIdValue(value) {
|
|
679
|
+
if (isRegExp(value))
|
|
680
|
+
return this.regexToString(value);
|
|
681
|
+
return this.quote(value);
|
|
682
|
+
}
|
|
683
|
+
toHasNotText(body) {
|
|
684
|
+
if (isRegExp(body))
|
|
685
|
+
return `HasNotTextRegex = ${this.regexToString(body)}`;
|
|
686
|
+
return `HasNotText = ${this.quote(body)}`;
|
|
687
|
+
}
|
|
688
|
+
quote(text) {
|
|
689
|
+
return escapeWithQuotes(text, '\"');
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
export class JsonlLocatorFactory {
|
|
693
|
+
generateLocator(base, kind, body, options = {}) {
|
|
694
|
+
return JSON.stringify({
|
|
695
|
+
kind,
|
|
696
|
+
body,
|
|
697
|
+
options,
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
chainLocators(locators) {
|
|
701
|
+
const objects = locators.map(l => JSON.parse(l));
|
|
702
|
+
for (let i = 0; i < objects.length - 1; ++i) {
|
|
703
|
+
// A locator may already be a chain, e.g. `contentFrame()` produces one. Append to its tail.
|
|
704
|
+
let tail = objects[i];
|
|
705
|
+
while (tail.next)
|
|
706
|
+
tail = tail.next;
|
|
707
|
+
tail.next = objects[i + 1];
|
|
708
|
+
}
|
|
709
|
+
return JSON.stringify(objects[0]);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
const generators = {
|
|
713
|
+
javascript: JavaScriptLocatorFactory,
|
|
714
|
+
python: PythonLocatorFactory,
|
|
715
|
+
java: JavaLocatorFactory,
|
|
716
|
+
csharp: CSharpLocatorFactory,
|
|
717
|
+
jsonl: JsonlLocatorFactory,
|
|
718
|
+
};
|
|
719
|
+
function isRegExp(obj) {
|
|
720
|
+
return obj instanceof RegExp;
|
|
721
|
+
}
|
|
722
|
+
// testgen: exposes the selector → locator decomposition to custom LocatorFactory
|
|
723
|
+
// implementations (upstream only accepts its built-in languages).
|
|
724
|
+
export function asLocatorWithFactory(factory, selector, isFrameLocator = false) {
|
|
725
|
+
return innerAsLocators(factory, parseSelector(selector), isFrameLocator, 1)[0];
|
|
726
|
+
}
|
|
727
|
+
//# sourceMappingURL=locatorGenerators.js.map
|