<commit_before>'use strict';

/**
 * @module e2e
 */

/**
 * A form field to help writing end to end tests.
 *
 * Help manipulate form fields in a Page.
 * Use Field.get() method to get a Field instance.
 *
 * @class Field
 * @constructor
 */
function Field(conf) {

  /**
   * Field label.
   *
   * @property name
   * @type String
   */
  this.name = conf.name;

  /**
   * Element from where to look for the field (typically the form element).
   *
   * @property baseElement
   * @type ElementFinder
   */
  this.baseElement = conf.baseElement;

  if (!this.name)
    throw new Error('Missing field name');
}

module.exports = Field;

/**
 * Gets an instance of a Field.
 *
 * @example
 *
 *     var Field = require('@openveo/test').e2e.Field;
 *
 *     var myTextField = Field.get({
 *       type: 'text',
 *       name: '<NAME>',
 *       baseElement: element(by.css('form'))
 *     });
 *
 * @method get
 * @static
 * @param {Object} conf A field configuration object
 * @return {Field} An instance of a Field sub object
 */
Field.get = function(conf) {
  switch (conf.type) {
    case 'text':
      var TextField = process.requireTest('lib/e2e/fields/TextField.js');
      return new TextField(conf);

    case 'textarea':
      var TextareaField = process.requireTest('lib/e2e/fields/TextareaField.js');
      return new TextareaField(conf);

    case 'select':
      var SelectField = process.requireTest('lib/e2e/fields/SelectField.js');
      return new SelectField(conf);

    case 'date':
      var DateField = process.requireTest('lib/e2e/fields/DateField.js');
      return new DateField(conf);

    case 'checkboxes':
      var CheckboxesField = process.requireTest('lib/e2e/fields/CheckboxesField.js');
      return new CheckboxesField(conf);

    case 'time':
      var TimeField = process.requireTest('lib/e2e/fields/TimeField.js');
      return new TimeField(conf);

    default:
      throw new Error('Unknown field type');
  }
};

/**
 * Gets field description.
 *
 * @example
 *
 *     myField.getDescription().then(function(description) {
 *       console.log('Field description is : ' + description);
 *     });
 *
 * @method getDescription
 * @return {Promise} Promise resolving with the description
 */
Field.prototype.getDescription = function() {
  return this.getElement().then(function(elementFinder) {
    var description = elementFinder.element(by.css('p'));
    return description.getText();
  });
};

/**
 * Gets field label.
 *
 * @example
 *
 *     myField.getLabel().then(function(label) {
 *       console.log('Field label is : ' + label);
 *     });
 *
 * @method getLabel
 * @return {Promise} Promise resolving with the label
 */
Field.prototype.getLabel = function() {
  return this.getElement().then(function(elementFinder) {
    var label = elementFinder.element(by.xpath('./label'));
    return label.getText();
  }).then(function(text) {
    return protractor.promise.fulfilled(text.replace(/ ?[\*:]?$/, ''));
  });
};

/**
 * Gets field element wrapper.
 *
 * Look for a form element label and return its parent.
 *
 * @method getElement
 * @return {Promise} Promise resolving with the element
 */
Field.prototype.getElement = function() {
  var self = this;
  var fieldElement;
  var deferred = protractor.promise.defer();

  // Get all labels
  this.baseElement.all(by.css('label')).each(function(label, index) {

    // Get label text
    label.getText().then(function(text) {

      // Label text corresponds to the searched text
      // Return parent element
      if (text.replace(/ ?[\*:]?$/, '') === self.name)
        fieldElement = label.element(by.xpath('..'));

    });
  }).then(function() {
    deferred.fulfill(fieldElement);
  }, function(error) {
    deferred.reject(new Error('"' + self.name + '" field not found'));
  });

  return deferred.promise;
};

/**
 * Gets field error message.<commit_msg>Add more precise error message when a field element is not found in end to end tests<commit_after>'use strict';

/**
 * @module e2e
 */

