pythonseleniumautomated-teststest-reporting

Test Result Status at Parameter level with Selenium Python


So I have a selenium/Python script that reads a JSON file for a list of URL and then one by one browses them; and takes screenshot.

I want to generate a result report (HTML) at the end of test BUT with following;

The report should not just say that script passed/failed but it also shares the result against each parameter URL. For example if 2 out of 5 websites were down at the point of test then it is reflected in the HTML results. Like facebook.com - OK while www.sdadas.com - Failed

Here is my code:

with open('Path to JSON file', encoding='utf-8') as s:
   data = json.loads(s.read())

driver = Edge()

driver.set_page_load_timeout(10)
driver.implicitly_wait(5)

for site in data['sites']:
    driver.get(data['sites'][site])
    driver.get_screenshot_as_file("Screenshot path\\Image" + site + '.png')
driver.close()

My JSON file

{
    "sites": {
        "facebook": "http://www.facebook.com",
            "Wrong": "http://www.gonssgle.com"
    }
}

Solution

  • There is a couple way to do check if a site is down/unavailable. You can use selenium to check if an error message showing the site down exists (this can be varied, depends on which browser you use), but I don't suggest you do this.

    I would just simply use python requests library (python3) or any similar library, to go to each site and check its response code, example:

    response = requests.get(data)
    assert response.status_code == requests.codes.ok
    

    then proceed with your getting screenshot code

    also, to generate HTML report, I'd suggest you run your code with python unit test framework, like unittest, pytest, etc. Each of it should have html report plugin and you can customize it if necessary. It will generate test report based on your test results.