waitForElementVisible.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var util = require('util');
  2. var WaitForElementPresent = require('./waitForElementPresent.js');
  3. /**
  4. * Waits a given time in milliseconds for an element to be visible in the page before performing any other commands or assertions.
  5. *
  6. * If the element fails to be present and visible 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.waitForElementVisible('body', 1000);
  15. * // continue if failed
  16. * browser.waitForElementVisible('body', 1000, false);
  17. * // with callback
  18. * browser.waitForElementVisible('body', 1000, function() {
  19. * // do something while we're here
  20. * });
  21. * // custom Spanish message
  22. * browser.waitForElementVisible('body', 1000, 'elemento %s no era visible en %d ms');
  23. * // many combinations possible - the message is always the last argument
  24. * browser.waitForElementVisible('body', 1000, false, function() {}, 'elemento %s no era visible en %d ms');
  25. * };
  26. * ```
  27. *
  28. * @method waitForElementVisible
  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 WaitForElementVisible() {
  37. WaitForElementPresent.call(this);
  38. this.expectedValue = 'visible';
  39. }
  40. util.inherits(WaitForElementVisible, WaitForElementPresent);
  41. WaitForElementVisible.prototype.elementFound = function(result, now) {
  42. return this.isVisible();
  43. };
  44. WaitForElementVisible.prototype.elementVisible = function(result, now) {
  45. var defaultMsg = 'Element <%s> was visible after %d milliseconds.';
  46. return this.pass(result, defaultMsg, now - this.startTimer);
  47. };
  48. WaitForElementVisible.prototype.elementNotVisible = function(result, now) {
  49. if (now - this.startTimer < this.ms) {
  50. // element wasn't visible, schedule another check
  51. this.reschedule('isVisible');
  52. return this;
  53. }
  54. var defaultMsg = 'Timed out while waiting for element <%s> to be visible for %d milliseconds.';
  55. return this.fail(result, 'not visible', this.expectedValue, defaultMsg);
  56. };
  57. module.exports = WaitForElementVisible;