/**
 * A form field to help writing end to end tests.
 *
 * Help manipulate form fields in a Page.
 * Use Field.get() method to get a Field instance.
 *
 * @class Field
 * @constructor
 */
function Field(conf) {

  /**
   * Field label.
   *
   * @property name
   * @type String
   */
  this.name = conf.name;

  /**
   * Element from where to look for the field (typically the form element).
   *
   * @property baseElement
   * @type ElementFinder
   */
  this.baseElement = conf.baseElement;

  if (!this.name)
    throw new Error('Missing field name');
}

module.exports = Field;

/**
 * Gets an instance of a Field.
 *
 * @example
 *
 *     var Field = require('@openveo/test').e2e.Field;
 *
 *     var myTextField = Field.get({
 *       type: 'text',
 *       name: '<NAME>',
 *       baseElement: element(by.css('form'))
 *     });
 *
 * @method get
 * @static
 * @param {Object} conf A field configuration object
 * @return {Field} An instance of a Field sub object
 */
Field.get = function(conf) {
  switch (conf.type) {
    case 'text':
      var TextField = process.requireTest('lib/e2e/fields/TextField.js');
      return new TextField(conf);

    case 'textarea':
      var TextareaField = process.requireTest('lib/e2e/fields/TextareaField.js');
      return new TextareaField(conf);

    case 'select':
      var SelectField = process.requireTest('lib/e2e/fields/SelectField.js');
      return new SelectField(conf);

    case 'date':
      var DateField = process.requireTest('lib/e2e/fields/DateField.js');
      return new DateField(conf);

    case 'checkboxes':
      var CheckboxesField = process.requireTest('lib/e2e/fields/CheckboxesField.js');
      return new CheckboxesField(conf);

    case 'time':
      var TimeField = process.requireTest('lib/e2e/fields/TimeField.js');
      return new TimeField(conf);

    default:
      throw new Error('Unknown field type');
  }
};

/**
 * Gets field description.
 *
 * @example
 *
 *     myField.getDescription().then(function(description) {
 *       console.log('Field description is : ' + description);
 *     });
 *
 * @method getDescription
 * @return {Promise} Promise resolving with the description
 */
Field.prototype.getDescription = function() {
  return this.getElement().then(function(elementFinder) {
    var description = elementFinder.element(by.css('p'));
    return description.getText();
  });
};

/**
 * Gets field label.
 *
 * @example
 *
 *     myField.getLabel().then(function(label) {
 *       console.log('Field label is : ' + label);
 *     });
 *
 * @method getLabel
 * @return {Promise} Promise resolving with the label
 */
Field.prototype.getLabel = function() {
  return this.getElement().then(function(elementFinder) {
    var label = elementFinder.element(by.xpath('./label'));
    return label.getText();
  }).then(function(text) {
    return protractor.promise.fulfilled(text.replace(/ ?[\*:]?$/, ''));
  });
};

/**
 * Gets field element wrapper.
 *
 * Look for a form element label and return its parent.
 *
 * @method getElement
 * @return {Promise} Promise resolving with the element
 */
Field.prototype.getElement = function() {
  var self = this;
  var fieldElement;
  var deferred = protractor.promise.defer();

  // Get all labels
  this.baseElement.all(by.css('label')).each(function(label, index) {

    // Get label text
    label.getText().then(function(text) {

      // Label text corresponds to the searched text
      // Return parent element
      if (text.replace(/ ?[\*:]?$/, '') === self.name)
        fieldElement = label.element(by.xpath('..'));

    });
  }).then(function() {
    if (fieldElement)
      deferred.fulfill(fieldElement);
    else
      deferred.reject(new Error('"' + self.name + '" field not found'));
  }, function(error) {
    deferred.reject(new Error('"' + self.name + '" field not found (' + error.message + ')'));
  });

  return deferred.promise;
};

/**
 * Gets field error message.
{'fraction_non_alphanumeric': 0.15122873345935728, 'fraction_numerical': 0.0020163831127914303, 'mean_word_length': 3.0469148393676697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4517889', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2383, 'n_words': 613}