Python API¶
behave-trace can be used as a library for loading, inspecting, and serializing trace data.
Public API¶
Attachment helpers¶
behave_trace.attach_screenshot
¶
Attach a screenshot to the current step.
source can be: bytes, path string, Selenium WebDriver, Playwright Page.
Source code in behave_trace/attach.py
behave_trace.attach_dom
¶
Attach a DOM snapshot to the current step.
source can be: HTML string, Selenium WebDriver, Playwright Page.
When source is a WebDriver or Page, the current URL is extracted and
injected as a <base> tag so relative URLs (CSS, JS, images) resolve
correctly in the viewer's iframe.
Source code in behave_trace/attach.py
behave_trace.attach_text
¶
Attach a plain text snippet to the current step.
Source code in behave_trace/attach.py
behave_trace.attach_network
¶
Attach an HTTP request/response as a network artifact to the current step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Any
|
The Behave context object. |
required |
request_data
|
Any
|
Can be:
- A dict with keys: |
required |
name
|
str
|
Artifact name (default: "network"). |
'network'
|
Source code in behave_trace/attach.py
behave_trace.log
¶
Append a log line to the current step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Any
|
The Behave context object. |
required |
message
|
str
|
The log message text. |
required |
level
|
str
|
Log level — "info", "warning", or "error" (default: "info"). |
'info'
|
Source code in behave_trace/attach.py
Trace model¶
behave_trace.models.Trace
dataclass
¶
Root of the trace tree — serialized to .trace file.
Source code in behave_trace/models.py
to_dict
¶
Serialize including computed properties for the viewer frontend.
Source code in behave_trace/models.py
behave_trace.models.Feature
dataclass
¶
A Gherkin feature.
Source code in behave_trace/models.py
to_dict
¶
Serialize including computed properties for the viewer frontend.
Source code in behave_trace/models.py
behave_trace.models.Scenario
dataclass
¶
A scenario or scenario outline example.
Source code in behave_trace/models.py
to_dict
¶
Serialize including computed properties for the viewer frontend.
Source code in behave_trace/models.py
behave_trace.models.Step
dataclass
¶
A single Gherkin step with its execution trace.
Source code in behave_trace/models.py
to_dict
¶
Serialize including computed properties for the viewer frontend.
Source code in behave_trace/models.py
behave_trace.models.Artifact
dataclass
¶
An artifact captured during a step execution.
Source code in behave_trace/models.py
to_dict
¶
Serialize including computed properties for the viewer frontend.
Source code in behave_trace/models.py
Serializer¶
behave_trace.serializer.Serializer
¶
Serialize and deserialize Trace objects.
Source code in behave_trace/serializer.py
load
staticmethod
¶
Load a trace from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Input file path. |
required |
Returns:
| Type | Description |
|---|---|
Trace
|
Reconstructed Trace object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
JSONDecodeError
|
If the file is not valid JSON. |
ValueError
|
If the JSON root is not a JSON object. |
Source code in behave_trace/serializer.py
save
staticmethod
¶
Save a trace to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
Trace
|
The Trace object to save. |
required |
path
|
str | Path
|
Output file path. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The path where the trace was written. |
Source code in behave_trace/serializer.py
Runner¶
behave_trace.runner.BehaveRunner
¶
Execute behave as a subprocess and load the resulting trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
behave_executable
|
str | Path | None
|
Path to the behave executable.
Defaults to |
None
|
Source code in behave_trace/runner.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
build_command
¶
build_command(features_dir: str | Path = '.', output_path: str | Path = 'trace.json', tags: str | None = None, extra_args: list[str] | None = None) -> list[str]
Build the behave command line without executing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_dir
|
str | Path
|
Directory containing .feature files. |
'.'
|
output_path
|
str | Path
|
Where the trace JSON will be written. |
'trace.json'
|
tags
|
str | None
|
Optional tag expression (e.g. |
None
|
extra_args
|
list[str] | None
|
Additional arguments to pass to behave. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The command list suitable for :func: |
Source code in behave_trace/runner.py
run
¶
run(features_dir: str | Path = '.', output_path: str | Path = 'trace.json', tags: str | None = None, extra_args: list[str] | None = None, cwd: str | Path | None = None, server_url: str | None = None) -> RunResult
Execute behave and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_dir
|
str | Path
|
Directory containing .feature files. |
'.'
|
output_path
|
str | Path
|
Where the trace JSON will be written. |
'trace.json'
|
tags
|
str | None
|
Optional tag expression. |
None
|
extra_args
|
list[str] | None
|
Additional arguments for behave. |
None
|
cwd
|
str | Path | None
|
Working directory for the subprocess. |
None
|
Returns:
| Type | Description |
|---|---|
RunResult
|
class: |
Source code in behave_trace/runner.py
run_and_load
¶
run_and_load(features_dir: str | Path = '.', output_path: str | Path = 'trace.json', tags: str | None = None, extra_args: list[str] | None = None, cwd: str | Path | None = None) -> tuple[RunResult, Trace | None]
Execute behave and load the resulting trace.
Returns a tuple of (RunResult, Trace | None). If behave fails to produce a trace file, the second element is None.
Source code in behave_trace/runner.py
run_filtered
¶
run_filtered(features_dir: str | Path = '.', output_path: str | Path = 'trace.json', tags: str | None = None, scenario_names: list[str] | None = None, cwd: str | Path | None = None, server_url: str | None = None) -> RunResult
Execute behave filtered by scenario names.
Uses --name flags to select specific scenarios. If
scenario_names is None or empty, behaves like :meth:run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features_dir
|
str | Path
|
Directory containing .feature files. |
'.'
|
output_path
|
str | Path
|
Where the trace JSON will be written. |
'trace.json'
|
tags
|
str | None
|
Optional tag expression. |
None
|
scenario_names
|
list[str] | None
|
List of scenario names to run. |
None
|
cwd
|
str | Path | None
|
Working directory for the subprocess. |
None
|
Returns:
| Type | Description |
|---|---|
RunResult
|
class: |
Source code in behave_trace/runner.py
behave_trace.runner.RunResult
dataclass
¶
Viewer¶
behave_trace.viewer.server.ViewerServer
¶
Serve the trace viewer SPA and trace data on localhost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
Trace | None
|
The :class: |
None
|
port
|
int
|
Port to bind (0 = auto-select a free port). |
0
|
watching
|
bool
|
Whether the server is in watch mode (enables SSE and run-status endpoints). |
False
|
rerun_callback
|
Callable[[list[str] | None], None] | None
|
Optional callback invoked when the client POSTs
to |
None
|
Example
server = ViewerServer(trace, port=8080) server.start() 'http://127.0.0.1:8080' server.stop()
Source code in behave_trace/viewer/server.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
get_auto_run
¶
notify
¶
set_auto_run
¶
set_running
¶
start
¶
Start the server and return the URL.
Source code in behave_trace/viewer/server.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
stop
¶
update_trace
¶
Update the trace served by this server and notify SSE clients.
behave_trace.viewer.browser.open_app
¶
Open the given URL in an app-like browser window.
Tries chrome --app first, falls back to webbrowser.open().