present.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Property that checks if an element is present in the DOM.
  3. *
  4. * ```
  5. * this.demoTest = function (browser) {
  6. * browser.expect.element('#main').to.be.present;
  7. * browser.expect.element('#main').to.not.be.present;
  8. * browser.expect.element('#main').to.be.present.before(100);
  9. * };
  10. * ```
  11. *
  12. * @method present
  13. * @display .present
  14. * @since v0.7
  15. * @api expect
  16. */
  17. var util = require('util');
  18. var events = require('events');
  19. var BaseAssertion = require('./_baseAssertion.js');
  20. function PresentAssertion() {
  21. this.flag('present', true);
  22. BaseAssertion.call(this);
  23. this.message = 'Expected element <%s> to ' + (this.negate ? 'not be present' : 'be present');
  24. this.start();
  25. }
  26. util.inherits(PresentAssertion, BaseAssertion);
  27. PresentAssertion.prototype.executeCommand = function(callback) {
  28. return callback(this.elementResult);
  29. };
  30. PresentAssertion.prototype.elementFound = function() {
  31. this.passed = !this.negate;
  32. if (!this.passed && this.shouldRetry()) {
  33. return;
  34. }
  35. if (this.waitForMs) {
  36. this.elapsedTime = this.getElapsedTime();
  37. this.messageParts.push(' - element was present in ' + this.elapsedTime + 'ms');
  38. }
  39. if (this.negate) {
  40. this.actual = 'present';
  41. this.expected = 'not present';
  42. }
  43. };
  44. PresentAssertion.prototype.elementNotFound = function() {
  45. this.passed = this.negate;
  46. if (!this.passed && this.shouldRetry()) {
  47. return;
  48. }
  49. if (this.waitForMs && this.negate) {
  50. this.messageParts.push(this.checkWaitForMsg(this.elapsedTime) + '.');
  51. }
  52. };
  53. PresentAssertion.prototype.retryCommand = function() {
  54. this.promise = this.element.createPromise();
  55. this.element.deferred.promise.then(this.onPromiseResolved.bind(this), this.onPromiseRejected.bind(this));
  56. this.element.locate();
  57. };
  58. module.exports = PresentAssertion;