-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Expand file tree
/
Copy pathlanguage.ts
More file actions
120 lines (112 loc) · 4 KB
/
Copy pathlanguage.ts
File metadata and controls
120 lines (112 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { BrowserContextOptions } from 'playwright-core';
import type * as types from './types';
import type { LanguageGenerator, LanguageGeneratorOptions } from './types';
import type * as actions from './actions';
export function generateCode(actions: actions.ActionInContext[], languageGenerator: LanguageGenerator, options: LanguageGeneratorOptions) {
languageGenerator.reset();
const header = languageGenerator.generateHeader(options);
const footer = languageGenerator.generateFooter(options.saveStorage);
const actionTexts = actions.map(a => languageGenerator.generateAction(a, options)).filter(Boolean);
const text = [header, ...actionTexts, footer].join('\n');
return { header, footer, actionTexts, text };
}
export function expectSignalAction(actionInContext: actions.ActionInContext, signal: actions.ExpectSignal): actions.ActionInContext {
return {
pageGuid: actionInContext.pageGuid,
startTime: actionInContext.startTime,
endTime: actionInContext.startTime,
action: {
name: 'assertVisible',
selector: signal.selector,
signals: [],
},
};
}
export function sanitizeDeviceOptions(device: any, options: BrowserContextOptions): BrowserContextOptions {
// Filter out all the properties from the device descriptor.
const cleanedOptions: Record<string, any> = {};
for (const property in options) {
if (JSON.stringify(device[property]) !== JSON.stringify((options as any)[property]))
cleanedOptions[property] = (options as any)[property];
}
return cleanedOptions;
}
export function toSignalMap(action: actions.Action) {
let popup: actions.PopupSignal | undefined;
let download: actions.DownloadSignal | undefined;
let dialog: actions.DialogSignal | undefined;
let expect: actions.ExpectSignal | undefined;
for (const signal of action.signals) {
if (signal.name === 'popup')
popup = signal;
else if (signal.name === 'download')
download = signal;
else if (signal.name === 'dialog')
dialog = signal;
else if (signal.name === 'expect')
expect = signal;
}
return {
popup,
download,
dialog,
expect,
};
}
export function toKeyboardModifiers(modifiers: number): types.SmartKeyboardModifier[] {
const result: types.SmartKeyboardModifier[] = [];
if (modifiers & 1)
result.push('Alt');
if (modifiers & 2)
result.push('ControlOrMeta');
if (modifiers & 4)
result.push('ControlOrMeta');
if (modifiers & 8)
result.push('Shift');
return result;
}
export function fromKeyboardModifiers(modifiers?: types.SmartKeyboardModifier[]): number {
let result = 0;
if (!modifiers)
return result;
if (modifiers.includes('Alt'))
result |= 1;
if (modifiers.includes('Control'))
result |= 2;
if (modifiers.includes('ControlOrMeta'))
result |= 2;
if (modifiers.includes('Meta'))
result |= 4;
if (modifiers.includes('Shift'))
result |= 8;
return result;
}
export function toClickOptionsForSourceCode(action: actions.ClickAction): types.MouseClickOptions {
const modifiers = toKeyboardModifiers(action.modifiers);
const options: types.MouseClickOptions = {};
if (action.button !== 'left')
options.button = action.button;
if (modifiers.length)
options.modifiers = modifiers;
// Do not render clickCount === 2 for dblclick.
if (action.clickCount > 2)
options.clickCount = action.clickCount;
if (action.position)
options.position = action.position;
return options;
}