{"id":10545,"date":"2019-01-28T04:34:19","date_gmt":"2019-01-28T09:34:19","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=10545"},"modified":"2019-01-28T06:26:56","modified_gmt":"2019-01-28T11:26:56","slug":"sending-images-and-log-messages-to-reportportal","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/sending-images-and-log-messages-to-reportportal\/","title":{"rendered":"Sending images and log messages to ReportPortal"},"content":{"rendered":"<p><strong>Problem:<\/strong> There is not much information on how to send or attach images to <a href=\"http:\/\/reportportal.io\/\">ReportPortal<\/a><\/p>\n<hr \/>\n<h3>Why this post?<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><strong>Note:<\/strong> This is in continuation to our previous post on <a href=\"https:\/\/qxf2.com\/blog\/reportportal-integration-with-pytest\/\">ReportPortal integration with pytestnd pytest markers<\/a><\/p>\n<hr \/>\n<h3>How to send logs and images to ReportPortal<\/h3>\n<p><strong>1. Sending log messages<\/strong><br \/>\nThe <a href=\"https:\/\/github.com\/reportportal\/agent-python-pytest\">agent-python-pytest<\/a> plugin provides a python logging handle which can be used in conftest.py file.<\/p>\n<pre lang=\"python\">@pytest.fixture(scope=\"session\")\r\ndef rp_logger(request):\r\n    import logging\r\n    # Import Report Portal logger and handler to the test module.\r\n    from pytest_reportportal import RPLogger, RPLogHandler\r\n    # Setting up a logging.\r\n    logging.setLoggerClass(RPLogger)\r\n    logger = logging.getLogger(__name__)\r\n    logger.setLevel(logging.DEBUG)\r\n    # Create handler for Report Portal.\r\n    rp_handler = RPLogHandler(request.node.config.py_test_service)\r\n    # Set INFO level for Report Portal handler.\r\n    rp_handler.setLevel(logging.INFO)\r\n    return logger\r\n<\/pre>\n<p>We can then use the <em>rp_logger<\/em> in tests to send log messages easily.<\/p>\n<pre lang=\"python\">def test_launch_success(rp_logger):\r\n    rp_logger.info(\"A GUI test. Launch Qxf2.com\")\r\n<\/pre>\n<p><strong>2. Sending screenshot<\/strong><br \/>\nWe can use the same method <em>rp_logger<\/em> 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 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Media_type\">mime<\/a> type.<\/p>\n<pre lang=\"python\">rp_logger.info(\r\n            image_name,\r\n            attachment={\r\n                \"data\": image,\r\n                \"mime\": \"application\/octet-stream\"\r\n            },\r\n        )\r\n<\/pre>\n<p>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.<\/p>\n<p>Below is the complete code for the test file.<\/p>\n<pre lang=\"python\"># Sample test to load Qxf2.com and send image to ReportPortal\r\n\r\nimport pytest\r\nfrom selenium import webdriver\r\n\r\n@pytest.mark.gui_test()\r\ndef test_launch_success(rp_logger):\r\n    \"Launch Qxf2 and save image\"\r\n    # Log message to ReportPortal\r\n    rp_logger.info(\"GUI Test. Launch Qxf2.com\")\r\n    driver = webdriver.Chrome()\r\n    driver.get(\"https:\/\/qxf2.com\")\r\n    image_name = \"Test_Launch_Success\"\r\n    driver.save_screenshot(image_name+'.png')\r\n    # Save screenshot to ReportPortal\r\n    save_screenshot_reportportal(rp_logger,image_name)\r\n\r\n\r\ndef save_screenshot_reportportal(rp_logger,image_name):\r\n    \"Method to save image to ReportPortal\"\r\n    try:\r\n        with open(image_name+'.png', \"rb\") as fh:\r\n            image = fh.read()\r\n        \r\n        rp_logger.info(\r\n            image_name,\r\n            attachment={\r\n                \"data\": image,\r\n                \"mime\": \"application\/octet-stream\"\r\n            },\r\n        )\r\n    except Exception as e:\r\n        print(\"Exception when trying to save screenshot to reportportal: %s\" %str(e))\r\n\r\n\r\n@pytest.mark.api_test()\r\ndef test_api():\r\n    print (\"API Test\")\r\n    pass\r\n\r\n<\/pre>\n<p><strong>3. Running the test<\/strong><br \/>\nWe can use the same conftest file mentioned in the <a href=\"https:\/\/qxf2.com\/blog\/reportportal-integration-with-pytest\/\">previous post<\/a> by adding the rp_logger method mentioned in step 1 and run the test<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/ReportPortal_Test_Run.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10552\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/ReportPortal_Test_Run.png\" alt=\"ReportPortal_Test_Run\" width=\"721\" height=\"162\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/ReportPortal_Test_Run.png 721w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/ReportPortal_Test_Run-300x67.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-10553\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image-1024x484.png\" alt=\"ReportPortal_Attachment\" width=\"1024\" height=\"484\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image-1024x484.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image-300x142.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image-768x363.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2019\/01\/Report_Portal_Image.png 1357w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>There we go, we now have our screenshots and log messages attached in ReportPortal. Enjoy testing and reporting&#8230;<\/p>\n<hr \/>\n<h3>References:<\/h3>\n<p>1. <a href=\"https:\/\/github.com\/reportportal\/client-Python\">ReportPortal python client<\/a><br \/>\n2. <a href=\"https:\/\/github.com\/reportportal\/agent-python-pytest\">agent-python-pytest<\/a><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107,18,181],"tags":[],"class_list":["post-10545","post","type-post","status-publish","format-standard","hentry","category-pytest","category-python","category-reportportal"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/10545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=10545"}],"version-history":[{"count":13,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/10545\/revisions"}],"predecessor-version":[{"id":10620,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/10545\/revisions\/10620"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=10545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=10545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=10545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}