waitForElementPresent.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var util = require('util');
  2. var WaitForElement = require('./_waitForElement.js');
  3. /**
  4. * Waits a given time in milliseconds for an element to be present in the page before performing any other commands or assertions.
  5. *
  6. * If the element fails to be present in the specified amount of time, the test fails. You can change this by setting `abortOnFailure` to `false`.
  7. *
  8. * You can change the polling interval by defining a `waitForConditionPollInterval` property (in milliseconds) in as a global property in your `nightwatch.json` or in your external globals file.
  9. *
  10. * Similarly, a default timeout can be specified as a global `waitForConditionTimeout` property (in milliseconds).
  11. *
  12. * ```
  13. * this.demoTest = function (browser) {
  14. * browser.waitForElementPresent('body', 1000);
  15. * // continue if failed
  16. * browser.waitForElementPresent('body', 1000, false);
  17. * // with callback
  18. * browser.waitForElementPresent('body', 1000, function() {
  19. * // do something while we're here
  20. * });
  21. * // custom Spanish message
  22. * browser.waitForElementPresent('body', 1000, 'elemento %s no era presente en %d ms');
  23. * // many combinations possible - the message is always the last argument
  24. * browser.waitForElementPresent('body', 1000, false, function() {}, 'elemento %s no era presente en %d ms');
  25. * };
  26. * ```
  27. *
  28. * @method waitForElementPresent
  29. * @param {string} selector The selector (CSS / Xpath) used to locate the element.
  30. * @param {number} time The number of milliseconds to wait. The runner performs repeated checks every 500 ms.
  31. * @param {boolean} [abortOnFailure] By the default if the element is not found the test will fail. Set this to false if you wish for the test to continue even if the assertion fails. To set this globally you can define a property `abortOnAssertionFailure` in your globals.
  32. * @param {function} [callback] Optional callback function to be called when the command finishes.
  33. * @param {string} [message] Optional message to be shown in the output; the message supports two placeholders: %s for current selector and %d for the time (e.g. Element %s was not in the page for %d ms).
  34. * @api commands
  35. */
  36. function WaitForElementPresent() {
  37. WaitForElement.call(this);
  38. this.expectedValue = 'found';
  39. }
  40. util.inherits(WaitForElementPresent, WaitForElement);
  41. WaitForElementPresent.prototype.elementFound = function(result, now) {
  42. var defaultMsg = 'Element <%s> was present after %d milliseconds.';
  43. return this.pass(result, defaultMsg, now - this.startTimer);
  44. };
  45. WaitForElementPresent.prototype.elementNotFound = function(result, now) {
  46. if (now - this.startTimer < this.ms) {
  47. // element wasn't found, schedule another check
  48. this.reschedule();
  49. return this;
  50. }
  51. var defaultMsg = 'Timed out while waiting for element <%s> to be present for %d milliseconds.';
  52. return this.fail({value:false}, 'not found', this.expectedValue, defaultMsg);
  53. };
  54. module.exports = WaitForElementPresent;