setup_args3.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. time.sleep(0.2)