Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

116 regels
2.4 KiB

  1. /// <reference types="node" />
  2. import { readFileSync, statSync } from 'fs'
  3. import { join, basename } from 'path'
  4. class JSDOMJQuery {
  5. private selectedElements: Node[]
  6. constructor(elements: NodeList) {
  7. this.selectedElements = Array.from(elements)
  8. }
  9. type(sRaw: string) {
  10. const s = sRaw.replace(/\{enter}/g, '\n');
  11. this.selectedElements.forEach((el: any) => {
  12. if (el.tagName === 'TEXTAREA') {
  13. el.innerText = s
  14. el.value = s
  15. return
  16. }
  17. if (el.type === 'datetime-local') {
  18. el.value = new Date(`${s}:00.000Z`).toISOString().slice(0, 'yyyy-MM-DDTHH:mm'.length)
  19. return
  20. }
  21. el.setAttribute('value', s)
  22. el.value = s
  23. })
  24. return this
  25. }
  26. check() {
  27. this.selectedElements.forEach((el: any) => {
  28. el.setAttribute('checked', '')
  29. el.checked = true
  30. })
  31. return this
  32. }
  33. select(v: string) {
  34. this.selectedElements.forEach((el: any) => {
  35. const option: any = Array.from(el.querySelectorAll('option')).find((o: any) => o.textContent === v)
  36. option.setAttribute('selected', '')
  37. el.value = option.value
  38. })
  39. return this
  40. }
  41. last() {
  42. this.selectedElements = this.selectedElements.slice(-1)
  43. return this
  44. }
  45. then(fn: (...args: unknown[]) => unknown) {
  46. fn(this.selectedElements)
  47. return this
  48. }
  49. click() {
  50. return this
  51. }
  52. submit() {
  53. return this
  54. }
  55. invoke(key: string, value: unknown) {
  56. if (key === 'val') {
  57. this.selectedElements.forEach((el) => (el as unknown as Record<string, unknown>).valueAsNumber = value);
  58. }
  59. return this
  60. }
  61. trigger(which: string) {
  62. return this
  63. }
  64. attachFile(filename: string | string[]) {
  65. const { File, FileList } = window;
  66. const theFilenames = Array.isArray(filename) ? filename : [filename];
  67. const theFiles = theFilenames.map((f) => {
  68. const filePath = join('cypress', 'fixtures', f);
  69. const { mtimeMs: lastModified } = statSync(filePath);
  70. const contents = readFileSync(filePath);
  71. return new File(
  72. [contents],
  73. basename(filePath),
  74. {
  75. lastModified,
  76. type: '',
  77. },
  78. );
  79. });
  80. (theFiles as unknown as Record<string, unknown>).__proto__ = Object.create(FileList.prototype);
  81. this.selectedElements.forEach((el) => {
  82. Object.defineProperty(el, 'files', {
  83. value: theFiles,
  84. writable: false,
  85. })
  86. });
  87. return this;
  88. }
  89. }
  90. export default class JSDOMDummyCypress {
  91. private currentElement = window.document;
  92. wait(time: number) {
  93. return this;
  94. }
  95. get(q: string) {
  96. return new JSDOMJQuery(this.currentElement.querySelectorAll(q));
  97. }
  98. }