Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

75 linhas
1.4 KiB

  1. /// <reference types="node" />
  2. import { readFileSync } from 'fs'
  3. import { join } from 'path'
  4. class JSDOMJQuery {
  5. private selectedElements: Node[]
  6. constructor(elements: NodeList) {
  7. this.selectedElements = Array.from(elements)
  8. }
  9. type(s: string) {
  10. this.selectedElements.forEach((el: any) => {
  11. if (el.tagName === 'TEXTAREA') {
  12. el.innerText = s
  13. el.value = s
  14. return
  15. }
  16. el.setAttribute('value', s)
  17. el.value = s
  18. })
  19. return this
  20. }
  21. check() {
  22. this.selectedElements.forEach((el: any) => {
  23. el.setAttribute('checked', '')
  24. el.checked = true
  25. })
  26. return this
  27. }
  28. select(v: string) {
  29. this.selectedElements.forEach((el: any) => {
  30. const option: any = Array.from(el.querySelectorAll('option')).find((o: any) => o.textContent === v)
  31. option.setAttribute('selected', '')
  32. el.value = option.value
  33. })
  34. return this
  35. }
  36. last() {
  37. this.selectedElements = this.selectedElements.slice(-1)
  38. return this
  39. }
  40. then(fn: (...args: unknown[]) => unknown) {
  41. fn(this.selectedElements)
  42. return this
  43. }
  44. click() {
  45. return this
  46. }
  47. submit() {
  48. return this
  49. }
  50. attachFile(filename: string) {
  51. const contents = readFileSync(join('cypress', 'fixtures', filename))
  52. // TODO
  53. contents.toString('binary')
  54. return this
  55. }
  56. }
  57. export default class JSDOMDummyCypress {
  58. private currentElement = window.document;
  59. get(q: string) {
  60. return new JSDOMJQuery(this.currentElement.querySelectorAll(q));
  61. }
  62. }