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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AppInfo: "chrome://remote/content/shared/AppInfo.sys.mjs",
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
EventPromise: "chrome://remote/content/shared/Sync.sys.mjs",
MessageManagerDestroyedPromise:
"chrome://remote/content/marionette/sync.sys.mjs",
TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
windowManager: "chrome://remote/content/shared/WindowManager.sys.mjs",
});
/** @namespace */
export const browser = {};
/**
* Variations of Marionette contexts.
*
* Choosing a context through the <tt>Marionette:SetContext</tt>
* command directs all subsequent browsing context scoped commands
* to that context.
*
* @class Marionette.Context
*/
export class Context {
/**
* Gets the correct context from a string.
*
* @param {string} s
* Context string serialisation.
*
* @returns {Context}
* Context.
*
* @throws {TypeError}
* If <var>s</var> is not a context.
*/
static fromString(s) {
switch (s) {
case "chrome":
return Context.Chrome;
case "content":
return Context.Content;
default:
throw new TypeError(`Unknown context: ${s}`);
}
}
}
Context.Chrome = "chrome";
Context.Content = "content";
/**
* Creates a browsing context wrapper.
*
* Browsing contexts handle interactions with the browser, according to
* the current environment.
*/
browser.Context = class {
/**
* @param {ChromeWindow} window
* ChromeWindow that contains the top-level browsing context.
* @param {GeckoDriver} driver
* Reference to driver instance.
*/
constructor(window, driver) {
this.window = window;
this.driver = driver;
// In Firefox this is <xul:tabbrowser> (not <xul:browser>!)
// and MobileTabBrowser in GeckoView.
this.tabBrowser = lazy.TabManager.getTabBrowser(this.window);
// Used to set curFrameId upon new session
this.newSession = true;
// A reference to the tab corresponding to the current window handle,
// if any. Specifically, this.tab refers to the last tab that Marionette
// switched to in this browser window. Note that this may not equal the
// currently selected tab. For example, if Marionette switches to tab
// A, and then clicks on a button that opens a new tab B in the same
// browser window, this.tab will still point to tab A, despite tab B
// being the currently selected tab.
this.tab = null;
}
/**
* Returns the content browser for the currently selected tab.
* If there is no tab selected, null will be returned.
*/
get contentBrowser() {
if (this.tab) {
return lazy.TabManager.getBrowserForTab(this.tab);
} else if (
this.tabBrowser &&
this.driver.isReftestBrowser(this.tabBrowser)
) {
return this.tabBrowser;
}
return null;
}
get messageManager() {
if (this.contentBrowser) {
return this.contentBrowser.messageManager;
}
return null;
}
/**
* Checks if the browsing context has been discarded.
*
* The browsing context will have been discarded if the content
* browser, represented by the <code><xul:browser></code>,
* has been detached.
*
* @returns {boolean}
* True if browsing context has been discarded, false otherwise.
*/
get closed() {
return this.contentBrowser === null;
}
/**
* Gets the position and dimensions of the top-level browsing context.
*
* @returns {Map.<string, number>}
* Object with |x|, |y|, |width|, and |height| properties.
*/
get rect() {
return {
x: this.window.screenX,
y: this.window.screenY,
width: this.window.outerWidth,
height: this.window.outerHeight,
};
}
/**
* Retrieves the current tabmodal UI object. According to the browser
* associated with the currently selected tab.
*/
getTabModal() {
let br = this.contentBrowser;
if (!br.hasAttribute("tabmodalPromptShowing")) {
return null;
}
// The modal is a direct sibling of the browser element.
// See tabbrowser.xml's getTabModalPromptBox.
let modalElements = br.parentNode.getElementsByTagName("tabmodalprompt");
return br.tabModalPromptBox.getPrompt(modalElements[0]);
}
/**
* Close the current window.
*
* @returns {Promise}
* A promise which is resolved when the current window has been closed.
*/
async closeWindow() {
return lazy.windowManager.closeWindow(this.window);
}
/**
* Focus the current window.
*
* @returns {Promise}
* A promise which is resolved when the current window has been focused.
*/
async focusWindow() {
await lazy.windowManager.focusWindow(this.window);
// Also focus the currently selected tab if present.
this.contentBrowser?.focus();
}
/**
* Open a new browser window.
*
* @returns {Promise}
* A promise resolving to the newly created chrome window.
*/
openBrowserWindow(focus = false, isPrivate = false) {
return lazy.windowManager.openBrowserWindow({
openerWindow: this.window,
focus,
isPrivate,
});
}
/**
* Close the current tab.
*
* @returns {Promise}
* A promise which is resolved when the current tab has been closed.
*
* @throws UnsupportedOperationError
* If tab handling for the current application isn't supported.
*/
async closeTab() {
// If the current window is not a browser then close it directly. Do the
// same if only one remaining tab is open, or no tab selected at all.
//
// Note: For GeckoView there will always be a single tab only. But for
// consistency with other platforms a specific condition has been added
// below as well even it's not really used.
if (
!this.tabBrowser ||
!this.tabBrowser.tabs ||
this.tabBrowser.tabs.length === 1 ||
!this.tab
) {
return this.closeWindow();
}
let destroyed = new lazy.MessageManagerDestroyedPromise(
this.messageManager
);
let tabClosed;
if (lazy.AppInfo.isAndroid) {
await lazy.TabManager.removeTab(this.tab);
} else if (lazy.AppInfo.isFirefox) {
tabClosed = new lazy.EventPromise(this.tab, "TabClose");
await this.tabBrowser.removeTab(this.tab);
} else {
throw new lazy.error.UnsupportedOperationError(
`closeTab() not supported for ${lazy.AppInfo.name}`
);
}
return Promise.all([destroyed, tabClosed]);
}
/**
* Open a new tab in the currently selected chrome window.
*/
async openTab(focus = false) {
let tab = null;
// Bug 1795841 - For Firefox the TabManager cannot be used yet. As such
// handle opening a tab differently for Android.
if (lazy.AppInfo.isAndroid) {
tab = await lazy.TabManager.addTab({ focus, window: this.window });
} else if (lazy.AppInfo.isFirefox) {
const opened = new lazy.EventPromise(this.window, "TabOpen");
this.window.BrowserOpenTab({ url: "about:blank" });
await opened;
tab = this.tabBrowser.selectedTab;
// The new tab is always selected by default. If focus is not wanted,
// the previously tab needs to be selected again.
if (!focus) {
await lazy.TabManager.selectTab(this.tab);
}
} else {
throw new lazy.error.UnsupportedOperationError(
`openTab() not supported for ${lazy.AppInfo.name}`
);
}
return tab;
}
/**
* Set the current tab.
*
* @param {number=} index
* Tab index to switch to. If the parameter is undefined,
* the currently selected tab will be used.
* @param {ChromeWindow=} window
* Switch to this window before selecting the tab.
* @param {boolean=} focus
* A boolean value which determins whether to focus
* the window. Defaults to true.
*
* @returns {Tab}
* The selected tab.
*
* @throws UnsupportedOperationError
* If tab handling for the current application isn't supported.
*/
async switchToTab(index, window = undefined, focus = true) {
if (window) {
this.window = window;
this.tabBrowser = lazy.TabManager.getTabBrowser(this.window);
}
if (!this.tabBrowser || this.driver.isReftestBrowser(this.tabBrowser)) {
return null;
}
if (typeof index == "undefined") {
this.tab = this.tabBrowser.selectedTab;
} else {
this.tab = this.tabBrowser.tabs[index];
}
if (focus) {
await lazy.TabManager.selectTab(this.tab);
}
// By accessing the content browser's message manager a new browsing
// context is created for browserless tabs, which is needed to successfully
// run the WebDriver's is browsing context open step. This is temporary
// until we find a better solution on bug 1812258.
this.messageManager;
return this.tab;
}
/**
* Registers a new frame, and sets its current frame id to this frame
* if it is not already assigned, and if a) we already have a session
* or b) we're starting a new session and it is the right start frame.
*/
register() {
if (!this.tabBrowser) {
return;
}
// If we're setting up a new session on Firefox, we only process the
// registration for this frame if it belongs to the current tab.
if (!this.tab) {
this.switchToTab();
}
}
};
/**
* Marionette representation of the {@link ChromeWindow} window state.
*
* @enum {string}
*/
export const WindowState = {
Maximized: "maximized",
Minimized: "minimized",
Normal: "normal",
Fullscreen: "fullscreen",
/**
* Converts {@link Window.windowState} to WindowState.
*
* @param {number} windowState
* Attribute from {@link Window.windowState}.
*
* @returns {WindowState}
* JSON representation.
*
* @throws {TypeError}
* If <var>windowState</var> was unknown.
*/
from(windowState) {
switch (windowState) {
case 1:
return WindowState.Maximized;
case 2:
return WindowState.Minimized;
case 3:
return WindowState.Normal;
case 4:
return WindowState.Fullscreen;
default:
throw new TypeError(`Unknown window state: ${windowState}`);
}
},
};
|