base.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os.path
  2. import os
  3. import subprocess
  4. import shlex
  5. import shutil
  6. from selenium import webdriver
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.common.keys import Keys
  9. from selenium.webdriver.support import expected_conditions as EC
  10. from selenium.webdriver.support.wait import WebDriverWait
  11. cwd = os.path.dirname(os.path.realpath(__file__))
  12. class Sample():
  13. def setUp(self):
  14. super().setUp()
  15. self.proc = None
  16. self.outputs = []
  17. def tearDown(self):
  18. super().tearDown()
  19. if self.proc is not None:
  20. self.proc.stdin.close()
  21. self.proc.stdout.close()
  22. self.proc.kill()
  23. def replaceVariables(self, filein ,fileout, vars):
  24. with open(filein, "rt") as fin:
  25. with open(fileout, "wt") as fout:
  26. for line in fin:
  27. for k, v in vars.items():
  28. line = line.replace(k, v)
  29. fout.write(line)
  30. def run_sample(self, filepath, variables):
  31. inpath = os.path.join(cwd, "..", "..", "docs", "examples", filepath)
  32. outpath = os.path.join(cwd, "tmp_{}".format(filepath))
  33. self.replaceVariables(inpath, outpath, variables)
  34. self.proc = subprocess.Popen(
  35. [shutil.which("python"),
  36. outpath],
  37. text=True, bufsize=1,
  38. stdin=subprocess.PIPE,
  39. stdout=subprocess.PIPE
  40. )
  41. def write(self, string):
  42. self.proc.stdin.write(string)
  43. self.proc.stdin.flush()
  44. def wait_for_pattern(self, pattern):
  45. try:
  46. while True:
  47. line = self.proc.stdout.readline()
  48. self.outputs.append(line)
  49. if pattern in line:
  50. return line
  51. except subprocess.TimeoutExpired:
  52. self.assertTrue(False, "timeout when looking for output")
  53. def wait_for_end(self):
  54. try:
  55. outs, err = self.proc.communicate(timeout=10)
  56. self.outputs += filter(lambda x: x != '', outs.split('\n'))
  57. except subprocess.TimeoutExpired:
  58. self.assertTrue(False, "timeout when looking for output")
  59. return self.outputs[-1]
  60. class Browser():
  61. def setUp(self):
  62. super().setUp()
  63. options = webdriver.ChromeOptions()
  64. options.add_argument("--headless=new")
  65. self.driver = webdriver.Chrome(options=options)
  66. self.user_username = os.environ.get("AUTH0_USERNAME")
  67. self.user_password = os.environ.get("AUTH0_PASSWORD")
  68. if not self.user_username or not self.user_password:
  69. self.skipTest("auth0 is not configured properly")
  70. def tearDown(self):
  71. super().tearDown()
  72. self.driver.quit()
  73. def authorize_auth0(self, authorize_url, expected_redirect_uri):
  74. self.driver.get(authorize_url)
  75. username = self.driver.find_element(By.ID, "username")
  76. password = self.driver.find_element(By.ID, "password")
  77. wait = WebDriverWait(self.driver, timeout=2)
  78. wait.until(lambda d : username.is_displayed())
  79. wait.until(lambda d : password.is_displayed())
  80. username.clear()
  81. username.send_keys(self.user_username)
  82. password.send_keys(self.user_password)
  83. username.send_keys(Keys.RETURN)
  84. wait.until(EC.url_contains(expected_redirect_uri))
  85. return self.driver.current_url