# behavex-images
This implementation extends the BehaveX library to enable attaching images to the generated HTML report
## Installation
pip install behavex-images
## Basic Usage
The following methods are provided to manage image attachments in the BehaveX HTML report:
1. `attach_image_binary(context, image_binary)`
- This function attaches an image from binary data to the test execution report. It takes two parameters:
- `context`: The context object which contains various attributes used in the function.
- `image_binary`: The binary data of the image to be attached.
- The function determines the binary format to ensure it maps to a JPG or PNG image. Otherwise, it raises an exception.
- Then, it converts the image to PNG format (if it is not already in that format) to add it to the gallery.
2. `attach_image_file(context, file_path)`
- This function attaches an image from a file to the test execution report. It takes two parameters:
- `context`: The context object which contains various attributes used in the function.
- `file_path`: The absolute path to the image file to be attached.
- The function first checks if the file exists at the given path. If it does not, it raises an exception.
- If the file exists, it checks the file extension. If the extension is not JPG or PNG, it raises an exception.
- Then, it converts the image to PNG format (if it is not already in that format) to add it to the gallery.
3. `set_attachments_condition(context, condition)`
- This function sets the condition to attach images to the HTML report. It takes two parameters:
- `context`: The context object which contains various attributes used in the function.
- `condition`: The condition to attach images to the HTML report. Possible values:
- AttachmentsCondition.ALWAYS: Attach images to the HTML report always.
- AttachmentsCondition.ON_FAILURE: Attach images to the HTML report only when the test fails. This is the default value.
- AttachmentsCondition.NEVER: Do not attach images to the HTML report, no matter the test result.
4. `clean_all_attached_images`
- This function removes all images already attached to the HTML report for the current test scenario.
## Examples
The provided methods can be used from the hooks available in the environment.py file, or directly from step definitions to attach images to the HTML report. For example:
* **Example 1**: Attaching an image file from a step definition
```python
...
from behavex_images import image_attachments
@given('I take a screenshot from current page')
def step_impl(context):
image_attachments.attach_image_file(context, 'path/to/image.png')
```
* **Example 2**: Attaching an image binary from the `after_step` hook in environment.py
```python
...
from behavex_images import image_attachments
from behavex_images.image_attachments import AttachmentsCondition
def before_all(context):
image_attachements.set_attachments_condition(context, AttachmentsCondition.ONLY_ON_FAILURE)
def after_step(context, step):
image_attachements.attach_image_binary(context, selenium_driver.get_screenshot_as_png())
```



Raw data
{
"_id": null,
"home_page": "https://github.com/abmercado19/behavex-images",
"name": "behavex-images",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Hernan Rey, Ana Mercado",
"author_email": "hernanrey@gmail.com, abmercado19@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/87/a9/36177ee715173edcbe58da40df821d45f5f4daec0793023e33f37b1891a9/behavex-images-3.0.6.tar.gz",
"platform": "any",
"description": "# behavex-images\nThis implementation extends the BehaveX library to enable attaching images to the generated HTML report\n\n## Installation\npip install behavex-images\n\n## Basic Usage\n\nThe following methods are provided to manage image attachments in the BehaveX HTML report:\n1. `attach_image_binary(context, image_binary)`\n - This function attaches an image from binary data to the test execution report. It takes two parameters:\n - `context`: The context object which contains various attributes used in the function.\n - `image_binary`: The binary data of the image to be attached.\n - The function determines the binary format to ensure it maps to a JPG or PNG image. Otherwise, it raises an exception.\n - Then, it converts the image to PNG format (if it is not already in that format) to add it to the gallery.\n\n2. `attach_image_file(context, file_path)`\n - This function attaches an image from a file to the test execution report. It takes two parameters:\n - `context`: The context object which contains various attributes used in the function.\n - `file_path`: The absolute path to the image file to be attached.\n - The function first checks if the file exists at the given path. If it does not, it raises an exception.\n - If the file exists, it checks the file extension. If the extension is not JPG or PNG, it raises an exception.\n - Then, it converts the image to PNG format (if it is not already in that format) to add it to the gallery.\n\n3. `set_attachments_condition(context, condition)`\n - This function sets the condition to attach images to the HTML report. It takes two parameters:\n - `context`: The context object which contains various attributes used in the function.\n - `condition`: The condition to attach images to the HTML report. Possible values:\n - AttachmentsCondition.ALWAYS: Attach images to the HTML report always.\n - AttachmentsCondition.ON_FAILURE: Attach images to the HTML report only when the test fails. This is the default value.\n - AttachmentsCondition.NEVER: Do not attach images to the HTML report, no matter the test result.\n\n4. `clean_all_attached_images`\n - This function removes all images already attached to the HTML report for the current test scenario.\n\n## Examples\nThe provided methods can be used from the hooks available in the environment.py file, or directly from step definitions to attach images to the HTML report. For example:\n\n* **Example 1**: Attaching an image file from a step definition\n```python\n...\nfrom behavex_images import image_attachments\n\n@given('I take a screenshot from current page')\ndef step_impl(context):\n image_attachments.attach_image_file(context, 'path/to/image.png')\n``` \n\n* **Example 2**: Attaching an image binary from the `after_step` hook in environment.py\n```python\n...\nfrom behavex_images import image_attachments\nfrom behavex_images.image_attachments import AttachmentsCondition\n\ndef before_all(context):\n image_attachements.set_attachments_condition(context, AttachmentsCondition.ONLY_ON_FAILURE)\n\ndef after_step(context, step):\n image_attachements.attach_image_binary(context, selenium_driver.get_screenshot_as_png())\n```\n\n\n\n\n\n\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "BehaveX extension library to attach images to the test execution report.",
"version": "3.0.6",
"project_urls": {
"Homepage": "https://github.com/abmercado19/behavex-images"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "87a936177ee715173edcbe58da40df821d45f5f4daec0793023e33f37b1891a9",
"md5": "30373d67e6df8edac5e94bee2458ded6",
"sha256": "3dae15e8cc4e87c598bf3a0e675f66ca3dfecfca0b7629ef597d7443393edefe"
},
"downloads": -1,
"filename": "behavex-images-3.0.6.tar.gz",
"has_sig": false,
"md5_digest": "30373d67e6df8edac5e94bee2458ded6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 2301914,
"upload_time": "2024-08-21T22:17:22",
"upload_time_iso_8601": "2024-08-21T22:17:22.392161Z",
"url": "https://files.pythonhosted.org/packages/87/a9/36177ee715173edcbe58da40df821d45f5f4daec0793023e33f37b1891a9/behavex-images-3.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-21 22:17:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "abmercado19",
"github_project": "behavex-images",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "behavex-images"
}