valueContains.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Checks if the given form element's value contains the expected value.
  3. *
  4. * ```
  5. * this.demoTest = function (client) {
  6. * browser.assert.valueContains("form.login input[type=text]", "username");
  7. * };
  8. * ```
  9. *
  10. * @method valueContains
  11. * @param {string} selector The selector (CSS / Xpath) used to locate the element.
  12. * @param {string} expectedText The expected text.
  13. * @param {string} [message] Optional log message to display in the output. If missing, one is displayed by default.
  14. * @api assertions
  15. */
  16. var util = require('util');
  17. exports.assertion = function(selector, expected, msg) {
  18. var DEFAULT_MSG = 'Testing if value of <%s> contains: "%s".';
  19. var MSG_ELEMENT_NOT_FOUND = DEFAULT_MSG + ' ' + 'Element could not be located.';
  20. var VALUE_ATTR_NOT_FOUND = DEFAULT_MSG + ' ' + 'Element does not have a value attribute.';
  21. this.message = msg || util.format(DEFAULT_MSG, selector, expected);
  22. this.expected = true;
  23. this.pass = function(value) {
  24. return value.indexOf(expected) > -1;
  25. };
  26. this.failure = function(result) {
  27. var failed = (result === false) ||
  28. // no such element
  29. result && (result.status === -1 || result.value === null);
  30. if (failed) {
  31. var defaultMsg = MSG_ELEMENT_NOT_FOUND;
  32. if (result && result.value === null) {
  33. defaultMsg = VALUE_ATTR_NOT_FOUND;
  34. }
  35. this.message = msg || util.format(defaultMsg, selector, expected);
  36. }
  37. return failed;
  38. };
  39. this.value = function(result) {
  40. return result.value;
  41. };
  42. this.command = function(callback) {
  43. return this.api.getValue(selector, callback);
  44. };
  45. };