Home Manual Reference Source

src/clients/web/client.js

import * as WebCon from "../../controllers/webControllers.js";
import * as webdriverio from "webdriverio";
import chromeCaps from "../../capabilities/chrome.capabilities.js";

/**
 * Clients hold a browser and then the various controllers.
 * @example
 * // Instantiate a new client tied to a specific browser
 * myClient = new Client(browserName, t)
 *
 * // This then allows us to take actions with the browser
 * myClient.loginController.login('user@lifesize.com', 'password')
 */
class Client {
  /**
   * Each client needs a browser. From there each one creates their own
   * instance of the respective controllers.
   * @param {String} browserName Name of browser we want to start
   * @param {Object} testName Test running the browser.
   */
  constructor(browserName, testName) {
    this.options = chromeCaps;
    this.testName = testName;
    this.setBuildAndName(testName);
    this.matrix = webdriverio.multiremote({ myBrowser: this.options });
    this.browser = this.matrix.select("myBrowser");
    this.instantiateControllers();
    this.state = "STOPPED";
  }

  /**
   * Instantiate all of the controllers for the client
   */
  instantiateControllers() {
    const args = { browser: this.browser, testName: this.testName };
    this.loginController = new WebCon.LoginController(args);
    this.callController = new WebCon.CallController(args);
    this.activityController = new WebCon.ActivityController(args);
    this.chatController = new WebCon.ChatController(args);
    this.contactsController = new WebCon.ContactsController(args);
    this.profileController = new WebCon.ProfileController(args);
    this.availabilityController = new WebCon.AvailabilityController(args);
    this.navController = new WebCon.NavController(args);
  }

  /** Set build and name on desiredCaps
   * @param {String} testName Name of test
   */
  setBuildAndName(testName) {
    // setting name of browser to test title for reporting
    if (process.env.BUILD_TAG) {
      this.options.desiredCapabilities.name = `${testName} - ${process.env
        .BUILD_TAG}`;
      this.options.desiredCapabilities.build = process.env.BUILD_TAG;
    } else {
      this.options.desiredCapabilities.name = testName;
      this.options.desiredCapabilities.build = "Local Build";
    }
  }

  /**
   * pause
   * Pause client for amount of milliseconds.
   * @param {Object} ms - amount of milliseconds to pause client
   * @example client.pause(10000) // pauses for 10 seconds
   */
  async pause(ms) {
    await this.browser.pause(ms);
  }

  /**
   * Open up the browser.
   */
  async start() {
    const WebdriverRTC = require("webdriverrtc");
    await WebdriverRTC.init(this.matrix, { browser: "myBrowser" });
    await this.browser.init();
    await require("wdio-screenshot").init(this.browser);
    this.state = "STARTED";
  }

  /**
   * Stop and close the browser.
   */
  async stop() {
    if (this.state === "STARTED") {
      await this.browser.end();
      this.state = "STOPPED";
    }
  }

  /**
   * Clear cookies of browser
   */
  async clearCookies() {
    await this.browser.deleteCookie();
  }
}

export default Client;