checklist.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { describe, it, before, after } from 'mocha'
  2. import { expect } from 'chai'
  3. import { By, Condition } from 'selenium-webdriver'
  4. import {
  5. DEFAULT_UI_WAIT_MS,
  6. getRootAndWait,
  7. getActionButton,
  8. takeScreenshotOnFailure,
  9. getTerminalBuffer,
  10. waitForArgumentFormPage,
  11. waitForArgumentFormReady,
  12. waitForLogsPage,
  13. waitForExecutionComplete,
  14. } from '../../lib/elements.js'
  15. async function openChecklistArgumentForm(actionTitle = 'Test checklist argument') {
  16. await getRootAndWait()
  17. const btn = await getActionButton(webdriver, actionTitle)
  18. await btn.click()
  19. await waitForArgumentFormPage()
  20. await waitForArgumentFormReady()
  21. }
  22. async function submitChecklistForm() {
  23. const submitButton = await webdriver.findElement(By.css('button[name="start"]'))
  24. await submitButton.click()
  25. }
  26. async function pollTerminal(matcher, timeoutMs = DEFAULT_UI_WAIT_MS) {
  27. await webdriver.wait(
  28. new Condition('wait for terminal output', async () => {
  29. try {
  30. const terminalReady = await webdriver.executeScript(`
  31. return !!(window.terminal && window.terminal.getBufferAsString);
  32. `)
  33. if (!terminalReady) {
  34. return false
  35. }
  36. const output = await getTerminalBuffer()
  37. if (!output) {
  38. return false
  39. }
  40. return matcher(output.trim())
  41. } catch (e) {
  42. return false
  43. }
  44. }),
  45. timeoutMs
  46. )
  47. }
  48. async function waitForTerminalOutput(expectedValue, label = 'Selected segments') {
  49. await pollTerminal(
  50. (output) => output.includes(`${label}: ${expectedValue}`),
  51. DEFAULT_UI_WAIT_MS
  52. )
  53. }
  54. async function waitForTerminalOutputPattern(pattern) {
  55. await pollTerminal(
  56. (output) => pattern.test(output),
  57. DEFAULT_UI_WAIT_MS
  58. )
  59. }
  60. async function waitForChecklistValue(expectedValue) {
  61. await webdriver.wait(
  62. new Condition('wait for checklist hidden value', async () => {
  63. const valueInput = await webdriver.findElement(By.css('.choice-checklist > input'))
  64. return (await valueInput.getAttribute('value')) === expectedValue
  65. }),
  66. DEFAULT_UI_WAIT_MS
  67. )
  68. }
  69. async function getCheckboxByValueIndex(index) {
  70. const checkboxes = await webdriver.findElements(
  71. By.css('.choice-checklist-item input[type="checkbox"]')
  72. )
  73. return checkboxes[index]
  74. }
  75. describe('config: checklist', function () {
  76. this.timeout(10000)
  77. before(async function () {
  78. await runner.start('checklist')
  79. })
  80. after(async () => {
  81. await runner.stop()
  82. })
  83. afterEach(function () {
  84. takeScreenshotOnFailure(this.currentTest, webdriver)
  85. })
  86. it('Checklist argument renders multiple checkbox inputs', async function () {
  87. await openChecklistArgumentForm()
  88. const kitchen = await getCheckboxByValueIndex(0)
  89. const bedroom = await getCheckboxByValueIndex(1)
  90. const hallway = await getCheckboxByValueIndex(2)
  91. expect(await kitchen.getAttribute('type')).to.equal('checkbox')
  92. expect(await bedroom.getAttribute('type')).to.equal('checkbox')
  93. expect(await hallway.getAttribute('type')).to.equal('checkbox')
  94. expect(await kitchen.isSelected()).to.be.true
  95. expect(await bedroom.isSelected()).to.be.true
  96. expect(await hallway.isSelected()).to.be.false
  97. })
  98. it('Checklist select none submits an empty value', async function () {
  99. await openChecklistArgumentForm()
  100. const selectNone = await webdriver.findElement(By.xpath("//button[normalize-space()='Select none']"))
  101. await selectNone.click()
  102. await waitForChecklistValue('')
  103. const valueInput = await webdriver.findElement(By.css('.choice-checklist > input'))
  104. expect(await valueInput.getAttribute('value')).to.equal('')
  105. await submitChecklistForm()
  106. await waitForLogsPage()
  107. await waitForExecutionComplete()
  108. await waitForTerminalOutputPattern(/Selected segments:\s*(\r?\n|$)/)
  109. })
  110. it('Checklist select all submits every choice value', async function () {
  111. await openChecklistArgumentForm()
  112. const selectNone = await webdriver.findElement(By.xpath("//button[normalize-space()='Select none']"))
  113. await selectNone.click()
  114. const selectAll = await webdriver.findElement(By.xpath("//button[normalize-space()='Select all']"))
  115. await selectAll.click()
  116. await submitChecklistForm()
  117. await waitForLogsPage()
  118. await waitForExecutionComplete()
  119. await waitForTerminalOutput('["kitchen","bedroom","hallway"]')
  120. })
  121. it('Checklist toggles individual choices before submit', async function () {
  122. await openChecklistArgumentForm()
  123. const hallway = await getCheckboxByValueIndex(2)
  124. await hallway.click()
  125. await submitChecklistForm()
  126. await waitForLogsPage()
  127. await waitForExecutionComplete()
  128. await waitForTerminalOutput('["kitchen","bedroom","hallway"]')
  129. })
  130. it('Checklist entity argument renders choices from entities', async function () {
  131. await openChecklistArgumentForm('Test checklist entity argument')
  132. const checkboxes = await webdriver.findElements(
  133. By.css('.choice-checklist-item input[type="checkbox"]')
  134. )
  135. expect(checkboxes).to.have.length(2)
  136. const labels = await webdriver.findElements(By.css('.choice-checklist-item span'))
  137. expect(await labels[0].getText()).to.equal('attic')
  138. expect(await labels[1].getText()).to.equal('basement')
  139. await checkboxes[0].click()
  140. await submitChecklistForm()
  141. await waitForLogsPage()
  142. await waitForExecutionComplete()
  143. await waitForTerminalOutput('["attic"]', 'Selected rooms')
  144. })
  145. })