setup_layout.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """Show action buttons in idle, running, and queued states."""
  3. import time
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. _START_ACTION_JS = """
  7. const done = arguments[arguments.length - 1];
  8. const title = arguments[0];
  9. function bindingIdForTitle(actionTitle) {
  10. const button = document.querySelector('[title="' + actionTitle + '"]');
  11. if (!button) {
  12. throw new Error('Action button not found: ' + actionTitle);
  13. }
  14. return button.closest('.action-button').id.replace('actionButton-', '');
  15. }
  16. function uniqueTrackingId() {
  17. if (window.isSecureContext && window.crypto?.randomUUID) {
  18. return window.crypto.randomUUID();
  19. }
  20. return 'doc-screenshot-' + Date.now() + '-' + Math.random();
  21. }
  22. window.client.startAction({
  23. bindingId: bindingIdForTitle(title),
  24. arguments: [],
  25. uniqueTrackingId: uniqueTrackingId(),
  26. }).then(() => done(true)).catch((err) => done(String(err)));
  27. """
  28. def _wait_for_dashboard(driver, timeout=30):
  29. WebDriverWait(driver, timeout).until(
  30. lambda d: d.execute_script("return !!window.client")
  31. )
  32. WebDriverWait(driver, timeout).until(
  33. lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
  34. )
  35. def _start_action(driver, title):
  36. driver.execute_async_script(_START_ACTION_JS, title)
  37. def _wait_for_layout_states(driver, timeout=20):
  38. def ready(d):
  39. try:
  40. restart = d.find_element(By.CSS_SELECTOR, '[title="Restart service"]')
  41. running = d.find_element(
  42. By.CSS_SELECTOR,
  43. '[title="Long task"]'
  44. ).find_element(By.XPATH, './ancestor::div[contains(@class, "action-button")]//span[contains(@class, "execution-indicator-running")]')
  45. queued = d.find_element(
  46. By.CSS_SELECTOR,
  47. '[title="Backup job"]'
  48. ).find_element(By.XPATH, './ancestor::div[contains(@class, "action-button")]//span[contains(@class, "execution-indicator-queued")]')
  49. onclick = d.find_element(
  50. By.CSS_SELECTOR,
  51. '[title="Restart service"] .navigate-on-start',
  52. )
  53. except Exception:
  54. return False
  55. return all(
  56. element.is_displayed()
  57. for element in (restart, running, queued, onclick)
  58. )
  59. WebDriverWait(driver, timeout).until(ready)
  60. def run(driver):
  61. _wait_for_dashboard(driver)
  62. _start_action(driver, "Long task")
  63. time.sleep(0.3)
  64. _start_action(driver, "Backup job")
  65. _wait_for_layout_states(driver)
  66. time.sleep(0.2)