Bug 1735162 - [marionette] Write Marionette port to MarionetteActivePort file in profile directory. r=webdriver-reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D128412
This commit is contained in:
Henrik Skupin 2021-10-15 14:45:28 +00:00
Родитель 71fbec519e
Коммит 110a77d073
2 изменённых файлов: 62 добавлений и 9 удалений

Просмотреть файл

@ -25,6 +25,8 @@ XPCOMUtils.defineLazyGetter(this, "logger", () =>
Log.get(Log.TYPES.MARIONETTE)
);
XPCOMUtils.defineLazyGetter(this, "textEncoder", () => new TextEncoder());
XPCOMUtils.defineLazyServiceGetter(
this,
"env",
@ -68,6 +70,7 @@ const isRemote =
class MarionetteParentProcess {
constructor() {
this.server = null;
this._marionetteActivePortPath;
this.classID = Components.ID("{786a1369-dca5-4adc-8486-33d23c88010a}");
this.helpInfo = " --marionette Enable remote control server.\n";
@ -126,7 +129,7 @@ class MarionetteParentProcess {
cmdLine.handleFlag("marionette", false);
}
observe(subject, topic) {
async observe(subject, topic) {
if (this.enabled) {
logger.trace(`Received observer notification ${topic}`);
}
@ -172,7 +175,7 @@ class MarionetteParentProcess {
Services.obs.addObserver(this, "quit-application");
this.finalUIStartup = true;
this.init();
await this.init();
}
break;
@ -184,13 +187,13 @@ class MarionetteParentProcess {
case "toplevel-window-ready":
subject.addEventListener(
"load",
ev => {
async ev => {
if (ev.target.documentElement.namespaceURI == XMLURI_PARSE_ERROR) {
Services.obs.removeObserver(this, topic);
let parserError = ev.target.querySelector("parsererror");
logger.fatal(parserError.textContent);
this.uninit();
await this.uninit();
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
}
},
@ -225,14 +228,14 @@ class MarionetteParentProcess {
Services.obs.addObserver(this, "quit-application");
this.finalUIStartup = true;
this.init();
await this.init();
}
break;
case "quit-application":
Services.obs.removeObserver(this, "quit-application");
this.uninit();
await this.uninit();
break;
}
}
@ -254,7 +257,7 @@ class MarionetteParentProcess {
);
}
init(quit = true) {
async init(quit = true) {
if (this.running || !this.enabled || !this.finalUIStartup) {
logger.debug(
`Init aborted (running=${this.running}, ` +
@ -282,7 +285,7 @@ class MarionetteParentProcess {
this.server.start();
} catch (e) {
logger.fatal("Remote protocol server failed to start", e);
this.uninit();
await this.uninit();
if (quit) {
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
}
@ -292,14 +295,41 @@ class MarionetteParentProcess {
env.set(ENV_ENABLED, "1");
Services.obs.notifyObservers(this, NOTIFY_LISTENING, true);
logger.debug("Marionette is listening");
// Write Marionette port to MarionetteActivePort file within the profile.
const profileDir = await PathUtils.getProfileDir();
this._marionetteActivePortPath = PathUtils.join(
profileDir,
"MarionetteActivePort"
);
const data = `${this.server.port}`;
try {
await IOUtils.write(
this._marionetteActivePortPath,
textEncoder.encode(data)
);
} catch (e) {
logger.warn(
`Failed to create ${this._marionetteActivePortPath} (${e.message})`
);
}
});
}
uninit() {
async uninit() {
if (this.running) {
this.server.stop();
Services.obs.notifyObservers(this, NOTIFY_LISTENING);
logger.debug("Marionette stopped listening");
try {
await IOUtils.remove(this._marionetteActivePortPath);
} catch (e) {
logger.warn(
`Failed to remove ${this._marionetteActivePortPath} (${e.message})`
);
}
}
}

Просмотреть файл

@ -4,6 +4,7 @@
from __future__ import absolute_import
import os
import socket
import time
@ -32,6 +33,28 @@ class TestMarionette(MarionetteTestCase):
self.assertRaises(socket.timeout, self.marionette.raise_for_port, timeout=5)
self.assertLess(time.time() - start_time, 5)
@run_if_manage_instance("Only runnable if Marionette manages the instance")
def test_marionette_active_port_file(self):
active_port_file = os.path.join(
self.marionette.instance.profile.profile, "MarionetteActivePort"
)
self.assertTrue(
os.path.exists(active_port_file), "MarionetteActivePort file written"
)
with open(active_port_file, "r") as fp:
lines = fp.readlines()
self.assertEqual(len(lines), 1, "MarionetteActivePort file contains two lines")
self.assertEqual(
int(lines[0]),
self.marionette.port,
"MarionetteActivePort file contains port",
)
self.marionette.quit(in_app=True)
self.assertFalse(
os.path.exists(active_port_file), "MarionetteActivePort file removed"
)
def test_single_active_session(self):
self.assertEqual(1, self.marionette.execute_script("return 1"))