cssClassPresent.js 1.4 KB

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