Skip to content
Advertisement

How to calculate time to load URl in selenium python automation

I have a selenium python automation test, which calculates Response time and adds it to JSON report. The problem is the code that I have calculates response time as before opening the URL and after it stops the test. I want to calculate the time it takes to load the URL page.

following is my code

test_screenshot.py

JavaScript

Conftest.py

JavaScript

I am getting confused how i should calculate load time in conftest.py.

Advertisement

Answer

What you need to do is pass the value of the response_time variable to pytest through a fixture so that it’s available to the pytest_json_modifyreport hook.

Since I wanted to add multiple timers to an individual test case, my solution to this was to create a new fixture that provides a factory, something like this (though I used a custom object in the factory, I’m guessing/hoping a dictionary like this will work):

JavaScript

your test class uses the fixture, and the test case gets an instance. something like:

JavaScript

now all of your response times are in a list of dictionaries in the json_report metadata’s environment attribute. from within the pytest_json_modifyreport hook:

JavaScript

and now you can do what you need to do with the information.

[note that I have pasted and manipulated the info here, so it may not work as written, but it should give you a direction to follow.]

Advertisement