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


Avinash Shetty

I am a software tester with over 13 years of experience in software testing. Currently, I am working at Qxf2 Services Bangalore. As a student of the context-driven approach to software testing, I feel there is a lot to learn out there which keeps me very excited. My work has helped me gain good experience in different areas of testing like CRM, Web, Mobile and Database testing. I have good knowledge of building test scripts using Automation tools like Selenium and Appium using Java and Python. Beside testing, I am a “Sports Fanatic” and love watching and playing sports.

4 Comments

  1. Nagarjuna said:

    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

    April 22, 2019
    Reply
    • Mohan Kumar said:

      Nagarjuna, You can configure rp_logger as a global variable.

      April 25, 2019
      Reply
  2. Anonymous said:

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

    May 28, 2019
    Reply
    • Annapoorani Gurusamy Annapoorani Gurusamy said:

      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”

      May 28, 2019
      Reply

Leave a Reply

Your email address will not be published.