|
| 1 | +#!python |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import time |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from selenium import webdriver |
| 8 | +from selenium.webdriver.common.by import By |
| 9 | +from selenium.webdriver.remote.webelement import WebElement |
| 10 | + |
| 11 | + |
| 12 | +def sub_book_path(text: str) -> str: |
| 13 | + return re.sub( |
| 14 | + r"@file://(.+)/superlists/src/", |
| 15 | + ".../goat-book/src/", |
| 16 | + text, |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def run(path: Path): |
| 21 | + assert path.exists() |
| 22 | + |
| 23 | + options = webdriver.FirefoxOptions() |
| 24 | + options.add_argument("--headless") |
| 25 | + browser = webdriver.Firefox(options=options) |
| 26 | + failed = False |
| 27 | + |
| 28 | + def _el_text(sel, node: webdriver.Remote | WebElement = browser): |
| 29 | + return "\n".join(el.text for el in node.find_elements(By.CSS_SELECTOR, sel)) |
| 30 | + |
| 31 | + try: |
| 32 | + browser.get(f"file:///{path}?seed=12345") |
| 33 | + time.sleep(0.2) |
| 34 | + |
| 35 | + print( |
| 36 | + f"{_el_text('.jasmine-overall-result')}\t\t{_el_text('.jasmine-duration')}" |
| 37 | + ) |
| 38 | + |
| 39 | + print(_el_text(".jasmine-menu.jasmine-failure-list")) |
| 40 | + for failures_el in browser.find_elements(By.CSS_SELECTOR, ".jasmine-failures"): |
| 41 | + for spec_failure in failures_el.find_elements( |
| 42 | + By.CSS_SELECTOR, ".jasmine-spec-detail.jasmine-failed" |
| 43 | + ): |
| 44 | + failed = True |
| 45 | + print() |
| 46 | + print(_el_text(".jasmine-description", spec_failure)) |
| 47 | + print(_el_text(".jasmine-messages", spec_failure)) |
| 48 | + |
| 49 | + for success_el in browser.find_elements(By.CSS_SELECTOR, ".jasmine-summary"): |
| 50 | + for suite_el in success_el.find_elements(By.CSS_SELECTOR, ".jasmine-suite"): |
| 51 | + if suite_el.is_displayed(): |
| 52 | + print(_el_text("li.jasmine-suite-detail", suite_el)) |
| 53 | + for spec_el in suite_el.find_elements( |
| 54 | + By.CSS_SELECTOR, "ul.jasmine-specs li.jasmine-passed" |
| 55 | + ): |
| 56 | + print(" * " + spec_el.text) |
| 57 | + |
| 58 | + finally: |
| 59 | + browser.quit() |
| 60 | + return failed |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + _, fn, *__ = sys.argv |
| 65 | + if fn.endswith("Spec.js"): |
| 66 | + fn = fn.replace("Spec.js", "SpecRunner.html") |
| 67 | + failed = run(Path(fn).resolve()) |
| 68 | + sys.exit(1 if failed is True else 0) |
0 commit comments