text.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Property that retrieves the text contained by an element. Can be chained to check if contains/equals/matches the specified text or regex.
  3. *
  4. * ```
  5. * this.demoTest = function (browser) {
  6. * browser.expect.element('#main').text.to.equal('The Night Watch');
  7. * browser.expect.element('#main').text.to.not.equal('The Night Watch');
  8. * browser.expect.element('#main').text.to.equal('The Night Watch').before(100);
  9. * browser.expect.element('#main').text.to.contain('The Night Watch');
  10. * browser.expect.element('#main').text.to.match(/The\ Night\ Watch/);
  11. * };
  12. * ```
  13. *
  14. * @method text
  15. * @since v0.7
  16. * @display .text
  17. * @api expect
  18. */
  19. var util = require('util');
  20. var events = require('events');
  21. var BaseAssertion = require('./_baseAssertion.js');
  22. function TextAssertion() {
  23. this.flag('textFlag', true);
  24. BaseAssertion.call(this);
  25. this.message = 'Expected element <%s> text to' + (this.negate ? ' not' : '');
  26. this.start();
  27. }
  28. util.inherits(TextAssertion, BaseAssertion);
  29. TextAssertion.prototype.executeCommand = function(callback) {
  30. this.protocol.elementIdText(this.elementResult.ELEMENT, callback);
  31. };
  32. TextAssertion.prototype.elementFound = function() {
  33. if (this.retries > 0 && this.negate) {
  34. return;
  35. }
  36. if (this.passed && this.waitForMs) {
  37. var message = '';
  38. if (this.hasCondition()) {
  39. message = 'condition was met';
  40. }
  41. this.elapsedTime = this.getElapsedTime();
  42. this.messageParts.push(' - ' + message + ' in ' + this.elapsedTime + 'ms');
  43. }
  44. };
  45. TextAssertion.prototype.elementNotFound = function() {
  46. this.passed = false;
  47. };
  48. module.exports = TextAssertion;