Source: e2e/fields/SelectField.js

  1. 'use strict';
  2. /**
  3. * @module e2e/fields/SelectField
  4. */
  5. var util = require('util');
  6. var Field = process.requireTest('lib/e2e/fields/Field.js');
  7. var browserExt = process.requireTest('lib/e2e/browser.js');
  8. /**
  9. * Defines a form select field.
  10. *
  11. * Use [Field.get]{@link module:e2e/fields/Field~Field.get} method to get an instance of SelectField.
  12. *
  13. * @example
  14. * var Field = require('@openveo/test').e2e.fields.Field;
  15. *
  16. * var mySelectField = Field.get({
  17. * type: 'select',
  18. * name: 'My field',
  19. * baseElement: element(by.css('form'))
  20. * });
  21. *
  22. * @class SelectField
  23. * @extends module:e2e/fields/Field~Field
  24. * @constructor
  25. */
  26. function SelectField(conf) {
  27. SelectField.super_.call(this, conf);
  28. }
  29. module.exports = SelectField;
  30. util.inherits(SelectField, Field);
  31. /**
  32. * Gets field value.
  33. *
  34. * @example
  35. * myField.getValue().then(function(value) {
  36. * console.log('Got value : ' + value);
  37. * });
  38. *
  39. * @return {Promise} Promise resolving with the name of the selected option (not the value)
  40. */
  41. SelectField.prototype.getValue = function() {
  42. var fieldElement;
  43. return this.getElement().then(function(elementFinder) {
  44. fieldElement = elementFinder;
  45. var select = fieldElement.element(by.css('select'));
  46. // Get selected value
  47. return browser.executeScript('return arguments[0].value;', select.getWebElement());
  48. }).then(function(value) {
  49. var option = fieldElement.element(by.css('option[value="' + value + '"]'));
  50. return option.getText();
  51. });
  52. };
  53. /**
  54. * Sets field value.
  55. *
  56. * @example
  57. * myField.setValue('new value').then(function() {
  58. * console.log('Value set');
  59. * });
  60. *
  61. * @param {String} [value=''] The name of the option to select (not the value)
  62. * @return {Promise} Promise resolving when the field is filled
  63. */
  64. SelectField.prototype.setValue = function(value) {
  65. if (!value)
  66. return this.clear();
  67. return this.getElement().then(function(elementFinder) {
  68. var option = elementFinder.element(by.cssContainingText('option', value));
  69. return browserExt.click(option);
  70. });
  71. };
  72. /**
  73. * Clears field value.
  74. *
  75. * This will select the first option of the select element.
  76. *
  77. * @example
  78. * myField.clear().then(function() {
  79. * console.log('Field cleared');
  80. * });
  81. *
  82. * @return {Promise} Promise resolving when the field is cleared
  83. */
  84. SelectField.prototype.clear = function() {
  85. return this.getElement().then(function(elementFinder) {
  86. var option = elementFinder.all(by.css('option')).get(0);
  87. return browserExt.click(option);
  88. });
  89. };