css.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Checks a given css property of an element exists and optionally if it has the expected value.
  3. *
  4. * ```
  5. * this.demoTest = function (browser) {
  6. * browser.expect.element('#main').to.have.css('display');
  7. * browser.expect.element('#main').to.have.css('display', 'Testing for display');
  8. * browser.expect.element('#main').to.not.have.css('display');
  9. * browser.expect.element('#main').to.have.css('display').before(100);
  10. * browser.expect.element('#main').to.have.css('display').which.equals('block');
  11. * browser.expect.element('#main').to.have.css('display').which.contains('some value');
  12. * browser.expect.element('#main').to.have.css('display').which.matches(/some\ value/);
  13. * };
  14. * ```
  15. *
  16. * @method css
  17. * @param {string} property The css property name
  18. * @param {string} [message] Optional log message to display in the output. If missing, one is displayed by default.*
  19. * @display .css(property)
  20. * @since v0.7
  21. * @api expect
  22. */
  23. var util = require('util');
  24. var events = require('events');
  25. var BaseAssertion = require('./_baseAssertion.js');
  26. function CssAssertion(property, msg) {
  27. this.cssProperty = property;
  28. this.flag('cssFlag', true);
  29. BaseAssertion.call(this);
  30. this.customMessage = typeof msg != 'undefined';
  31. this.message = msg || 'Expected element <%s> to ' + (this.negate ? 'not have' : 'have') + ' css property "' + property + '"';
  32. this.start();
  33. }
  34. util.inherits(CssAssertion, BaseAssertion);
  35. CssAssertion.prototype.executeCommand = function(callback) {
  36. this.protocol.elementIdCssProperty(this.elementResult.ELEMENT, this.cssProperty, callback);
  37. };
  38. CssAssertion.prototype['@haveFlag'] = function() {
  39. this.passed = this.negate ? (this.resultValue === '') : (this.resultValue !== '');
  40. this.expected = this.negate ? 'not present' : 'present';
  41. this.actual = this.resultValue === '' ? 'not present' : 'present';
  42. };
  43. CssAssertion.prototype.elementFound = function() {
  44. if (this.retries > 0 && this.negate) {
  45. return;
  46. }
  47. if (this.passed && this.waitForMs) {
  48. var message = 'property was present';
  49. if (this.hasCondition()) {
  50. message = 'condition was met';
  51. }
  52. this.elapsedTime = this.getElapsedTime();
  53. this.messageParts.push(' - ' + message + ' in ' + this.elapsedTime + 'ms');
  54. }
  55. };
  56. CssAssertion.prototype.elementNotFound = function() {
  57. this.passed = false;
  58. };
  59. module.exports = CssAssertion;