containsText.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Checks if the given element contains the specified text.
  3. *
  4. * ```
  5. * this.demoTest = function (client) {
  6. * browser.assert.containsText('#main', 'The Night Watch');
  7. * };
  8. * ```
  9. *
  10. * @method containsText
  11. * @param {string} selector The selector (CSS / Xpath) used to locate the element.
  12. * @param {string} expectedText The text to look for.
  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, expectedText, msg) {
  18. var MSG_ELEMENT_NOT_FOUND = 'Testing if element <%s> contains text: "%s". ' +
  19. 'Element could not be located.';
  20. this.message = msg || util.format('Testing if element <%s> contains text: "%s".', selector, expectedText);
  21. this.expected = function() {
  22. return expectedText;
  23. };
  24. this.pass = function(value) {
  25. return value.indexOf(expectedText) > -1;
  26. };
  27. this.failure = function(result) {
  28. var failed = result === false || result && result.status === -1;
  29. if (failed) {
  30. this.message = msg || util.format(MSG_ELEMENT_NOT_FOUND, selector, expectedText);
  31. }
  32. return failed;
  33. };
  34. this.value = function(result) {
  35. return result.value;
  36. };
  37. this.command = function(callback) {
  38. return this.api.getText(selector, callback);
  39. };
  40. };