attribute.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Checks if a given attribute of an element exists and optionally if it has the expected value.
  3. *
  4. * ```
  5. * this.demoTest = function (browser) {
  6. * browser.expect.element('body').to.have.attribute('data-attr');
  7. * browser.expect.element('body').to.not.have.attribute('data-attr');
  8. * browser.expect.element('body').to.not.have.attribute('data-attr', 'Testing if body does not have data-attr');
  9. * browser.expect.element('body').to.have.attribute('data-attr').before(100);
  10. * browser.expect.element('body').to.have.attribute('data-attr')
  11. * .equals('some attribute');
  12. * browser.expect.element('body').to.have.attribute('data-attr')
  13. * .not.equals('other attribute');
  14. * browser.expect.element('body').to.have.attribute('data-attr')
  15. * .which.contains('something');
  16. * browser.expect.element('body').to.have.attribute('data-attr')
  17. * .which.matches(/^something\ else/);
  18. * };
  19. * ```
  20. *
  21. * @method attribute
  22. * @param {string} attribute The attribute name
  23. * @param {string} [message] Optional log message to display in the output. If missing, one is displayed by default.
  24. * @display .attribute(name)
  25. * @since v0.7
  26. * @api expect
  27. */
  28. var util = require('util');
  29. var events = require('events');
  30. var BaseAssertion = require('./_baseAssertion.js');
  31. function AttributeAssertion(attribute, msg) {
  32. this.flag('attributeFlag', true);
  33. this.attribute = attribute;
  34. this.customMessage = msg;
  35. this.message = msg || 'Expected element <%s> to ' + (this.negate ? 'not have' : 'have') + ' attribute "' + attribute + '"';
  36. BaseAssertion.call(this);
  37. this.start();
  38. }
  39. util.inherits(AttributeAssertion, BaseAssertion);
  40. AttributeAssertion.prototype.executeCommand = function(callback) {
  41. this.protocol.elementIdAttribute(this.elementResult.ELEMENT, this.attribute, function(result) {
  42. if (result.value !== null && result.status === 0) {
  43. callback(result);
  44. } else {
  45. this.attributeNotFound();
  46. }
  47. }.bind(this));
  48. };
  49. AttributeAssertion.prototype.elementFound = function() {
  50. if (this.retries > 0 && this.negate) {
  51. return;
  52. }
  53. if (!this.hasCondition()) {
  54. this.passed = this.negate ? false : true;
  55. this.expected = this.negate ? 'not found' : 'found';
  56. this.actual = 'found';
  57. }
  58. if (this.waitForMs && this.passed) {
  59. var message = 'attribute was present';
  60. if (this.hasCondition()) {
  61. message = 'condition was met';
  62. }
  63. this.elapsedTime = this.getElapsedTime();
  64. this.messageParts.push(' - ' + message + ' in ' + this.elapsedTime + 'ms');
  65. }
  66. };
  67. AttributeAssertion.prototype.attributeNotFound = function() {
  68. this.processFlags();
  69. this.passed = this.hasCondition() ? false : this.negate;
  70. if (!this.passed && this.shouldRetry()) {
  71. this.scheduleRetry();
  72. } else {
  73. if (!this.hasCondition()) {
  74. this.expected = this.negate ? 'not found' : 'found';
  75. this.actual = 'not found';
  76. }
  77. if (!this.negate) {
  78. this.messageParts.push(' - attribute was not found');
  79. }
  80. this.done();
  81. }
  82. };
  83. AttributeAssertion.prototype.elementNotFound = function() {
  84. this.passed = false;
  85. };
  86. module.exports = AttributeAssertion;