Skip to content
Go back

Test Automation - How To Attach Session Storage, Local Storage, Cookies, and Console logs To Allure Report in Selenium Python

Published:

Introduction

In this article, I will review the ability to add session storage, local storage, cookies, and console logs to our test automation report.

The technological stack used to implement the solution is:

Programing language: Python

Testing Framework: pytest

Reporting Framework: Allure

link to the project that was developed together with Elias Shourosh.

We understand how critical it is to ensure that our applications run smoothly and efficiently as Test Automation engineers. This can be achieved by logging key information that is helpful for troubleshooting and debugging. This information includes console logs, session storage, local storage, and cookies.

Console Logs refer to messages that are logged in the browser’s developer console. Messages such as these are typically used to log information about the current state of the application, to report errors and warnings, and to log debugging information. If you press F12 in a modern browser and select the Console tab, you can access the console logs.

Session Storage and Local Storage are web storage mechanisms that allow web applications to store data locally on the user’s device. Session storage is used to store data that is specific to a single user session, while local storage is used to store data that should persist even after the user closes the browser. The difference between the two is that session storage data is deleted when the user closes the browser, while local storage data persists.

Cookies, on the other hand, are small text files that are stored on the user’s device by a website. They are used to remember user preferences, login credentials, and other information that is specific to the user. Cookies are stored on the user’s device and are sent back to the server with every request, allowing the server to recognize the user and their preferences.

Implementing the Solution

import json
import allure

allure.attach(
    body=json.dumps(driver.get_cookies(), indent=4),
    name="Cookies",
    attachment_type=allure.attachment_type.JSON
)

allure.attach(
    body=json.dumps(
        {item[0]: item[1] for item in driver.execute_script("return Object.entries(sessionStorage);")},
        indent=4
    ),
    name="Session Storage",
    attachment_type=allure.attachment_type.JSON
)

allure.attach(
    body=json.dumps(
        {item[0]: item[1] for item in driver.execute_script("return Object.entries(localStorage);")},
        indent=4
    ),
    name="Local Storage",
    attachment_type=allure.attachment_type.JSON
)

allure.attach(
    body=json.dumps(driver.get_log("browser"), indent=4),
    name="Console Logs",
    attachment_type=allure.attachment_type.JSON
)

We use Allure library to attach various types of data to the test report. This library provides an easy-to-use API for attaching data, including text, JSON, and images, to the report.

Using the get_cookies() method of Selenium to get the cookies associated with the current browser session and attach them to the report formatted as JSON.

The cookies are now included in the report attachments:

No alt text provided for this image

The following lines of code use the execute_script() method of Selenium to retrieve the contents of the session storage and local storage, respectively. These values are then converted to a dictionary, formatted as JSON, and attached to the report.

The session and local storage are now included in the report attachments:

No alt text provided for this image

The last line of code uses the get_log() method of Selenium to retrieve the console logs and attach them to the report formatted as JSON.

The console logs are now included in the report attachments:

No alt text provided for this image

In conclusion

The code which is found here demonstrates how to add console logs, session storage, local storage, and cookies to Allure reports using Python, Selenium, and Allure. By using these libraries and this code, developers, and testers can ensure that they have all the information they need to thoroughly test their applications and resolve any issues that arise during the testing process.

Happy testing!


Suggest Changes

Have a challenge? Let's Talk


Previous Post
Test Automation - How To Add Git Version Control Data To Allure Report in Python
Next Post
Test Automation - How To Capture Full-Page Screenshots In Selenium 4 Python Using Chrome DevTools Protocol