elements.js 4.4 KB

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