Bug 1336124 - Add assert.session for checking if session is active; r?maja_zf draft
authorAndreas Tolfsen <ato@mozilla.com>
Thu, 02 Feb 2017 16:09:14 +0000
changeset 479423 6822f0035a004c0f5871880e1bc5addb3edc6040
parent 479417 7f1b358fb17dfd982c5e18c34d5735cd481c7f7c
child 479424 81bb572d22573c7b57eb1480a0d25d503645f406
push id44249
push userbmo:ato@mozilla.com
push dateMon, 06 Feb 2017 16:53:35 +0000
reviewersmaja_zf
bugs1336124
milestone54.0a1
Bug 1336124 - Add assert.session for checking if session is active; r?maja_zf MozReview-Commit-ID: CmHFag2q1V2
testing/marionette/assert.js
testing/marionette/test_assert.js
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -17,16 +17,36 @@ this.EXPORTED_SYMBOLS = ["assert"];
 const isFennec = () => AppConstants.platform == "android";
 const isB2G = () => AppConstants.MOZ_B2G;
 const isFirefox = () => Services.appinfo.name == "Firefox";
 
 /** Shorthands for common assertions made in Marionette. */
 this.assert = {};
 
 /**
+ * Asserts that Marionette has a session.
+ *
+ * @param {GeckoDriver} driver
+ *     Marionette driver instance.
+ * @param {string=} msg
+ *     Custom error message.
+ *
+ * @return {string}
+ *     Session ID.
+ *
+ * @throws {InvalidSessionIdError}
+ *     If |driver| does not have a session ID.
+ */
+assert.session = function (driver, msg = "") {
+  assert.that(sessionID => sessionID,
+      msg, InvalidSessionIdError)(driver.sessionId);
+  return driver.sessionId;
+};
+
+/**
  * Asserts that the current browser is Firefox Desktop.
  *
  * @param {string=} msg
  *     Custom error message.
  *
  * @throws {UnsupportedOperationError}
  *     If current browser is not Firefox.
  */
--- a/testing/marionette/test_assert.js
+++ b/testing/marionette/test_assert.js
@@ -4,16 +4,25 @@
 
 "use strict";
 
 const {utils: Cu} = Components;
 
 Cu.import("chrome://marionette/content/assert.js");
 Cu.import("chrome://marionette/content/error.js");
 
+add_test(function test_session() {
+  assert.session({sessionId: "foo"});
+  for (let typ of [null, undefined, ""]) {
+    Assert.throws(() => assert.session({sessionId: typ}), InvalidSessionIdError);
+  }
+
+  run_next_test();
+});
+
 add_test(function test_platforms() {
   // at least one will fail
   let raised;
   for (let fn of [assert.firefox, assert.fennec, assert.b2g, assert.mobile]) {
     try {
       fn();
     } catch (e) {
       raised = e;