def browser_has_js_returned(expected: Any, script: str, *args) -> Condition[Browser]: def script_result(browser: Browser): return browser.driver.execute_script(script, *args) return BrowserCondition.raise_if_not_actual( f'has the ```{script}``` script returned {expected}', script_result, predicate.equals(expected))
def produced_todos(number: int) -> Condition[Browser]: def fn(entity: Browser) -> None: size = len(browser.all("#todo-list>li")) produced_enough = size >= number if not produced_enough: entity.element('#new-todo').type('one more').press_enter() raise AssertionError(f'actual produced todos were: {size}') return BrowserCondition(f'have produced {number} todos', fn)
def browser_has_tabs_number( expected: int, describing_matched_to='has tabs number', compared_by_predicate_to=predicate.equals) -> Condition[Browser]: def tabs_number(browser: Browser) -> int: return len(browser.driver.window_handles) return BrowserCondition.raise_if_not_actual( f'{describing_matched_to} {expected}', tabs_number, compared_by_predicate_to(expected))
def browser_has_title( expected: str, describing_matched_to='has title', compared_by_predicate_to=predicate.equals) -> Condition[Browser]: def title(browser: Browser) -> str: return browser.driver.title return BrowserCondition.raise_if_not_actual( f"{describing_matched_to} '{expected}'", title, compared_by_predicate_to(expected))
def browser_has_url( expected: str, describing_matched_to='has url', compared_by_predicate_to=predicate.equals) -> Condition[Browser]: def url(browser: Browser) -> str: return browser.driver.current_url return BrowserCondition.raise_if_not_actual( f"{describing_matched_to} '{expected}'", url, compared_by_predicate_to(expected))
def notification_on_reload(message: str) -> BrowserCondition: def fn(entity: Browser): entity.element('[href*=notification_message]').click() notification = entity.element('#flash') webelement = notification() actual = webelement.text expected = message if actual != expected: raise AssertionError(f'notification message was wrong:' f'\texpected: {expected}' f'\t actual: {actual}') return BrowserCondition( f'received message {message} on reload', fn, )