setup_dropdown_logs_detail.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. """Prepare the execution results screenshot for a dropdown action."""
  3. import time
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. def _wait_for_dashboard(driver, timeout=15):
  7. WebDriverWait(driver, timeout).until(
  8. lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
  9. )
  10. WebDriverWait(driver, timeout).until(
  11. lambda d: d.execute_script("return !!window.client")
  12. )
  13. def _wait_for_logs_page(driver, timeout=15):
  14. WebDriverWait(driver, timeout).until(
  15. lambda d: "/logs/" in d.current_url and not d.current_url.rstrip("/").endswith("/logs")
  16. )
  17. def _wait_for_execution_complete(driver, timeout=15):
  18. def finished(d):
  19. try:
  20. status = d.find_element(By.CSS_SELECTOR, ".execution-dialog-status").text
  21. except Exception:
  22. return False
  23. return "Still running" not in status and "Queued" not in status
  24. WebDriverWait(driver, timeout).until(finished)
  25. def _wait_for_terminal_output(driver, expected, timeout=15):
  26. WebDriverWait(driver, timeout).until(
  27. lambda d: expected
  28. in d.execute_script(
  29. """
  30. if (!window.terminal || !window.terminal.getBufferAsString) {
  31. return '';
  32. }
  33. return window.terminal.getBufferAsString();
  34. """
  35. )
  36. )
  37. def run(driver):
  38. _wait_for_dashboard(driver)
  39. driver.execute_async_script(
  40. """
  41. const done = arguments[arguments.length - 1];
  42. const button = document.querySelector('[title="Print a message"]');
  43. const bindingId = button.closest('.action-button').id.replace('actionButton-', '');
  44. window.client.startAction({
  45. bindingId: bindingId,
  46. arguments: [{ name: 'message', value: 'Hello there!' }],
  47. uniqueTrackingId: 'doc-screenshot-' + Date.now(),
  48. }).then((response) => {
  49. window.location.href = '/logs/' + response.executionTrackingId;
  50. done(true);
  51. }).catch((err) => done(String(err)));
  52. """
  53. )
  54. _wait_for_logs_page(driver)
  55. _wait_for_execution_complete(driver)
  56. _wait_for_terminal_output(driver, "Hello there!")
  57. time.sleep(0.2)