setup_args3.py 2.4 KB

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