Home Manual Reference Source

src/controllers/web/contacts.controller.js

import * as WebPageObjects from "../../page_objects/web/webPageObjects.js";
import BaseController from "./base.controller.js";

/** Controller for making and handling calls */
class ContactsController extends BaseController {
  /* eslint-disable max-statements */
  /**
   * @param {args} args Args from client
   */
  constructor(args) {
    super(args);
    this.headerPanel = new WebPageObjects.HeaderPanel(args);
    this.contactsPage = new WebPageObjects.ContactsPage(args);
    this.meetingsPage = new WebPageObjects.MeetingsPage(args);
    this.roomSystemsPage = new WebPageObjects.RoomSystemsPage(args);
    this.contactDetailsPanel = new WebPageObjects.ContactDetailsPanel(args);
    this.meetingDetailsPanel = new WebPageObjects.MeetingDetailsPanel(args);
    this.roomSystemDetailsPanel = new WebPageObjects.RoomSystemDetailsPanel(
      args
    );
    // eslint-disable-next-line max-len
    this.personalContactDetailsPanel = new WebPageObjects.PersonalContactDetailsPanel(
      args
    );
    this.createContactModal = new WebPageObjects.CreateContactModal(args);
    this.createMeetingModal = new WebPageObjects.CreateMeetingModal(args);
    this.baseModalPage = new WebPageObjects.BaseModalPage(args);
    this.baseContactFormPage = new WebPageObjects.BaseContactFormPage(args);
    this.deleteModal = new WebPageObjects.DeleteModal(args);
  }
  /* eslint-enable max-statements */

  /**
   * Fills form with provided parameters and saves contact
   * @param {String} name Name of contact
   * @returns {Boolean} true if contact is found in list, false if not
   */
  async isContactInList(name) {
    const foundContact = await this.contactsPage.findContact(name);
    return foundContact ? true : false;
  }

  /**
   * Opens contact creation form
   */
  async openCreateContactForm() {
    await this.contactsPage.openCreateContactModal();
  }

  /**
   * Open meeting creation form
   */
  async openCreateMeetingForm() {
    await this.meetingsPage.openCreateMeetingModal();
  }

  /**
   * Creates a personal contact
   * @param {String} name Contact name
   * @param {String} details Contact details
   * @param {Object} options Favorite as boolean, or doNotSubmit as boolean
   */
  async createPersonalContact(name, details, options = { favorite: false }) {
    await this.openCreateContactForm();
    await this.createContactModal.waitForHeader();
    await this.createContactModal.fillForm(name, details, options.favorite);
    if (!options.doNotSubmit) {
      await this.createContactModal.clickSaveButton();
      await this.browser.pause(1000);
      if ((await this.isFieldErrorsVisible()) === false) {
        await this.personalContactDetailsPanel
          .contactName()
          .waitForVisible(10000);
      }
    }
  }

  /**
   * Creates a meeting
   * @param {String} name Contact name
   * @param {Object} options Favorite as boolean, or doNotSubmit as boolean
   */
  async createMeeting(name) {
    await this.openCreateMeetingForm();
    await this.createMeetingModal.waitForHeader();
    await this.createMeetingModal.fillFormAndSave(name);
  }

  /**
   * Looks for the contact in the contacts list and get contact details
   * @param  {String} name Name of the contact to look for
   * @return {Object} Details of the contact
   */
  async getContactDetails(name) {
    await this.contactsPage.selectContact(name);
    await this.contactDetailsPanel.contactName().waitForVisible(10000);
    return await this.contactDetailsPanel.parseContactDetails();
  }

  /**
   * Looks for a personal contact in the contacts list and get contact details
   * @param  {String} name Name of the personal contact to look for
   * @return {Object} Details of personal contact
   */
  async getPersonalContactDetails(name) {
    await this.contactsPage.selectContact(name);
    await this.personalContactDetailsPanel.contactName().waitForVisible(10000);
    return await this.personalContactDetailsPanel.parsePersonalContactDetails();
  }

  /**
   * Returns if contact form header is visible 
   * @return {Boolean} True if contact form header is visible
   */
  async isContactFormHeaderVisible() {
    return await this.baseContactFormPage.isHeaderVisible();
  }

  /**
   * Checks if save button is enabled
   * @return {Boolean} True if button is enabled
   */
  async isContactFormSaveButtonEnabled() {
    return await this.baseContactFormPage.isSaveButtonEnabled();
  }

  /**
   * Clicks on contact form cancel button
   */
  async clickContactFormCancelButton() {
    await this.baseContactFormPage.clickCancelButton();
  }

  /**
   * Closes the create/edit contact form 
   * (Applicable to contact and meeting create/edit/delete modal)
   */
  async closeModal() {
    await this.baseModalPage.closeModal();
  }

  /**
   * Checks for error message and exclamation for the name field 
   * (Applicable to both contact and meeting create/edit modal)
   * @param  {String} expectedError Expected error message
   * @return {Boolean} Returns true if error is present
   */
  async checkContactFormNameFieldError(expectedError) {
    return (
      (await this.baseContactFormPage.isNameFieldErrorExclamationVisible()) &&
      (await this.baseContactFormPage.getNameFieldErrorText()) === expectedError
    );
  }

