Modules API Reference¶
Complete auto-generated reference for every technology module.
API module¶
API module: HTTP testing steps.
- steplib.modules.api.register(registry)[source]¶
Register all API steps into the given registry.
- Return type:
API step definitions for behave.
These steps cover the MVP: configuration, requests, and assertions.
All steps delegate to pure action functions in actions.py.
- steplib.modules.api.steps.register(registry)[source]¶
Register all API steps into the given registry.
- Return type:
- steplib.modules.api.steps.step_clear_request_data(context)[source]¶
Clear request-specific data.
- Return type:
- steplib.modules.api.steps.step_json_path_contains(context, path, value)[source]¶
Assert JSON path contains value.
- Return type:
- steplib.modules.api.steps.step_json_path_equals(context, path, value)[source]¶
Assert JSON path equals value.
- Return type:
- steplib.modules.api.steps.step_json_path_exists(context, path)[source]¶
Assert JSON path exists.
- Return type:
- steplib.modules.api.steps.step_json_path_has_length(context, path, length)[source]¶
Assert JSON path value has a specific length.
- Return type:
- steplib.modules.api.steps.step_json_path_is_not_null(context, path)[source]¶
Assert JSON path value is not null.
- Return type:
- steplib.modules.api.steps.step_json_path_is_null(context, path)[source]¶
Assert JSON path value is null.
- Return type:
- steplib.modules.api.steps.step_json_path_matches_regex(context, path, pattern)[source]¶
Assert JSON path value matches regex pattern.
- Return type:
- steplib.modules.api.steps.step_json_path_not_equals(context, path, value)[source]¶
Assert JSON path does not equal value.
- Return type:
- steplib.modules.api.steps.step_json_path_type(context, path, type)[source]¶
Assert JSON path value type.
- Return type:
- steplib.modules.api.steps.step_remove_header(context, name)[source]¶
Remove a default header.
- Return type:
- steplib.modules.api.steps.step_remove_query_param(context, name)[source]¶
Remove a query parameter.
- Return type:
- steplib.modules.api.steps.step_response_body_contains(context, text)[source]¶
Assert response body contains text.
- Return type:
- steplib.modules.api.steps.step_response_body_not_contains(context, text)[source]¶
Assert response body does not contain text.
- Return type:
- steplib.modules.api.steps.step_response_body_valid_json(context)[source]¶
Assert response body is valid JSON.
- Return type:
- steplib.modules.api.steps.step_response_content_type(context, content_type)[source]¶
Assert content type equals.
- Return type:
- steplib.modules.api.steps.step_response_content_type_contains(context, content_type)[source]¶
Assert content type contains.
- Return type:
- steplib.modules.api.steps.step_response_header_contains(context, name, value)[source]¶
Assert response header contains substring.
- Return type:
- steplib.modules.api.steps.step_response_header_equals(context, name, value)[source]¶
Assert response header equals value.
- Return type:
- steplib.modules.api.steps.step_response_header_exists(context, name)[source]¶
Assert response header exists.
- Return type:
- steplib.modules.api.steps.step_response_header_not_equals(context, name, value)[source]¶
Assert response header does not equal value.
- Return type:
- steplib.modules.api.steps.step_response_header_not_exists(context, name)[source]¶
Assert response header does not exist.
- Return type:
- steplib.modules.api.steps.step_response_matches_schema(context)[source]¶
Validate response against a JSON Schema from step text.
- Return type:
- steplib.modules.api.steps.step_response_matches_table(context)[source]¶
Compare the response JSON with a behave table.
Uses
behave-tablesto convertcontext.tableto dicts andbehave-kitfor soft assertions. Both require their respective extras.- Return type:
- steplib.modules.api.steps.step_response_status(context, status)[source]¶
Assert response status code.
- Return type:
- steplib.modules.api.steps.step_response_status_in(context, statuses)[source]¶
Assert response status is in a list.
- Return type:
- steplib.modules.api.steps.step_response_time_between(context, min, max)[source]¶
Assert response time is between min and max seconds.
- Return type:
- steplib.modules.api.steps.step_response_time_greater_than(context, seconds)[source]¶
Assert response time is greater than N seconds.
- Return type:
- steplib.modules.api.steps.step_response_time_less_than(context, seconds)[source]¶
Assert response time is less than N seconds.
- Return type:
- steplib.modules.api.steps.step_save_cookies(context)[source]¶
Save cookies from the response.
- Return type:
- steplib.modules.api.steps.step_send_form_data(context, method, url)[source]¶
Send an HTTP request with form data from a behave table.
- Return type:
- steplib.modules.api.steps.step_send_json_body(context, method, url)[source]¶
Send an HTTP request with a JSON body from step text.
- Return type:
- steplib.modules.api.steps.step_send_request(context, method, url)[source]¶
Send an HTTP request.
- Return type:
- steplib.modules.api.steps.step_send_request_with_body(context, method, url)[source]¶
Send an HTTP request with a body from the step’s text.
- Return type:
- steplib.modules.api.steps.step_send_with_headers(context, method, url)[source]¶
Send an HTTP request with extra headers from a behave table.
- Return type:
- steplib.modules.api.steps.step_send_with_params(context, method, url)[source]¶
Send an HTTP request with query params from a behave table.
- Return type:
- steplib.modules.api.steps.step_set_base_url(context, url)[source]¶
Set the API base URL.
- Return type:
- steplib.modules.api.steps.step_set_basic_auth(context, user, password)[source]¶
Set basic auth credentials.
- Return type:
- steplib.modules.api.steps.step_set_bearer_token(context, token)[source]¶
Set a bearer token.
- Return type:
- steplib.modules.api.steps.step_set_header(context, name, value)[source]¶
Set a default API header.
- Return type:
- steplib.modules.api.steps.step_set_query_param(context, name, value)[source]¶
Set a query parameter.
- Return type:
- steplib.modules.api.steps.step_set_timeout(context, seconds)[source]¶
Set the API timeout.
- Return type:
- steplib.modules.api.steps.step_store_header(context, name, variable)[source]¶
Store a response header as a variable.
- Return type:
- steplib.modules.api.steps.step_store_json_path(context, path, variable)[source]¶
Store a JSON path value as a variable.
- Return type:
- steplib.modules.api.steps.step_store_response_body(context, variable)[source]¶
Store the response body as a variable.
- Return type:
- steplib.modules.api.steps.step_store_response_time(context, variable)[source]¶
Store response time as a variable.
- Return type:
- steplib.modules.api.steps.step_store_status(context, variable)[source]¶
Store the response status as a variable.
- Return type:
- steplib.modules.api.steps.step_use_variable_as_header(context, variable, name)[source]¶
Set a header from a stored variable.
- Return type:
- steplib.modules.api.steps.step_use_variable_as_param(context, variable, name)[source]¶
Set a query param from a stored variable.
- Return type:
- steplib.modules.api.steps.step_variable_equals(context, variable, value)[source]¶
Assert stored variable equals value.
- Return type:
Pure action functions for the API module.
These functions contain the actual logic and are called by step definitions.
They operate on ApiContext and are fully testable without behave.
- steplib.modules.api.actions.api_assert_body_contains(api_ctx, text)[source]¶
Assert that the last response body contains text.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.text (
str) – The substring to search for.
- Raises:
AssertionError – If no response exists or the body does not contain text.
- Return type:
- steplib.modules.api.actions.api_assert_body_not_contains(api_ctx, text)[source]¶
Assert that the last response body does NOT contain text.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.text (
str) – The substring that should not be present.
- Raises:
AssertionError – If no response exists or the body contains text.
- Return type:
- steplib.modules.api.actions.api_assert_content_type(api_ctx, expected)[source]¶
Assert that the Content-Type response header equals expected.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.expected (
str) – The expected content type value.
- Raises:
AssertionError – If no response exists or the content type does not match.
- Return type:
- steplib.modules.api.actions.api_assert_content_type_contains(api_ctx, substring)[source]¶
Assert that the Content-Type response header contains substring.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.substring (
str) – The substring to search for in the Content-Type header.
- Raises:
AssertionError – If no response exists or the content type does not contain the substring.
- Return type:
- steplib.modules.api.actions.api_assert_header_contains(api_ctx, name, substring)[source]¶
Assert that a response header contains substring.
- Parameters:
- Raises:
AssertionError – If no response exists, the header is missing, or the value does not contain the substring.
- Return type:
- steplib.modules.api.actions.api_assert_header_equals(api_ctx, name, expected)[source]¶
Assert that a response header equals expected.
- Parameters:
- Raises:
AssertionError – If no response exists, the header is missing, or the value does not match.
- Return type:
- steplib.modules.api.actions.api_assert_header_exists(api_ctx, name)[source]¶
Assert that a response header exists.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.name (
str) – The header name.
- Raises:
AssertionError – If no response exists or the header is missing.
- Return type:
- steplib.modules.api.actions.api_assert_header_not_equals(api_ctx, name, value)[source]¶
Assert that a response header does NOT equal value.
- Parameters:
- Raises:
AssertionError – If no response exists, the header is missing, or the value matches.
- Return type:
- steplib.modules.api.actions.api_assert_header_not_exists(api_ctx, name)[source]¶
Assert that a response header does NOT exist.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.name (
str) – The header name.
- Raises:
AssertionError – If no response exists or the header is present.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_contains(api_ctx, path, value)[source]¶
Assert that a JSON path value contains value (for lists or strings).
- Parameters:
- Raises:
AssertionError – If no response exists or the value is not contained.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_equals(api_ctx, path, expected)[source]¶
Assert that a JSON path in the last response equals expected.
- Parameters:
- Raises:
AssertionError – If no response exists or the value does not match.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_exists(api_ctx, path)[source]¶
Assert that a JSON path exists in the last response.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.path (
str) – A JSONPath expression starting with$.
- Raises:
AssertionError – If no response exists or the path does not exist.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_has_length(api_ctx, path, expected)[source]¶
Assert that a JSON path value has a specific length.
- Parameters:
- Raises:
AssertionError – If no response exists or the length does not match.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_is_not_null(api_ctx, path)[source]¶
Assert that a JSON path value is not null.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.path (
str) – A JSONPath expression starting with$.
- Raises:
AssertionError – If no response exists or the value is null.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_is_null(api_ctx, path)[source]¶
Assert that a JSON path value is null.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.path (
str) – A JSONPath expression starting with$.
- Raises:
AssertionError – If no response exists or the value is not null.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_matches_regex(api_ctx, path, pattern)[source]¶
Assert that a JSON path string value matches a regex pattern.
- Parameters:
- Raises:
AssertionError – If no response exists or the value does not match.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_not_equals(api_ctx, path, value)[source]¶
Assert that a JSON path value does NOT equal value.
- Parameters:
- Raises:
AssertionError – If no response exists or the value matches.
- Return type:
- steplib.modules.api.actions.api_assert_json_path_type(api_ctx, path, expected_type)[source]¶
Assert that the value at a JSON path is of a specific type.
- Parameters:
- Raises:
AssertionError – If no response exists or the type does not match.
ValueError – If expected_type is not a recognized type name.
- Return type:
- steplib.modules.api.actions.api_assert_json_schema(api_ctx, schema)[source]¶
Validate the last response body against a JSON Schema (draft-07 subset).
Supports: type, properties, required, items, enum, minimum, maximum, minLength, maxLength, pattern.
- Parameters:
- Raises:
AssertionError – If no response exists or validation fails.
- Return type:
- steplib.modules.api.actions.api_assert_json_valid(api_ctx)[source]¶
Assert that the last response body is valid JSON.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.- Raises:
AssertionError – If no response exists or the body is not valid JSON.
- Return type:
- steplib.modules.api.actions.api_assert_response_time_between(api_ctx, min_s, max_s)[source]¶
Assert that the last response time is between min_s and max_s seconds.
- Parameters:
- Raises:
AssertionError – If no response exists or the response time is out of range.
- Return type:
- steplib.modules.api.actions.api_assert_response_time_greater_than(api_ctx, seconds)[source]¶
Assert that the last response took more than seconds seconds.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.seconds (
float) – The minimum acceptable response time in seconds.
- Raises:
AssertionError – If no response exists or the response was too fast.
- Return type:
- steplib.modules.api.actions.api_assert_response_time_less_than(api_ctx, seconds)[source]¶
Assert that the last response took less than seconds seconds.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.seconds (
float) – The maximum acceptable response time in seconds.
- Raises:
AssertionError – If no response exists or the response was too slow.
- Return type:
- steplib.modules.api.actions.api_assert_status(api_ctx, expected)[source]¶
Assert that the last response status matches expected.
- Raises:
AssertionError – If the status does not match or no response exists.
- Return type:
- steplib.modules.api.actions.api_assert_status_in(api_ctx, expected)[source]¶
Assert that the last response status is one of the expected values.
- Parameters:
- Raises:
AssertionError – If no response exists or the status is not in the list.
- Return type:
- steplib.modules.api.actions.api_assert_variable_equals(api_ctx, variable, expected)[source]¶
Assert that a stored variable equals expected (compared as string).
- Parameters:
- Raises:
AssertionError – If the variable does not exist or the value does not match.
- Return type:
- steplib.modules.api.actions.api_clear_request_data(api_ctx)[source]¶
Reset request-specific data: headers, params, auth, cookies, body.
Keeps base_url, timeout, ssl_verify, allow_redirects, and proxies.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.- Return type:
- steplib.modules.api.actions.api_remove_header(api_ctx, name)[source]¶
Remove a default header from the context.
Header lookup is case-insensitive per RFC 7230.
- steplib.modules.api.actions.api_remove_query_param(api_ctx, name)[source]¶
Remove a query parameter from the default params.
- steplib.modules.api.actions.api_save_cookies(api_ctx)[source]¶
Extract cookies from the last response and store them in the context.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.- Raises:
AssertionError – If no response exists.
- Return type:
- steplib.modules.api.actions.api_send(api_ctx, method, url, body=None, *, params=None, extra_headers=None)[source]¶
Send an HTTP request and store the response in
api_ctx.- Parameters:
api_ctx (
ApiContext) – The API context to operate on.method (
str) – HTTP method (e.g."GET","POST").url (
str) – URL (relative URLs are resolved againstbase_url).body (
str|bytes|None) – Optional request body as string or bytes.params (
dict[str,str] |None) – Optional per-request query params (overrides context defaults).extra_headers (
dict[str,str] |None) – Optional per-request headers merged with defaults.
- Return type:
Response- Returns:
The
Responseobject.- Raises:
RuntimeError – If no HTTP client is configured.
- steplib.modules.api.actions.api_send_form(api_ctx, method, url, data)[source]¶
Send an HTTP request with form-encoded data.
Sets
Content-Type: application/x-www-form-urlencodedand encodes data as the request body.
- steplib.modules.api.actions.api_send_json(api_ctx, method, url, data)[source]¶
Send an HTTP request with a JSON body.
Sets
Content-Type: application/json. If data is a dict, it is serialized to JSON. If it is a string, it is sent as-is.
- steplib.modules.api.actions.api_set_allow_redirects(api_ctx, allow)[source]¶
Enable or disable following redirects.
- steplib.modules.api.actions.api_set_base_url(api_ctx, url)[source]¶
Set the base URL for subsequent requests.
- Return type:
- steplib.modules.api.actions.api_set_basic_auth(api_ctx, username, password)[source]¶
Set basic authentication credentials for subsequent requests.
- steplib.modules.api.actions.api_set_bearer_token(api_ctx, token)[source]¶
Set a Bearer token in the Authorization header.
- steplib.modules.api.actions.api_set_header(api_ctx, name, value)[source]¶
Set a default header that will be sent with every request.
- Return type:
- steplib.modules.api.actions.api_set_proxy(api_ctx, url)[source]¶
Set a proxy URL for both HTTP and HTTPS requests.
- steplib.modules.api.actions.api_set_query_param(api_ctx, name, value)[source]¶
Set a default query parameter sent with every request.
- steplib.modules.api.actions.api_set_ssl_verify(api_ctx, verify)[source]¶
Enable or disable SSL certificate verification.
- steplib.modules.api.actions.api_set_timeout(api_ctx, seconds)[source]¶
Set the request timeout in seconds.
- Return type:
- steplib.modules.api.actions.api_store(api_ctx, variable, value)[source]¶
Store a value under variable name in the API context.
- steplib.modules.api.actions.api_store_header(api_ctx, name, variable)[source]¶
Store a response header value as variable.
- Parameters:
- Raises:
AssertionError – If no response exists or the header is missing.
- Return type:
- steplib.modules.api.actions.api_store_json_path(api_ctx, path, variable)[source]¶
Store the value at a JSON path from the last response as variable.
- Parameters:
- Raises:
AssertionError – If no response exists.
- Return type:
- steplib.modules.api.actions.api_store_response_body(api_ctx, variable)[source]¶
Store the last response body as variable.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.variable (
str) – The variable name to store the body under.
- Raises:
AssertionError – If no response exists.
- Return type:
- steplib.modules.api.actions.api_store_response_time(api_ctx, variable)[source]¶
Store the last response time in milliseconds as variable.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.variable (
str) – The variable name to store the elapsed time under.
- Raises:
AssertionError – If no response exists.
- Return type:
- steplib.modules.api.actions.api_store_status(api_ctx, variable)[source]¶
Store the last response status code as variable.
- Parameters:
api_ctx (
ApiContext) – The API context to operate on.variable (
str) – The variable name to store the status code under.
- Raises:
AssertionError – If no response exists.
- Return type:
- steplib.modules.api.actions.api_use_variable_as_header(api_ctx, name, variable)[source]¶
Set a header from a stored variable.
- steplib.modules.api.actions.api_use_variable_as_query_param(api_ctx, name, variable)[source]¶
Set a query parameter from a stored variable.
ApiContext: per-scenario HTTP state for the API module.
- class steplib.modules.api.context.ApiContext(client=None, base_url='', default_headers=<factory>, query_params=<factory>, auth=None, cookies=<factory>, timeout=None, allow_redirects=True, ssl_verify=True, proxies=<factory>, last_request=None, last_response=None, variables=<factory>, backend='stdlib')[source]
Bases:
objectHolds all HTTP state for a scenario.
Lives at
context.steplib.apiand is reset between scenarios.- client
The HTTP client implementation (defaults to
UrllibHTTPClient).
- base_url
The base URL for resolving relative URLs.
- default_headers
Headers sent with every request.
- query_params
Default query parameters sent with every request.
- auth
Optional
(username, password)tuple for basic auth.
- cookies
Cookies sent with every request.
- timeout
Request timeout in seconds (
None= no timeout).
- allow_redirects
Whether to follow redirects (default
True).
- ssl_verify
Whether to verify SSL certificates (default
True).
- proxies
Proxy mappings (e.g.
{"http": "http://proxy:8080"}).
- last_request
The most recent
Requestsent.
- last_response
The most recent
Responsereceived.
- variables
User-defined variables stored by steps.
- backend
The backend name (e.g.
"stdlib","httpx").
-
allow_redirects:
bool= True
-
backend:
str= 'stdlib'
-
base_url:
str= ''
-
client:
HTTPClient|None= None
-
last_request:
Request|None= None
-
last_response:
Response|None= None
-
ssl_verify:
bool= True
HTTP client abstraction: protocol, stdlib fallback, and lazy httpx/requests clients.
- class steplib.modules.api.client.HTTPClient(*args, **kwargs)[source]
Bases:
ProtocolProtocol for HTTP client implementations.
- request(method, url, *, headers=None, body=None, timeout=None, params=None, auth=None, cookies=None, allow_redirects=True, verify=True, proxies=None)[source]
Send an HTTP request and return the response.
- Parameters:
- Return type:
Response- Returns:
The
Responseobject.
- class steplib.modules.api.client.HttpxHTTPClient[source]
Bases:
objectHTTP client backed by httpx (requires the
[api]extra).- request(method, url, *, headers=None, body=None, timeout=None, params=None, auth=None, cookies=None, allow_redirects=True, verify=True, proxies=None)[source]
Send an HTTP request using httpx.
- Parameters:
- Return type:
Response- Returns:
The
Responseobject.
- class steplib.modules.api.client.NoRedirectHandler[source]
Bases:
HTTPRedirectHandlerRedirect handler that raises HTTPError instead of following redirects.
- class steplib.modules.api.client.Request(method, url, headers=<factory>, body=None)[source]
Bases:
objectImmutable HTTP request representation.
- method
The HTTP method (e.g.
"GET","POST").
- url
The resolved URL.
- headers
Request headers.
- body
Optional request body as bytes.
-
method:
str
-
url:
str
- class steplib.modules.api.client.RequestsHTTPClient[source]
Bases:
objectHTTP client backed by requests (requires the
requestspackage).- request(method, url, *, headers=None, body=None, timeout=None, params=None, auth=None, cookies=None, allow_redirects=True, verify=True, proxies=None)[source]
Send an HTTP request using requests.
- Parameters:
- Return type:
Response- Returns:
The
Responseobject.
- class steplib.modules.api.client.Response(status, headers=<factory>, body=b'', elapsed_ms=0.0)[source]
Bases:
objectImmutable HTTP response representation.
- status
The HTTP status code.
- headers
Response headers.
- body
The raw response body as bytes.
- elapsed_ms
The request duration in milliseconds.
-
body:
bytes
-
elapsed_ms:
float
- json()[source]
Parse the body as JSON.
- Return type:
- Returns:
The parsed JSON data.
- Raises:
json.JSONDecodeError – If the body is not valid JSON.
-
status:
int
- property text: str
Decode the body as UTF-8 text.
- class steplib.modules.api.client.UrllibHTTPClient[source]
Bases:
objectHTTP client using only the standard library (urllib).
- request(method, url, *, headers=None, body=None, timeout=None, params=None, auth=None, cookies=None, allow_redirects=True, verify=True, proxies=None)[source]
Send an HTTP request using urllib.
- Parameters:
method (
str) – The HTTP method (e.g."GET").url (
str) – The target URL.params (
dict[str,str] |None) – Optional query parameters appended to the URL.auth (
tuple[str,str] |None) – Optional(username, password)for basic auth.cookies (
dict[str,str] |None) – Optional cookies sent as a Cookie header.allow_redirects (
bool) – Whether to follow redirects (defaultTrue).verify (
bool) – Whether to verify SSL certificates (defaultTrue).
- Return type:
Response- Returns:
The
Responseobject.
- steplib.modules.api.client.get_client(backend='stdlib')[source]
Return an HTTP client for the given backend.
- Parameters:
backend (
str) –"stdlib","httpx", or"requests".- Return type:
HTTPClient- Returns:
An
HTTPClientinstance for the requested backend.- Raises:
MissingDependencyError – If the backend’s dependency is not installed.
Transformers for the API module: HttpMethod, Url, JsonPath.
- class steplib.modules.api.transforms.HttpMethod(method)[source]¶
Bases:
objectValidates and normalizes HTTP method strings.
- class steplib.modules.api.transforms.JsonPath(path)[source]¶
Bases:
objectSimple JSONPath evaluator supporting
$.path.to.valuesyntax.- Parameters:
path (
str) – A JSONPath expression starting with$(e.g."$.users[0].name").
- class steplib.modules.api.transforms.Url(url, base_url='')[source]¶
Bases:
objectRepresents a URL, resolving relative paths against a base URL.
- steplib.modules.api.transforms.parse_json(text)[source]¶
Parse a JSON string, raising ValueError on invalid input.
- Parameters:
text (
str) – A JSON string.- Return type:
- Returns:
The parsed JSON data.
- Raises:
json.JSONDecodeError – If the text is not valid JSON.
Web module¶
Web module: browser testing steps with Selenium.
- steplib.modules.web.register(registry)[source]¶
Register all web steps into the given registry.
- Return type:
Web step definitions for behave.
- steplib.modules.web.steps.register(registry)[source]¶
Register all web steps into the given registry.
- Return type:
- steplib.modules.web.steps.step_clear_input(context, by, value)[source]¶
Clear an input element.
- Return type:
- steplib.modules.web.steps.step_click_element(context, by, value)[source]¶
Click an element.
- Return type:
- steplib.modules.web.steps.step_cookie_exists(context, name)[source]¶
Assert cookie exists.
- Return type:
- steplib.modules.web.steps.step_element_attribute_equals(context, by, value, attr, expected)[source]¶
Assert element attribute equals.
- Return type:
- steplib.modules.web.steps.step_element_enabled(context, by, value)[source]¶
Assert element is enabled.
- Return type:
- steplib.modules.web.steps.step_element_not_present(context, by, value)[source]¶
Assert element is not present.
- Return type:
- steplib.modules.web.steps.step_element_present(context, by, value)[source]¶
Assert element is present.
- Return type:
- steplib.modules.web.steps.step_element_text_equals(context, by, value, expected)[source]¶
Assert element text equals.
- Return type:
- steplib.modules.web.steps.step_element_visible(context, by, value)[source]¶
Assert element is visible.
- Return type:
Navigate to a URL.
- Return type:
Navigate back.
- Return type:
Navigate forward.
- Return type:
- steplib.modules.web.steps.step_page_contains(context, text)[source]¶
Assert page contains text.
- Return type:
- steplib.modules.web.steps.step_page_not_contains(context, text)[source]¶
Assert page does not contain text.
- Return type:
- steplib.modules.web.steps.step_select_option(context, option, by, value)[source]¶
Select an option from a dropdown.
- Return type:
- steplib.modules.web.steps.step_set_implicit_wait(context, seconds)[source]¶
Set implicit wait.
- Return type:
- steplib.modules.web.steps.step_set_page_load_timeout(context, seconds)[source]¶
Set page load timeout.
- Return type:
- steplib.modules.web.steps.step_set_web_base_url(context, url)[source]¶
Set the web base URL.
- Return type:
- steplib.modules.web.steps.step_set_window_size(context, width, height)[source]¶
Set window size.
- Return type:
- steplib.modules.web.steps.step_store_cookie(context, name, variable)[source]¶
Store cookie as variable.
- Return type:
- steplib.modules.web.steps.step_store_current_url(context, variable)[source]¶
Store current URL as variable.
- Return type:
- steplib.modules.web.steps.step_store_element_attribute(context, attr, by, value, variable)[source]¶
Store element attribute as variable.
- Return type:
- steplib.modules.web.steps.step_store_element_text(context, by, value, variable)[source]¶
Store element text as variable.
- Return type:
- steplib.modules.web.steps.step_switch_to_default(context)[source]¶
Switch to default content.
- Return type:
- steplib.modules.web.steps.step_switch_to_frame(context, by, value)[source]¶
Switch to frame.
- Return type:
- steplib.modules.web.steps.step_take_screenshot(context, filename)[source]¶
Take a screenshot.
- Return type:
- steplib.modules.web.steps.step_type_text(context, text, by, value)[source]¶
Type text into an input.
- Return type:
- steplib.modules.web.steps.step_url_contains(context, fragment)[source]¶
Assert URL contains fragment.
- Return type:
- steplib.modules.web.steps.step_wait_for_element(context, by, value)[source]¶
Wait for element to be present.
- Return type:
- steplib.modules.web.steps.step_wait_for_element_visible(context, by, value)[source]¶
Wait for element to be visible.
- Return type:
- steplib.modules.web.steps.step_wait_for_text(context, text)[source]¶
Wait for text to appear on page.
- Return type:
Pure action functions for the Web module.
- steplib.modules.web.actions.web_assert_cookie_exists(web_ctx, name)[source]¶
Assert a cookie exists.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.name (
str) – The cookie name.
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the cookie does not exist.
- Return type:
- steplib.modules.web.actions.web_assert_element_attribute(web_ctx, by, value, attr, expected)[source]¶
Assert an element’s attribute equals expected.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the attribute does not match.
- Return type:
- steplib.modules.web.actions.web_assert_element_enabled(web_ctx, by, value)[source]¶
Assert an element is enabled.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the element is not enabled.
- Return type:
- steplib.modules.web.actions.web_assert_element_not_present(web_ctx, by, value)[source]¶
Assert an element is NOT present on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the element IS present.
- Return type:
- steplib.modules.web.actions.web_assert_element_present(web_ctx, by, value)[source]¶
Assert an element is present on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If no element matches the locator.
- Return type:
- steplib.modules.web.actions.web_assert_element_text_equals(web_ctx, by, value, expected)[source]¶
Assert an element’s text content equals expected.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the text does not match.
- Return type:
- steplib.modules.web.actions.web_assert_element_visible(web_ctx, by, value)[source]¶
Assert an element is visible on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the element is not visible.
- Return type:
- steplib.modules.web.actions.web_assert_page_contains(web_ctx, text)[source]¶
Assert the page source contains text.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.text (
str) – The substring to search for in the page source.
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the page does not contain text.
- Return type:
- steplib.modules.web.actions.web_assert_page_not_contains(web_ctx, text)[source]¶
Assert the page source does NOT contain text.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.text (
str) – The substring that should not be present.
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the page contains text.
- Return type:
- steplib.modules.web.actions.web_assert_title(web_ctx, expected)[source]¶
Assert the page title equals expected.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.expected (
str) – The expected page title.
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the title does not match.
- Return type:
- steplib.modules.web.actions.web_assert_url_contains(web_ctx, fragment)[source]¶
Assert the current URL contains fragment.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.fragment (
str) – The substring to search for in the current URL.
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the URL does not contain fragment.
- Return type:
- steplib.modules.web.actions.web_clear_input(web_ctx, by, value)[source]¶
Clear an input element.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_click(web_ctx, by, value)[source]¶
Click an element on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_delete_cookie(web_ctx, name)[source]¶
Delete a cookie by name.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.name (
str) – The cookie name.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
Navigate to url, resolving relative URLs against the base URL.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.url (
str) – The URL (absolute or relative).
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
Navigate back in browser history.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
Navigate forward in browser history.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_refresh_page(web_ctx)[source]¶
Refresh the current page.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_select_option(web_ctx, by, value, option)[source]¶
Select an option from a <select> element by visible text.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_set_base_url(web_ctx, url)[source]¶
Set the base URL for subsequent navigations.
- steplib.modules.web.actions.web_set_implicit_wait(web_ctx, seconds)[source]¶
Set the implicit wait time.
- steplib.modules.web.actions.web_set_page_load_timeout(web_ctx, seconds)[source]¶
Set the page load timeout.
- steplib.modules.web.actions.web_set_window_size(web_ctx, width, height)[source]¶
Set the browser window size.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_store(web_ctx, variable, value)[source]¶
Store a value under variable name in the Web context.
- steplib.modules.web.actions.web_store_cookie(web_ctx, name, variable)[source]¶
Store a cookie value as a variable.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the cookie does not exist.
- Return type:
- steplib.modules.web.actions.web_store_current_url(web_ctx, variable)[source]¶
Store the current URL as a variable.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.variable (
str) – The variable name to store under.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_store_element_attribute(web_ctx, by, value, attr, variable)[source]¶
Store an element’s attribute value as a variable.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_store_element_text(web_ctx, by, value, variable)[source]¶
Store an element’s text content as a variable.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_switch_to_default(web_ctx)[source]¶
Switch back to the default content from a frame.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_switch_to_frame(web_ctx, by, value)[source]¶
Switch to an iframe element.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_take_screenshot(web_ctx, filename)[source]¶
Take a screenshot and save it.
- Parameters:
web_ctx (
WebContext) – The web context to operate on.filename (
str) – The filename (or full path) for the screenshot.
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_type_text(web_ctx, by, value, text)[source]¶
Type text into an input element, clearing it first.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
- Return type:
- steplib.modules.web.actions.web_wait_for_element(web_ctx, by, value, timeout=None)[source]¶
Wait until an element is present on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the element is not found within the timeout.
- Return type:
- steplib.modules.web.actions.web_wait_for_element_visible(web_ctx, by, value, timeout=None)[source]¶
Wait until an element is visible on the page.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the element is not visible within the timeout.
- Return type:
- steplib.modules.web.actions.web_wait_for_text(web_ctx, text, timeout=None)[source]¶
Wait until the page source contains text.
- Parameters:
- Raises:
RuntimeError – If no browser driver is configured.
AssertionError – If the text does not appear within the timeout.
- Return type:
WebContext: per-scenario browser state for the Web module.
- class steplib.modules.web.context.WebContext(driver=None, base_url='', implicit_wait=10.0, page_load_timeout=30.0, window_size=None, screenshots_dir='', last_screenshot='', variables=<factory>, backend='selenium')[source]
Bases:
objectHolds all browser state for a scenario.
Lives at
context.steplib.weband is reset between scenarios.- driver
The browser driver instance (e.g.
SeleniumDriver).
- base_url
The base URL for resolving relative navigations.
- implicit_wait
Implicit wait time in seconds for element lookups.
- page_load_timeout
Page load timeout in seconds.
- window_size
Optional (width, height) tuple for the browser window.
- screenshots_dir
Directory path where screenshots are saved.
- last_screenshot
Path to the last screenshot taken.
- variables
User-defined variables stored by steps.
- backend
The backend name (e.g.
"selenium").
-
backend:
str= 'selenium'
-
base_url:
str= ''
-
driver:
Any= None
-
implicit_wait:
float= 10.0
-
last_screenshot:
str= ''
-
page_load_timeout:
float= 30.0
-
screenshots_dir:
str= ''
Browser client abstraction: protocol and lazy Selenium driver.
- class steplib.modules.web.client.BrowserDriver(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol for browser driver implementations.
- get_element_attribute(by, value, attr)[source]¶
Get an attribute value from an element.
- Return type:
- class steplib.modules.web.client.SeleniumDriver(browser='chrome', headless=True)[source]¶
Bases:
objectBrowser driver backed by Selenium (requires the
[web]extra).- get_element_attribute(by, value, attr)[source]¶
Get an attribute value from an element.
- Return type:
- steplib.modules.web.client.get_driver(backend='selenium', **kwargs)[source]¶
Return a browser driver for the given backend.
- Parameters:
- Return type:
- Returns:
A
BrowserDriverinstance.- Raises:
MissingDependencyError – If the backend’s dependency is not installed.
ValueError – If the backend is not supported.
DB module¶
DB module: database testing steps with SQLAlchemy.
- steplib.modules.db.register(registry)[source]¶
Register all DB steps into the given registry.
- Return type:
DB step definitions for behave.
- steplib.modules.db.steps.register(registry)[source]¶
Register all DB steps into the given registry.
- Return type:
- steplib.modules.db.steps.step_column_contains(context, column, value)[source]¶
Assert column value contains substring.
- Return type:
- steplib.modules.db.steps.step_column_equals(context, column, value)[source]¶
Assert column value in first row.
- Return type:
- steplib.modules.db.steps.step_column_is_not_null(context, column)[source]¶
Assert column value is not null.
- Return type:
- steplib.modules.db.steps.step_column_is_null(context, column)[source]¶
Assert column value is null.
- Return type:
- steplib.modules.db.steps.step_column_not_equals(context, column, value)[source]¶
Assert column value does not equal.
- Return type:
- steplib.modules.db.steps.step_db_disconnect(context)[source]¶
Disconnect from the database.
- Return type:
- steplib.modules.db.steps.step_execute_query(context, query)[source]¶
Execute a SQL query.
- Return type:
- steplib.modules.db.steps.step_execute_query_with_params(context, query, params)[source]¶
Execute a SQL query with bind parameters.
- Return type:
- steplib.modules.db.steps.step_query_row_count(context, count)[source]¶
Assert query row count.
- Return type:
- steplib.modules.db.steps.step_query_row_count_greater_than(context, count)[source]¶
Assert query row count is greater than.
- Return type:
- steplib.modules.db.steps.step_query_row_count_less_than(context, count)[source]¶
Assert query row count is less than.
- Return type:
- steplib.modules.db.steps.step_rollback_transaction(context)[source]¶
Rollback transaction.
- Return type:
- steplib.modules.db.steps.step_scalar_equals(context, query, value)[source]¶
Assert scalar query equals value.
- Return type:
- steplib.modules.db.steps.step_set_db_connection(context, connection_string)[source]¶
Set the database connection string.
- Return type:
- steplib.modules.db.steps.step_store_column_value(context, column, variable)[source]¶
Store column value as variable.
- Return type:
- steplib.modules.db.steps.step_store_row_count(context, variable)[source]¶
Store row count as variable.
- Return type:
- steplib.modules.db.steps.step_store_scalar(context, query, variable)[source]¶
Store scalar query result as variable.
- Return type:
- steplib.modules.db.steps.step_table_exists(context, table)[source]¶
Assert table exists.
- Return type:
- steplib.modules.db.steps.step_table_row_count(context, table, count)[source]¶
Assert table row count.
- Return type:
Pure action functions for the DB module.
- steplib.modules.db.actions.db_assert_column_contains(db_ctx, query, column, substring)[source]¶
Assert that a column in the first row contains substring.
- Parameters:
- Raises:
AssertionError – If the query returns no rows, the column is missing, or the value does not contain substring.
- Return type:
- steplib.modules.db.actions.db_assert_column_equals(db_ctx, query, column, expected)[source]¶
Assert that a column in the first row of a query equals expected.
- Parameters:
- Raises:
AssertionError – If the query returns no rows, the column is missing, or the value does not match.
- Return type:
- steplib.modules.db.actions.db_assert_column_is_not_null(db_ctx, query, column)[source]¶
Assert that a column in the first row is NOT NULL.
- Parameters:
- Raises:
AssertionError – If the query returns no rows, the column is missing, or the value is NULL.
- Return type:
- steplib.modules.db.actions.db_assert_column_is_null(db_ctx, query, column)[source]¶
Assert that a column in the first row is NULL.
- Parameters:
- Raises:
AssertionError – If the query returns no rows, the column is missing, or the value is not NULL.
- Return type:
- steplib.modules.db.actions.db_assert_column_not_equals(db_ctx, query, column, expected)[source]¶
Assert that a column in the first row does NOT equal expected.
- Parameters:
- Raises:
AssertionError – If the query returns no rows, the column is missing, or the value equals expected.
- Return type:
- steplib.modules.db.actions.db_assert_row_count(db_ctx, query, expected)[source]¶
Assert that a query returns exactly expected rows.
- Parameters:
- Raises:
AssertionError – If the row count does not match.
- Return type:
- steplib.modules.db.actions.db_assert_row_count_greater_than(db_ctx, query, minimum)[source]¶
Assert that a query returns more than minimum rows.
- Parameters:
- Raises:
AssertionError – If the row count is not greater than minimum.
- Return type:
- steplib.modules.db.actions.db_assert_row_count_less_than(db_ctx, query, maximum)[source]¶
Assert that a query returns fewer than maximum rows.
- Parameters:
- Raises:
AssertionError – If the row count is not less than maximum.
- Return type:
- steplib.modules.db.actions.db_assert_scalar_equals(db_ctx, query, expected, params=None)[source]¶
Assert that a scalar query result equals expected.
- steplib.modules.db.actions.db_assert_table_exists(db_ctx, table_name)[source]¶
Assert that a table exists in the database.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.table_name (
str) – The table name to check.
- Raises:
AssertionError – If the table does not exist.
ValueError – If the table name is not a valid identifier.
- Return type:
- steplib.modules.db.actions.db_assert_table_row_count(db_ctx, table_name, expected)[source]¶
Assert that a table has exactly expected rows.
- Parameters:
- Raises:
AssertionError – If the row count does not match.
ValueError – If the table name is not a valid identifier.
- Return type:
- steplib.modules.db.actions.db_begin_transaction(db_ctx)[source]¶
Begin a transaction on the current connection.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.- Raises:
RuntimeError – If no database connection is configured.
- Return type:
- steplib.modules.db.actions.db_commit(db_ctx)[source]¶
Commit the current transaction.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.- Raises:
RuntimeError – If no transaction is active.
- Return type:
- steplib.modules.db.actions.db_connect(db_ctx)[source]¶
Create a database connection from the stored connection string.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.- Raises:
RuntimeError – If no connection string is configured.
MissingDependencyError – If SQLAlchemy is not installed.
- Return type:
- steplib.modules.db.actions.db_disconnect(db_ctx)[source]¶
Close the database connection and dispose the engine.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.- Return type:
- steplib.modules.db.actions.db_query(db_ctx, query)[source]¶
Execute a SQL query and return rows as a list of dicts.
- steplib.modules.db.actions.db_query_scalar(db_ctx, query, params=None)[source]¶
Execute a SQL query and return a single scalar value.
- steplib.modules.db.actions.db_query_with_params(db_ctx, query, params)[source]¶
Execute a SQL query with bind parameters and return rows as a list of dicts.
- Parameters:
- Return type:
- Returns:
A list of dictionaries, one per row, keyed by column name.
- Raises:
RuntimeError – If no database connection is configured.
- steplib.modules.db.actions.db_rollback(db_ctx)[source]¶
Rollback the current transaction.
- Parameters:
db_ctx (
DbContext) – The DB context to operate on.- Raises:
RuntimeError – If no transaction is active.
- Return type:
- steplib.modules.db.actions.db_set_connection_string(db_ctx, connection_string)[source]¶
Set the database connection string.
- steplib.modules.db.actions.db_store(db_ctx, variable, value)[source]¶
Store a value under variable name in the DB context.
- steplib.modules.db.actions.db_store_column_value(db_ctx, query, column, variable)[source]¶
Store a column value from the first row of a query as a variable.
- Parameters:
- Raises:
AssertionError – If the query returns no rows or the column is missing.
- Return type:
- steplib.modules.db.actions.db_store_row_count(db_ctx, query, variable)[source]¶
Store the row count of a query as a variable.
- steplib.modules.db.actions.db_store_scalar(db_ctx, query, variable, params=None)[source]¶
Store a scalar query result as a variable.
DbContext: per-scenario database state for the DB module.
- class steplib.modules.db.context.DbContext(engine=None, connection=None, connection_string='', transaction=None, variables=<factory>, backend='sqlalchemy')[source]
Bases:
objectHolds all database state for a scenario.
Lives at
context.steplib.dband is reset between scenarios.- engine
The SQLAlchemy engine instance.
- connection
The active SQLAlchemy connection.
- connection_string
The SQLAlchemy connection string.
- transaction
The active transaction (if any).
- variables
User-defined variables stored by steps.
- backend
The backend name (e.g.
"sqlalchemy").
-
backend:
str= 'sqlalchemy'
-
connection:
Any= None
-
connection_string:
str= ''
-
engine:
Any= None
-
transaction:
Any= None
Database client abstraction with lazy SQLAlchemy import.
- class steplib.modules.db.client.DatabaseClient(connection_string)[source]
Bases:
objectDatabase client backed by SQLAlchemy (requires the
[db]extra).- engine
The SQLAlchemy engine instance.
- connection
The active SQLAlchemy connection.
- begin()[source]
Begin a transaction on the underlying connection.
- Return type:
- Returns:
The transaction object from SQLAlchemy.
- execute(query, params=None)[source]
Execute a SQL query and return rows as a list of dicts.
- steplib.modules.db.client.get_client(connection_string)[source]
Return a database client for the given connection string.
- Parameters:
connection_string (
str) – A SQLAlchemy-compatible connection string.- Return type:
DatabaseClient- Returns:
A
DatabaseClientinstance.- Raises:
MissingDependencyError – If SQLAlchemy is not installed.
Kafka module¶
Kafka module: Kafka producer/consumer testing steps.
- steplib.modules.kafka.register(registry)[source]¶
Register all Kafka steps into the given registry.
- Return type:
Kafka step definitions for behave.
- steplib.modules.kafka.steps.register(registry)[source]¶
Register all Kafka steps into the given registry.
- Return type:
- steplib.modules.kafka.steps.step_consume_messages(context, topic)[source]¶
Consume Kafka messages.
- Return type:
- steplib.modules.kafka.steps.step_consume_with_timeout(context, topic, timeout_ms)[source]¶
Consume Kafka messages with custom timeout.
- Return type:
- steplib.modules.kafka.steps.step_message_contains(context, text)[source]¶
Assert a message contains text.
- Return type:
- steplib.modules.kafka.steps.step_message_count(context, count)[source]¶
Assert message count.
- Return type:
- steplib.modules.kafka.steps.step_message_count_greater_than(context, count)[source]¶
Assert message count is greater than.
- Return type:
- steplib.modules.kafka.steps.step_message_key_equals(context, index, key)[source]¶
Assert message key equals.
- Return type:
- steplib.modules.kafka.steps.step_message_order(context, keys)[source]¶
Assert message order.
- Return type:
- steplib.modules.kafka.steps.step_message_value_equals(context, index, value)[source]¶
Assert message value equals.
- Return type:
- steplib.modules.kafka.steps.step_message_value_matches_regex(context, pattern)[source]¶
Assert message value matches regex.
- Return type:
- steplib.modules.kafka.steps.step_produce_json(context, topic, key, payload)[source]¶
Produce a JSON message.
- Return type:
- steplib.modules.kafka.steps.step_produce_message(context, topic, key, value)[source]¶
Produce a Kafka message.
- Return type:
- steplib.modules.kafka.steps.step_produce_multiple(context, count, topic)[source]¶
Produce multiple messages from a behave table.
- Return type:
- steplib.modules.kafka.steps.step_set_auto_offset_reset(context, strategy)[source]¶
Set auto offset reset.
- Return type:
- steplib.modules.kafka.steps.step_set_consumer_config(context, config)[source]¶
Set consumer config from JSON string.
- Return type:
- steplib.modules.kafka.steps.step_set_consumer_group(context, group)[source]¶
Set consumer group.
- Return type:
- steplib.modules.kafka.steps.step_set_kafka_servers(context, servers)[source]¶
Set Kafka bootstrap servers.
- Return type:
- steplib.modules.kafka.steps.step_set_producer_config(context, config)[source]¶
Set producer config from JSON string.
- Return type:
- steplib.modules.kafka.steps.step_store_message_count(context, variable)[source]¶
Store message count as variable.
- Return type:
- steplib.modules.kafka.steps.step_store_message_key(context, index, variable)[source]¶
Store message key as variable.
- Return type:
- steplib.modules.kafka.steps.step_store_message_value(context, index, variable)[source]¶
Store message value as variable.
- Return type:
Pure action functions for the Kafka module.
- steplib.modules.kafka.actions.kafka_assert_message_contains(messages, text)[source]¶
Assert that at least one message value contains text.
- steplib.modules.kafka.actions.kafka_assert_message_count(messages, expected)[source]¶
Assert that the number of messages equals expected.
- steplib.modules.kafka.actions.kafka_assert_message_count_greater_than(messages, minimum)[source]¶
Assert that the number of messages is greater than minimum.
- steplib.modules.kafka.actions.kafka_assert_message_key_equals(messages, index, expected)[source]¶
Assert that the key of the message at index equals expected.
- steplib.modules.kafka.actions.kafka_assert_message_order(messages, expected_keys)[source]¶
Assert that message keys appear in the order specified by expected_keys.
- steplib.modules.kafka.actions.kafka_assert_message_value_equals(messages, index, expected)[source]¶
Assert that the value of the message at index equals expected.
- steplib.modules.kafka.actions.kafka_assert_message_value_matches_regex(messages, pattern)[source]¶
Assert that at least one message value matches pattern (regex).
- steplib.modules.kafka.actions.kafka_consume(kafka_ctx, topic, timeout_ms=5000, max_records=100)[source]¶
Consume messages from a Kafka topic.
- Parameters:
- Return type:
- Returns:
A list of dicts with
key,value,topic,partition, andoffsetkeys.- Raises:
MissingDependencyError – If kafka-python-ng is not installed.
- steplib.modules.kafka.actions.kafka_produce(kafka_ctx, topic, key=None, value='')[source]¶
Produce a message to a Kafka topic.
- steplib.modules.kafka.actions.kafka_produce_json(kafka_ctx, topic, key=None, value=None)[source]¶
Produce a JSON-serialized message to a Kafka topic.
- Parameters:
- Raises:
MissingDependencyError – If kafka-python-ng is not installed.
- Return type:
- steplib.modules.kafka.actions.kafka_produce_multiple(kafka_ctx, topic, messages)[source]¶
Produce multiple messages to a Kafka topic.
- steplib.modules.kafka.actions.kafka_set_auto_offset_reset(kafka_ctx, strategy)[source]¶
Set the auto offset reset strategy.
- Parameters:
kafka_ctx (
KafkaContext) – The Kafka context to operate on.strategy (
str) –"earliest"or"latest".
- Raises:
ValueError – If the strategy is not valid.
- Return type:
- steplib.modules.kafka.actions.kafka_set_bootstrap_servers(kafka_ctx, servers)[source]¶
Set the Kafka bootstrap servers.
- steplib.modules.kafka.actions.kafka_set_consumer_config(kafka_ctx, config)[source]¶
Set additional consumer configuration overrides.
- steplib.modules.kafka.actions.kafka_set_consumer_group(kafka_ctx, group)[source]¶
Set the Kafka consumer group ID.
- steplib.modules.kafka.actions.kafka_set_producer_config(kafka_ctx, config)[source]¶
Set additional producer configuration overrides.
- steplib.modules.kafka.actions.kafka_store_message_count(messages, kafka_ctx, variable)[source]¶
Store the message count as a variable.
- steplib.modules.kafka.actions.kafka_store_message_key(messages, index, kafka_ctx, variable)[source]¶
Store the key of the message at index as a variable.
- Parameters:
- Raises:
AssertionError – If the index is out of range.
- Return type:
- steplib.modules.kafka.actions.kafka_store_message_value(messages, index, kafka_ctx, variable)[source]¶
Store the value of the message at index as a variable.
- Parameters:
- Raises:
AssertionError – If the index is out of range.
- Return type:
KafkaContext: per-scenario Kafka state for the Kafka module.
- class steplib.modules.kafka.context.KafkaContext(producer=None, consumer=None, bootstrap_servers='localhost:9092', consumer_group='steplib-group', auto_offset_reset='earliest', producer_config=<factory>, consumer_config=<factory>, variables=<factory>, backend='kafka-python-ng')[source]
Bases:
objectHolds all Kafka state for a scenario.
Lives at
context.steplib.kafkaand is reset between scenarios.- producer
The Kafka producer instance.
- consumer
The Kafka consumer instance.
- bootstrap_servers
Comma-separated Kafka bootstrap server addresses.
- consumer_group
The consumer group ID.
- auto_offset_reset
Offset reset strategy (
"earliest"or"latest").
- producer_config
Additional producer configuration overrides.
- consumer_config
Additional consumer configuration overrides.
- variables
User-defined variables stored by steps.
- backend
The backend name (e.g.
"kafka-python-ng").
-
auto_offset_reset:
str= 'earliest'
-
backend:
str= 'kafka-python-ng'
-
bootstrap_servers:
str= 'localhost:9092'
-
consumer:
Any= None
-
consumer_group:
str= 'steplib-group'
-
producer:
Any= None
- reset()[source]
Reset per-scenario state, keeping bootstrap servers and group config.
- Return type:
Data module¶
Data module: generic variable and environment variable testing steps.
- steplib.modules.data.register(registry)[source]¶
Register all data steps into the given registry.
- Return type:
Data step definitions for behave — generic variables and environment.
- steplib.modules.data.steps.register(registry)[source]¶
Register all data steps into the given registry.
- Return type:
- steplib.modules.data.steps.step_copy_variable(context, source, target)[source]¶
Copy a variable.
- Return type:
- steplib.modules.data.steps.step_delete_env_var(context, key)[source]¶
Delete an environment variable.
- Return type:
- steplib.modules.data.steps.step_delete_variable(context, name)[source]¶
Delete a variable.
- Return type:
- steplib.modules.data.steps.step_env_equals(context, key, value)[source]¶
Assert env var equals.
- Return type:
- steplib.modules.data.steps.step_env_exists(context, key)[source]¶
Assert env var exists.
- Return type:
- steplib.modules.data.steps.step_env_not_equals(context, key, value)[source]¶
Assert env var does not equal.
- Return type:
- steplib.modules.data.steps.step_env_not_exists(context, key)[source]¶
Assert env var does not exist.
- Return type:
- steplib.modules.data.steps.step_extract_key_path(context, key_path, name, target)[source]¶
Extract a value via dot-path navigation.
- Return type:
- steplib.modules.data.steps.step_increment_variable(context, name, amount)[source]¶
Increment a numeric variable.
- Return type:
- steplib.modules.data.steps.step_load_env_file(context, path)[source]¶
Load env vars from a .env file.
- Return type:
- steplib.modules.data.steps.step_load_json_file(context, path, name)[source]¶
Load JSON file into variable.
- Return type:
- steplib.modules.data.steps.step_load_yaml(context, path, name)[source]¶
Load YAML file into variable.
- Return type:
- steplib.modules.data.steps.step_set_env_from_variable(context, key, variable)[source]¶
Set env var from a data variable.
- Return type:
- steplib.modules.data.steps.step_set_env_var(context, key, value)[source]¶
Set an environment variable.
- Return type:
- steplib.modules.data.steps.step_set_variable(context, name, value)[source]¶
Set a generic variable.
- Return type:
- steplib.modules.data.steps.step_set_variable_json(context, name, json_str)[source]¶
Set a variable from a JSON string.
- Return type:
- steplib.modules.data.steps.step_store_env_var(context, key, variable)[source]¶
Store env var into a data variable.
- Return type:
- steplib.modules.data.steps.step_variable_contains(context, name, text)[source]¶
Assert variable contains substring.
- Return type:
- steplib.modules.data.steps.step_variable_ends_with(context, name, text)[source]¶
Assert variable ends with text.
- Return type:
- steplib.modules.data.steps.step_variable_equals(context, name, value)[source]¶
Assert variable equals.
- Return type:
- steplib.modules.data.steps.step_variable_exists(context, name)[source]¶
Assert variable exists.
- Return type:
- steplib.modules.data.steps.step_variable_greater_than(context, name, value)[source]¶
Assert variable is greater than value.
- Return type:
- steplib.modules.data.steps.step_variable_has_length(context, name, count)[source]¶
Assert variable has length.
- Return type:
- steplib.modules.data.steps.step_variable_is_empty(context, name)[source]¶
Assert variable is empty.
- Return type:
- steplib.modules.data.steps.step_variable_is_not_empty(context, name)[source]¶
Assert variable is not empty.
- Return type:
- steplib.modules.data.steps.step_variable_less_than(context, name, value)[source]¶
Assert variable is less than value.
- Return type:
- steplib.modules.data.steps.step_variable_matches(context, name, pattern)[source]¶
Assert variable matches regex pattern.
- Return type:
- steplib.modules.data.steps.step_variable_not_equals(context, name, value)[source]¶
Assert variable does not equal.
- Return type:
- steplib.modules.data.steps.step_variable_not_exists(context, name)[source]¶
Assert variable does not exist.
- Return type:
- steplib.modules.data.steps.step_variable_starts_with(context, name, text)[source]¶
Assert variable starts with text.
- Return type:
- steplib.modules.data.steps.step_wait(context, seconds)[source]¶
Wait for a number of seconds.
- Return type:
Pure action functions for the data module (variables + environment).
- steplib.modules.data.actions.data_assert_env_equals(key, expected)[source]¶
Assert that an environment variable equals an expected value.
- Parameters:
- Raises:
AssertionError – If the env var does not exist or differs.
- Return type:
- steplib.modules.data.actions.data_assert_env_exists(key)[source]¶
Assert that an environment variable exists.
- Parameters:
key (
str) – The environment variable name.- Raises:
AssertionError – If the env var does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_env_not_equals(key, expected)[source]¶
Assert that an environment variable does not equal a value.
- Parameters:
- Raises:
AssertionError – If the env var does not exist or equals the value.
- Return type:
- steplib.modules.data.actions.data_assert_env_not_exists(key)[source]¶
Assert that an environment variable does not exist.
- Parameters:
key (
str) – The environment variable name.- Raises:
AssertionError – If the env var exists.
- Return type:
- steplib.modules.data.actions.data_assert_variable_contains(data_ctx, name, substring)[source]¶
Assert that a variable’s string value contains a substring.
- Parameters:
- Raises:
AssertionError – If the variable does not exist or doesn’t contain the substring.
- Return type:
- steplib.modules.data.actions.data_assert_variable_ends_with(data_ctx, name, text)[source]¶
Assert that a variable’s string value ends with the given text.
- Parameters:
- Raises:
AssertionError – If the variable does not end with the text or does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_variable_equals(data_ctx, name, expected)[source]¶
Assert that a variable equals an expected value.
- Parameters:
- Raises:
AssertionError – If the variable does not exist or the value differs.
- Return type:
- steplib.modules.data.actions.data_assert_variable_exists(data_ctx, name)[source]¶
Assert that a variable exists in the data context.
- Parameters:
data_ctx (
DataContext) – The data context to check.name (
str) – The variable name.
- Raises:
AssertionError – If the variable does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_variable_greater_than(data_ctx, name, value)[source]¶
Assert that a variable’s numeric value is greater than a threshold.
- Parameters:
- Raises:
AssertionError – If the variable is not greater than the value or does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_variable_has_length(data_ctx, name, expected)[source]¶
Assert that a variable has a specific length.
Works with strings, lists, dicts, and any object with
__len__.- Parameters:
- Raises:
AssertionError – If the variable does not exist or has a different length.
- Return type:
- steplib.modules.data.actions.data_assert_variable_is_empty(data_ctx, name)[source]¶
Assert that a variable is empty (empty string, empty list, empty dict, or None).
- Parameters:
data_ctx (
DataContext) – The data context to check.name (
str) – The variable name.
- Raises:
AssertionError – If the variable does not exist or is not empty.
- Return type:
- steplib.modules.data.actions.data_assert_variable_is_not_empty(data_ctx, name)[source]¶
Assert that a variable is not empty.
- Parameters:
data_ctx (
DataContext) – The data context to check.name (
str) – The variable name.
- Raises:
AssertionError – If the variable does not exist or is empty.
- Return type:
- steplib.modules.data.actions.data_assert_variable_less_than(data_ctx, name, value)[source]¶
Assert that a variable’s numeric value is less than a threshold.
- Parameters:
- Raises:
AssertionError – If the variable is not less than the value or does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_variable_matches(data_ctx, name, pattern)[source]¶
Assert that a variable’s string value matches a regex pattern.
- Parameters:
- Raises:
AssertionError – If the variable does not match or does not exist.
- Return type:
- steplib.modules.data.actions.data_assert_variable_not_equals(data_ctx, name, expected)[source]¶
Assert that a variable does not equal a value.
- Parameters:
- Raises:
AssertionError – If the variable does not exist or equals the value.
- Return type:
- steplib.modules.data.actions.data_assert_variable_not_exists(data_ctx, name)[source]¶
Assert that a variable does not exist in the data context.
- Parameters:
data_ctx (
DataContext) – The data context to check.name (
str) – The variable name.
- Raises:
AssertionError – If the variable exists.
- Return type:
- steplib.modules.data.actions.data_assert_variable_starts_with(data_ctx, name, text)[source]¶
Assert that a variable’s string value starts with the given text.
- Parameters:
- Raises:
AssertionError – If the variable does not start with the text or does not exist.
- Return type:
- steplib.modules.data.actions.data_clear_variables(data_ctx)[source]¶
Clear all variables from the data context.
- Return type:
- steplib.modules.data.actions.data_copy_variable(data_ctx, source, target)[source]¶
Copy a variable to a new name.
- steplib.modules.data.actions.data_delete_env_var(data_ctx, key)[source]¶
Delete an environment variable, backing up the original for restoration.
- steplib.modules.data.actions.data_delete_variable(data_ctx, name)[source]¶
Delete a variable from the data context.
- steplib.modules.data.actions.data_extract_key_path(data_ctx, source, key_path, target)[source]¶
Extract a value from a variable using dot-path navigation.
Navigates nested dicts/lists using dot-separated keys. List indices are supported via integer keys (e.g.
items.0.name).- Parameters:
- Raises:
- Return type:
- steplib.modules.data.actions.data_increment_variable(data_ctx, name, amount=1)[source]¶
Increment a numeric variable by a given amount.
- Parameters:
- Raises:
KeyError – If the variable does not exist.
ValueError – If the variable is not numeric.
- Return type:
- steplib.modules.data.actions.data_load_env_file(data_ctx, path)[source]¶
Load environment variables from a .env-style file.
Parses simple
KEY=VALUElines. Lines starting with#are ignored. Quoted values (single or double) are unquoted.- Parameters:
data_ctx (
DataContext) – The data context (used for backup tracking).path (
str) – Path to the .env file.
- Raises:
FileNotFoundError – If the file does not exist.
- Return type:
- steplib.modules.data.actions.data_load_json_file(data_ctx, path, name)[source]¶
Load a JSON file into a variable as a dict.
- Parameters:
- Raises:
FileNotFoundError – If the file does not exist.
- Return type:
- steplib.modules.data.actions.data_load_yaml_file(data_ctx, path, name)[source]¶
Load a YAML file into a variable as a dict.
- Parameters:
- Raises:
MissingDependencyError – If PyYAML is not installed.
FileNotFoundError – If the file does not exist.
- Return type:
- steplib.modules.data.actions.data_set_env_from_variable(data_ctx, variable, key)[source]¶
Set an environment variable from a data variable’s value.
- steplib.modules.data.actions.data_set_env_var(data_ctx, key, value)[source]¶
Set an environment variable, backing up the original for restoration.
- steplib.modules.data.actions.data_set_variable(data_ctx, name, value)[source]¶
Set a generic variable in the data context.
- steplib.modules.data.actions.data_set_variable_json(data_ctx, name, json_str)[source]¶
Set a variable to a parsed JSON value.
- Parameters:
- Raises:
json.JSONDecodeError – If the string is not valid JSON.
- Return type:
- steplib.modules.data.actions.data_store_env_var(data_ctx, key, variable)[source]¶
Store an environment variable’s value into a data variable.
- Parameters:
- Raises:
AssertionError – If the env var does not exist.
- Return type:
DataContext: per-scenario state for the data module (variables + env).
- class steplib.modules.data.context.DataContext(variables=<factory>, _env_backup=<factory>)[source]
Bases:
objectHolds all data-module state for a scenario.
Lives at
context.steplib.dataand is reset between scenarios.- variables
User-defined variables stored by steps.
- _env_backup
Snapshot of environment variables modified during the scenario, so they can be restored on reset/cleanup.