pytest_dash package¶
Submodules¶
pytest_dash.application_runners module¶
Run dash applications with a context manager. When exiting the context, the server will close.
-
class
pytest_dash.application_runners.
BaseDashRunner
(driver, keep_open=False)[source]¶ Base context manager class for running applications.
-
__init__
(driver, keep_open=False)[source]¶ Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- keep_open (bool) – Keep the server open
-
url
¶ The url with the port auto-formatted.
Returns: Formatted url with the port.
-
-
class
pytest_dash.application_runners.
DashSubprocess
(driver, keep_open=False)[source]¶ Bases:
pytest_dash.application_runners.BaseDashRunner
Runs a dash application in a waitress-serve subprocess.
-
class
pytest_dash.application_runners.
DashThreaded
(driver, keep_open=False)[source]¶ Bases:
pytest_dash.application_runners.BaseDashRunner
Runs a dash application in a thread.
-
start
(app, port=8050, start_wait_time=0.5, start_timeout=10, **kwargs)[source]¶ Start the threaded dash app server.
See also
Parameters: - app (dash.Dash) – The dash application instance.
- port (int) – Port of the dash application.
- start_wait_time (float) – Poll rate for the server started wait
- start_timeout (float) – Max time to start the server.
- kwargs –
Returns:
-
-
pytest_dash.application_runners.
import_app
(app_file, application_name='app')[source]¶ Import a dash application from a module. The import path is in dot notation to the module. The variable named app will be returned.
Example: >>> app = import_app('my_app.app')
Will import the application in module app of the package my_app.
Parameters: - app_file (str) – Path to the app (dot-separated).
- application_name – The name of the dash application instance.
Raise: pytest_dash.errors.NoAppFoundError
Returns: App from module.
Return type: dash.Dash
pytest_dash.behavior_parser module¶
Custom lark parser and transformer for dash behavior tests.
-
class
pytest_dash.behavior_parser.
BehaviorTransformer
(driver, variables=None)[source]¶ Bases:
lark.visitors.Transformer
,object
Transform and execute behavior commands.
-
__init__
(driver, variables=None)[source]¶ Parameters: driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver to find elements in the tree
-
element_id
(*args, **kwargs)[source]¶ Find an element by id when found in the tree.
Example: #dropdown
Kind: value Parameters: element_id – Text after #
-
element_prop
(*args, **kwargs)[source]¶ Property value of an element
Example: #element.prop
Kind: value
-
element_selector
(*args, **kwargs)[source]¶ Find an element by selector when found in the tree.
Example: {#radio-items > label:nth-child(9) > input[type="radio"]}
Kind: value Parameters: selector – Text contained between { & }
-
elements_xpath
(*args, **kwargs)[source]¶ Find all elements by xpath
Example: *[//div/span]
Kind: value
-
escape_string
(*args, **kwargs)[source]¶ Escaped string handler, remove the
"
from the token.Kind: value
-
prop_compare
(*args, **kwargs)[source]¶ Wait for a property to equal a value.
Example: #output.id should be "my-element"
Kind: comparison
-
send_value
(*args, **kwargs)[source]¶ Send key inputs to the element
Example: enter "Hello" in #input
Kind: command
-
style_compare
(*args, **kwargs)[source]¶ Compare a style value of an of element.
Example: style “color” of #style should be “rgba(0, 0, 255, 1)”
Kind: comparison
Parameters: - style – Name of the style property
- element – Element to find
- _ – eq
- value – Value to compare to the element style attribute.
Returns:
-
-
class
pytest_dash.behavior_parser.
BehaviorTransformerMeta
[source]¶ Bases:
type
Dynamically create a parser transformer with user defined behaviors
-
pytest_dash.behavior_parser.
parser_factory
(driver, variables=None, behaviors=None)[source]¶ Create a Lark parser with a BehaviorTransformer with the provided selenium driver to find the elements.
A new behavior transformer class is created and behaviors are assigned a transformer function from the supplied behaviors in the pytest_add_behaviors hook.
Parameters: - driver – Selenium driver to use when parsing elements.
- variables – Variables to use in the parser transformer.
- behaviors – Custom behaviors, come from plugin.behaviors.
Returns:
pytest_dash.behaviors module¶
Experimental behavioral test api for dash apps.
-
class
pytest_dash.behaviors.
DashBehaviorTestFile
(fspath, parent, plugin)[source]¶ Bases:
_pytest.nodes.File
A yaml test file definition
-
class
pytest_dash.behaviors.
DashBehaviorTestItem
(plugin, name, parent, spec, application=None, **kwargs)[source]¶ Bases:
_pytest.nodes.Item
A single test of a test file.
pytest_dash.errors module¶
Pytest-dash errors.
-
exception
pytest_dash.errors.
DashAppLoadingError
[source]¶ Bases:
pytest_dash.errors.PytestDashError
The dash app failed to load
-
exception
pytest_dash.errors.
InvalidDriverError
[source]¶ Bases:
pytest_dash.errors.PytestDashError
An invalid selenium driver was specified.
-
exception
pytest_dash.errors.
MissingBehaviorError
[source]¶ Bases:
pytest_dash.errors.PytestDashError
A behavior was missing from the
-
exception
pytest_dash.errors.
NoAppFoundError
[source]¶ Bases:
pytest_dash.errors.PytestDashError
No app was found in the file.
-
exception
pytest_dash.errors.
PytestDashError
[source]¶ Bases:
exceptions.Exception
Base error for pytest-dash.
-
exception
pytest_dash.errors.
ServerCloseError
[source]¶ Bases:
pytest_dash.errors.PytestDashError
Pytest-dash had trouble closing a server.
pytest_dash.new_hooks module¶
Custom hooks for pytest dash
-
pytest_dash.new_hooks.
pytest_add_behaviors
(add_behavior)[source]¶ Use this hook to add custom behavior parsing.
Example conftest.py
def pytest_add_behavior(add_behavior): @add_behavior('Text to parse') def custom_parse_action(params): pass
Parameters: add_behavior – Decorator for a behavior handler function. Returns:
-
pytest_dash.new_hooks.
pytest_setup_selenium
(driver_name)[source]¶ Called before the driver is created, return a dictionary to use as kwargs for the driver init.
Parameters: driver_name (str) – The name of the driver specified by either cli argument or in pytest.ini. Returns: The dictionary of kwargs to give to the driver constructor.
pytest_dash.plugin module¶
Pytest-dash plugin¶
Main entry point for pytest
- Hooks definitions
- Plugin config container
- Plugin selenium driver
- Fixtures
-
class
pytest_dash.plugin.
DashPlugin
[source]¶ Bases:
object
Plugin configuration and selenium driver container
-
driver
¶
-
-
pytest_dash.plugin.
dash_subprocess
(*args, **kwargs)[source]¶ Start a Dash server with subprocess.Popen and waitress-serve.
Example: def test_application(dash_subprocess): # consider the application file is named `app.py` dash_subprocess('app')
-
pytest_dash.plugin.
dash_threaded
(*args, **kwargs)[source]¶ Start a local dash server in a new thread. Stop the server in teardown.
Example: import dash import dash_html_components as html def test_application(dash_threaded): app = dash.Dash(__name__) app.layout = html.Div('My app) dash_threaded(app)
pytest_dash.wait_for module¶
Utils methods for pytest-dash such wait_for wrappers
-
pytest_dash.wait_for.
wait_for_element_by_css_selector
(driver, selector, timeout=10.0)[source]¶ Wait until a single element is found and return it. This variant use the css selector api: https://www.w3schools.com/jsref/met_document_queryselector.asp
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- selector (str) – CSS selector to find.
- timeout (float) – Maximum time to find the element.
Returns:
-
pytest_dash.wait_for.
wait_for_element_by_id
(driver, _id, timeout=10)[source]¶ Wait until a single element is found and return it. This variant find by id.
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- _id – The id of the element to find.
- timeout (float) – Maximum time to find the element.
Returns:
-
pytest_dash.wait_for.
wait_for_element_by_xpath
(driver, xpath, timeout=10)[source]¶ Wait until a single element is found and return it. This variant use xpath to find the element. https://www.w3schools.com/xml/xml_xpath.asp
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- xpath (str) – Xpath query string.
- timeout (float) – Maximum time to find the element.
Returns:
-
pytest_dash.wait_for.
wait_for_elements_by_css_selector
(driver, selector, timeout=10.0)[source]¶ Wait until all elements are found by the selector before the timeout.
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- selector (str) – Search for elements
- timeout (float) – Maximum wait time
Returns: Found elements
-
pytest_dash.wait_for.
wait_for_elements_by_xpath
(driver, xpath, timeout=10)[source]¶ Wait until all are found before the timeout. This variant use xpath to find the elements. https://www.w3schools.com/xml/xml_xpath.asp
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- xpath (str) – Xpath query string.
- timeout (float) – Maximum time to find the element.
Returns:
-
pytest_dash.wait_for.
wait_for_property_to_equal
(driver, selector, prop_name, prop_value, timeout=10)[source]¶ Wait for an element property to equal a value.
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- selector (str) – Selector of the element to assert it’s property.
- prop_name (str) – The name of property.
- prop_value – The value to assert.
- timeout (float) – Maximum time.
Returns:
-
pytest_dash.wait_for.
wait_for_style_to_equal
(driver, selector, style_attribute, style_assertion, timeout=10)[source]¶ Wait for an element style attribute to equal.
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- selector (str) – Selector of the element to assert it’s style property.
- style_attribute (str) – The name of the CSS attribute to assert.
- style_assertion (str) – The value to equal of CSS attribute.
- timeout (float) – Maximum time.
Returns:
-
pytest_dash.wait_for.
wait_for_text_to_equal
(driver, selector, text, timeout=10)[source]¶ Wait an element text found by css selector is equal to text.
Parameters: - driver (selenium.webdriver.remote.webdriver.WebDriver) – Selenium driver
- selector (str) – Selector of the element to assert it’s text property.
- text (str) – Text to equal.
- timeout (float) – Maximum time for the text to equal.
Returns: