elementHasCount.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * A custom Nightwatch assertion. The assertion name is the filename.
  3. *
  4. * Example usage:
  5. * browser.assert.elementHasCount(selector, count)
  6. *
  7. * For more information on custom assertions see:
  8. * https://nightwatchjs.org/guide/extending-nightwatch/adding-custom-assertions.html
  9. *
  10. */
  11. exports.assertion = function elementHasCount(selector: string, count: number) {
  12. // Message to be displayed on the console while running this assertion.
  13. this.message = `Testing if element <${selector}> has count: ${count}`
  14. // Expected value of the assertion, to be displayed in case of failure.
  15. this.expected = count
  16. // Given the result value (from `this.value` below), this function will
  17. // evaluate if the assertion has passed.
  18. this.evaluate = function (value: any) {
  19. return value === count
  20. }
  21. // Retrieve the value from the result object we got after running the
  22. // assertion command (defined below), which is to be evaluated against
  23. // the value passed into the assertion as the second argument.
  24. this.value = function (result: Record<string, any>) {
  25. return result.value
  26. }
  27. // Script to be executed in the browser to find the actual element count.
  28. function elementCountScript(_selector: string) {
  29. // eslint-disable-next-line
  30. return document.querySelectorAll(_selector).length
  31. }
  32. // The command to be executed by the assertion runner, to find the actual
  33. // result. Nightwatch API is available as `this.api`.
  34. this.command = function (callback: () => void) {
  35. this.api.execute(elementCountScript, [selector], callback)
  36. }
  37. }