4
0

elements.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { By } from 'selenium-webdriver'
  2. import fs from 'fs'
  3. import { expect } from 'chai'
  4. import { Condition } from 'selenium-webdriver'
  5. export async function getActionButtons () {
  6. // Currently, only the active dashboard's contents are rendered,
  7. // so we don't need to scope the selector by dashboard title.
  8. return await webdriver.findElements(By.css('.action-button button'))
  9. }
  10. export async function getExecutionDialogOutput() {
  11. await webdriver.wait(new Condition('Dialog with long int is visible', async () => {
  12. const dialog = await webdriver.findElement({ id: 'execution-results-popup' })
  13. return await dialog.isDisplayed()
  14. }));
  15. const ret = await webdriver.executeScript('return window.logEntries.get(window.executionDialog.executionTrackingId).output')
  16. return ret
  17. }
  18. export async function closeExecutionDialog() {
  19. const btnClose = await webdriver.findElements(By.css('[title="Close"]'))
  20. await btnClose[0].click()
  21. }
  22. export function takeScreenshotOnFailure (test, webdriver) {
  23. if (test.state === 'failed') {
  24. const title = test.fullTitle();
  25. console.log(`Test failed, taking screenshot: ${title}`);
  26. takeScreenshot(webdriver, title);
  27. }
  28. }
  29. export function takeScreenshot (webdriver, title) {
  30. return webdriver.takeScreenshot().then((img) => {
  31. fs.mkdirSync('screenshots', { recursive: true });
  32. title = title.replaceAll('config: ', '')
  33. title = title.replaceAll(/[\(\)\|\*\<\>\:]/g, "_")
  34. title = title + '.failed-test'
  35. fs.writeFileSync('screenshots/' + title + '.png', img, 'base64')
  36. })
  37. }
  38. export async function getRootAndWait() {
  39. await webdriver.get(runner.baseUrl())
  40. await webdriver.wait(new Condition('wait for loaded-dashboard', async function() {
  41. const body = await webdriver.findElement(By.tagName('body'))
  42. const attr = await body.getAttribute('loaded-dashboard')
  43. console.log('loaded-dashboard: ', attr)
  44. if (attr) {
  45. return true
  46. } else {
  47. return false
  48. }
  49. }))
  50. }
  51. export async function closeSidebar() {
  52. await webdriver.findElement(By.id('sidebar-toggler-button')).click()
  53. const sidebar = await webdriver.findElement(By.id('mainnav'))
  54. const neededLeft = '-250px' // Assuming sidebar is closed at this position
  55. let lastLeft = ''
  56. await webdriver.wait(new Condition('wait for sidebar to close', async function() {
  57. const left = await sidebar.getCssValue('left')
  58. if (left !== lastLeft) {
  59. lastLeft = left
  60. console.log('Sidebar left changed to: ', left)
  61. return false
  62. } else {
  63. console.log('Sidebar closed, left is: *' + left, left === neededLeft ? ' (as expected)' : '')
  64. return left === neededLeft
  65. }
  66. }), 10000); // Wait up to 10 seconds for the sidebar to close
  67. }
  68. export async function openSidebar() {
  69. await webdriver.findElement(By.id('sidebar-toggler-button')).click()
  70. const sidebar = await webdriver.findElement(By.id('mainnav'))
  71. let lastLeft = 0
  72. await webdriver.wait(new Condition('wait for sidebar to open', async function() {
  73. const left = await sidebar.getCssValue('left')
  74. if (left !== lastLeft) {
  75. lastLeft = left
  76. console.log('Sidebar left changed to: ', left)
  77. return false
  78. } else {
  79. console.log('Sidebar opened, left is: ', left)
  80. return true
  81. }
  82. }));
  83. }
  84. export async function getNavigationLinks() {
  85. const navigationLinks = await webdriver.findElements(By.css('.navigation-links li'))
  86. return navigationLinks
  87. }
  88. export async function requireExecutionDialogStatus (webdriver, expected) {
  89. await webdriver.wait(new Condition('wait for action to be running', async function () {
  90. const dialogStatus = await webdriver.findElement(By.id('execution-dialog-status'))
  91. const actual = await dialogStatus.getText()
  92. if (actual === expected) {
  93. return true
  94. } else {
  95. console.log('Waiting for domStatus text to be: ', expected, ', it is currently: ', actual)
  96. return false
  97. }
  98. }))
  99. }
  100. export async function findExecutionDialog (webdriver) {
  101. return webdriver.findElement(By.id('execution-results-popup'))
  102. }
  103. export async function getActionButton (webdriver, title) {
  104. const buttons = await webdriver.findElements(By.css('[title="' + title + '"]'))
  105. expect(buttons).to.have.length(1)
  106. return buttons[0]
  107. }
  108. export async function getTerminalBuffer() {
  109. try {
  110. const output = await webdriver.executeScript(`
  111. if (window.terminal && window.terminal.getBufferAsString) {
  112. return window.terminal.getBufferAsString();
  113. }
  114. return null;
  115. `)
  116. return output
  117. } catch (e) {
  118. console.log('[getTerminalBuffer] Error:', e.message)
  119. return null
  120. }
  121. }