Sending images and log messages to ReportPortal

Problem: There is not much information on how to send or attach images to ReportPortal


Why this post?

We started using ReportPortal for reporting automation test results. Along with the test results we also need to send log messages and images/screenshot to ReportPortal. These are useful artifacts which help people monitoring them check the success and failure reasons and take necessary actions.

We were not able to find information on how to send a screenshot to ReportPortal easily and had to spend some time to get it going. So this blog post is to share our knowledge on how to send the images to ReportPortal using pytest.

Note: This is in continuation to our previous post on ReportPortal integration with pytestnd pytest markers


How to send logs and images to ReportPortal

1. Sending log messages
The agent-python-pytest plugin provides a python logging handle which can be used in conftest.py file.

@pytest.fixture(scope="session")
def rp_logger(request):
    import logging
    # Import Report Portal logger and handler to the test module.
    from pytest_reportportal import RPLogger, RPLogHandler
    # Setting up a logging.
    logging.setLoggerClass(RPLogger)
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    # Create handler for Report Portal.
    rp_handler = RPLogHandler(request.node.config.py_test_service)
    # Set INFO level for Report Portal handler.
    rp_handler.setLevel(logging.INFO)
    return logger

We can then use the rp_logger in tests to send log messages easily.

def test_launch_success(rp_logger):
    rp_logger.info("A GUI test. Launch Qxf2.com")

2. Sending screenshot
We can use the same method rp_logger along with the attachment details to send image to ReportPortal. We need to pass the image_name which will be the name of the image in ReportPortal, data which is the image and the mime type.

rp_logger.info(
            image_name,
            attachment={
                "data": image,
                "mime": "application/octet-stream"
            },
        )

We have created a separate method save_screenshot_reportportal() which takes in an image and sends it to ReportPortal. We also need to open the .png(binary file) before sending the image file.

Below is the complete code for the test file.

# Sample test to load Qxf2.com and send image to ReportPortal
 
import pytest
from selenium import webdriver
 
@pytest.mark.gui_test()
def test_launch_success(rp_logger):
    "Launch Qxf2 and save image"
    # Log message to ReportPortal
    rp_logger.info("GUI Test. Launch Qxf2.com")
    driver = webdriver.Chrome()
    driver.get("https://qxf2.com")
    image_name = "Test_Launch_Success"
    driver.save_screenshot(image_name+'.png')
    # Save screenshot to ReportPortal
    save_screenshot_reportportal(rp_logger,image_name)
 
 
def save_screenshot_reportportal(rp_logger,image_name):
    "Method to save image to ReportPortal"
    try:
        with open(image_name+'.png', "rb") as fh:
            image = fh.read()
 
        rp_logger.info(
            image_name,
            attachment={
                "data": image,
                "mime": "application/octet-stream"
            },
        )
    except Exception as e:
        print("Exception when trying to save screenshot to reportportal: %s" %str(e))
 
 
@pytest.mark.api_test()
def test_api():
    print ("API Test")
    pass

3. Running the test
We can use the same conftest file mentioned in the previous post by adding the rp_logger method mentioned in step 1 and run the test

ReportPortal_Test_Run

ReportPortal_Attachment

There we go, we now have our screenshots and log messages attached in ReportPortal. Enjoy testing and reporting…


References:

1. ReportPortal python client
2. agent-python-pytest


11 thoughts on “Sending images and log messages to ReportPortal

  1. Hi,

    This is very useful post .I facing an error when try to use rp_logger in other class inside method directly and getting an errot that attribute is not defined

  2. Not sure if some can help same for JAVA+TEsngNG.I am getting hard time in sending screenshot to reportportal.

    1. Hi,

      I am not sure you have tried this link. If not you can refer this link. I hope it should be helpful for you.
      “https://github.com/reportportal/example-java-TestNG”

  3. Anybody got the issue resolved??

    Im getting this – com.epam.reportportal.message.ReportPortalMessage@1d0305fb

    Using version – implementation ‘com.epam.reportportal:logger-java-log4j:5.1.3’

    1. Hi,
      We have blogged our experience of using pytest plugins to send images and log messages to ReportPortal. However, We would want to try and help if you are using other frameworks/languages. Could you provide enough details about the issue you are facing?
      Regards,
      Preedhi

Leave a Reply

Your email address will not be published. Required fields are marked *