  /**
   * Checks for error message and exclamation for the 
   * calling details field in contact create/edit form
   * @param {String} expectedError Expected error message
   * @returns {Boolean} Returns true if error is present
   */
  async checkContactCallingDetailsFieldError(expectedError) {
    return (
      (await this.createContactModal.isCallingDetailsFieldErrorVisible()) &&
      (await this.createContactModal.getCallingDetailsFieldErrorText()) ===
        expectedError
    );
  }

  /**
   * Checks to see either the name or calling details field has an error
   * @returns {Boolean} True if error is visible
   */
  async isFieldErrorsVisible() {
    return (
      (await this.baseContactFormPage.isNameFieldErrorExclamationVisible()) &&
      (await this.createContactModal.isCallingDetailsFieldErrorVisible())
    );
  }

  /**
   * Checks name field limit and returns a boolean value
   * @param  {String}  expectedLimit Expected field limit
   * @returns {Boolean} Returns true if expected and actual is same else false
   */
  async checkNameFieldLimit(expectedLimit) {
    return (
      parseInt(expectedLimit) ===
      parseInt(await this.baseContactFormPage.getNameFieldLimit())
    );
  }

  /**
   * Checks calling details field limit and returns a boolean value
   * @param {Integer} expectedLimit Expected field limit
   * @returns {Boolean} Returns true if expected and actual is same else false
   */
  async checkCallingDetailsFieldLimit(expectedLimit) {
    return (
      parseInt(expectedLimit) ===
      parseInt(await this.createContactModal.getCallingDetailsFieldLimit())
    );
  }

  /**
   * Checks for the form header to be the expected string
   * @param {String} expectedHeader Expected form header
   * @returns {Boolean} Whether the expected form header is same as the actual
   */
  async checkFormHeader(expectedHeader) {
    return expectedHeader === (await this.baseModalPage.getModalHeaderText());
  }

  /**
   * Looks for a contact in contact list and opens the edit contact form
   * @param {String} name Name of contact to edit
   */
  async openEditContactForm(name) {
    await this.contactsPage.selectContact(name);
    await this.personalContactDetailsPanel.editButton().click();
    await this.createContactModal.waitForHeader();
  }

  /**
   * Edits a personal contact
   * @param {String} name Name of the contact to be edited
   * @param {Object} newData Object with new name, callingDetails and favorite
   */
  async editPersonalContact(name, newData) {
    await this.openEditContactForm(name);

    newData.hasOwnProperty("name") &&
      (await this.createContactModal.setNameField(newData.name));

    newData.hasOwnProperty("callingDetails") &&
      (await this.createContactModal.setCallingDetails(newData.callingDetails));

    newData.hasOwnProperty("favorite") &&
      (await this.createContactModal.setFavoriteField(newData.favorite));

    await this.createContactModal.clickSaveButton();
    await this.browser.pause(2000);
    await this.personalContactDetailsPanel.contactName().waitForVisible(5000);
  }

  /**
   * Opens delete contact modal
   * @param {String} name Name of the contact
   */
  async openDeleteContactModal(name) {
    await this.contactsPage.selectContact(name);
    await this.personalContactDetailsPanel.deleteButton().click();
    await this.baseModalPage.waitForHeader();
  }

  /**
   * Deletes a personal contact
   * @param {String} name Name of the contact to be deleted
   * @param {Boolean} confirmDelete Confirms deletion on true else cancels it
   */
  async deletePersonalContact(name, confirmDelete = true) {
    await this.openDeleteContactModal(name);
    if (confirmDelete) {
      await this.deleteModal.clickDeleteButton();
      // wait for the contact card to be closed after deletion
      await this.personalContactDetailsPanel
        .contactName()
        .waitForNotVisible(3000);
    } else await this.deleteModal.clickCancelButton();
  }

  /**
   * Get the names of the contact list
   * @returns {Array} Array of names
   */
  async getContactNamesInList() {
    return await this.contactsPage.getNamesInList();
  }

  /**
   * Get the names of the meeting list
   * @returns {Array} Array of names
   */
  async getMeetingNamesInList() {
    return await this.meetingsPage.getNamesInList();
  }

  /**
   * Get the names of the room list
   * @returns {Array} Array of names
   */
  async getRoomNamesInList() {
    return await this.roomSystemsPage.getNamesInList();
  }

  /**
   * Get the text of the no contacts match message
   * @returns {String} Text of the message
   */
  async getNoContactsMessage() {
    return await this.contactsPage.noContactsMessage().getText();
  }

  /**
   * Get the text of the no contacts match message
   * @returns {String} Text of the message
   */
  async getNoMeetingsMessage() {
    return await this.meetingsPage.noContactsMessage().getText();
  }

  /**
   * Get the text of the no contacts match message
   * @returns {String} Text of the message
   */
  async getNoRoomsMessage() {
    return await this.roomSystemsPage.noContactsMessage().getText();
  }
  /**
   * Determines if Contacts are Chattable
   * @param {String} contact Name of Contact
   * @returns {Boolean} True if Chattable and False if Not
   */
  async isContactChattable(contact) {
    await this.contactsPage.selectContact(contact);
    const details = await this.contactDetailsPanel.chatIcon().isVisible();
    const list = await this.contactsPage.chatIconPresent(contact);
    return list && details;
  }
}

export default ContactsController;