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