Skip to content

Commit

Permalink
make sure validation browsers do not stay open
Browse files Browse the repository at this point in the history
  • Loading branch information
eyaler committed Jul 6, 2022
1 parent 091e0ec commit f6b94ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

[ ] Add validation testing for Safari

[ ] Keep browsers open for faster validation and quit when finished


### Encoding / Compression

Expand Down
36 changes: 16 additions & 20 deletions ztml/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,26 @@
from . import text_utils


default_browsers = ['chrome', 'edge', 'firefox']
default_element_name = 'body'
default_timeout = 60
default_element_name = 'body'


options = {}
webdriver = {}
logging.getLogger('WDM').setLevel(logging.NOTSET)
for browser, driver, Driver, Manager in zip(default_browsers,
[chrome, edge, firefox],
[Chrome, Edge, Firefox],
[ChromeDriverManager, EdgeChromiumDriverManager, GeckoDriverManager]):
options[browser] = driver.options.Options()
options[browser].headless = True
webdriver[browser] = Driver(service=driver.service.Service(Manager().install()), options=options[browser])
drivers = dict(chrome=[Chrome, chrome, ChromeDriverManager().install()],
edge=[Edge, edge, EdgeChromiumDriverManager().install()],
firefox=[Firefox, firefox, GeckoDriverManager().install()]
)


def render_html(filename: str, browser: str = default_browsers[0], timeout: int = default_timeout, element_name: str = default_element_name) -> Optional[str]:
b = webdriver[browser]
b.get('file:///' + os.path.abspath(filename))
try:
return WebDriverWait(b, timeout).until(lambda x: x.find_element(by=By.TAG_NAME, value=element_name).text)
except TimeoutException:
return None
def render_html(filename: str, browser: str = list(drivers)[0], timeout: int = default_timeout, element_name: str = default_element_name) -> Optional[str]:
options = drivers[browser][1].options.Options()
options.headless = True
with drivers[browser][0](service=drivers[browser][1].service.Service(drivers[browser][2], log_path=os.devnull), options=options) as b:
b.get('file:///' + os.path.abspath(filename))
try:
return WebDriverWait(b, timeout).until(lambda x: x.find_element(by=By.TAG_NAME, value=element_name).text)
except TimeoutException:
return None


def validate_html(filename: str,
Expand All @@ -47,7 +43,7 @@ def validate_html(filename: str,
eos: str = text_utils.default_eos,
ignore_regex: str = '',
unicode_A: int = 0,
browser: str = default_browsers[0],
browser: str = list(drivers)[0],
timeout: int = default_timeout,
element_name: str = default_element_name,
verbose: bool = True
Expand Down Expand Up @@ -94,7 +90,7 @@ def validate_files(filenames: dict[str, str],
verbose: bool = True
) -> None:
if browsers is None:
browsers = default_browsers
browsers = list(drivers)
text_size = None
base64_size = None
for label, filename in filenames.items():
Expand Down

0 comments on commit f6b94ef

Please sign in to comment.