HTML report generation in Selenium Python - Build an Automation Script
import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import HtmlTestRunner class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = (By.ID, 'username') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def enter_username(self, username): self.driver.find_element(*self.username_input).clear() self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): self.driver.find_element(*self.password_input).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): self.driver.find_element(*self.login_button).click() class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('https://example.com/login') self.login_page = LoginPage(self.driver) def test_login_success(self): self.login_page.enter_username('testuser') self.login_page.enter_password('Test@1234') self.login_page.click_login() wait = WebDriverWait(self.driver, 10) wait.until(EC.url_to_be('https://example.com/dashboard')) current_url = self.driver.current_url self.assertEqual(current_url, 'https://example.com/dashboard', 'URL after login should be dashboard') title = self.driver.title self.assertEqual(title, 'Dashboard - Example', 'Page title should be Dashboard - Example') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))
The code uses unittest framework with Selenium WebDriver to automate the login test.
The LoginPage class follows the Page Object Model pattern to keep locators and actions organized.
In setUp, the browser opens and navigates to the login page.
The test enters username and password, clicks login, then waits explicitly for the URL to change to the dashboard URL.
Assertions check the current URL and page title to confirm successful login.
The tearDown method closes the browser after the test.
The test runner HtmlTestRunner generates an HTML report saved in the reports folder showing test steps and pass/fail results.
Now add data-driven testing with 3 different username and password combinations to verify login success or failure.
Practice
Solution
Step 1: Understand the role of HTML reports
HTML reports present test results in a clear, visual format that anyone can read.Step 2: Compare with other options
Speeding tests, writing tests faster, or avoiding assertions are unrelated to HTML reports.Final Answer:
To make test results easy to read and share -> Option CQuick Check:
HTML reports = clear, shareable results [OK]
- Thinking HTML reports speed up tests
- Confusing report generation with test writing
- Believing reports remove need for assertions
Solution
Step 1: Identify the package for unittest HTML reports
HtmlTestRunner is designed to work with unittest for HTML report generation.Step 2: Eliminate other options
pytest-html is for pytest, selenium-html and unittest-html are not standard packages.Final Answer:
HtmlTestRunner -> Option DQuick Check:
unittest + HtmlTestRunner = HTML reports [OK]
- Choosing pytest-html for unittest tests
- Assuming selenium-html is a real package
- Confusing unittest-html as official
import unittest
import HtmlTestRunner
class TestExample(unittest.TestCase):
def test_pass(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))Solution
Step 1: Check the output parameter in HTMLTestRunner
The output='reports' argument tells HtmlTestRunner to save reports in the 'reports' folder.Step 2: Understand default behavior
Without output specified, reports save in current directory, but here 'reports' is explicit.Final Answer:
reports folder -> Option BQuick Check:
output='reports' means save in reports folder [OK]
- Assuming reports save in current directory by default
- Thinking HtmlTestRunner creates its own folder
- Believing reports print only to console
import unittest
import HtmlTestRunner
class TestExample(unittest.TestCase):
def test_fail(self):
self.assertEqual(1, 2)
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=''))Solution
Step 1: Check the output parameter usage
Setting output='' means no folder is specified, which can cause HtmlTestRunner to fail saving reports properly.Step 2: Verify other parts
assertEqual syntax is correct, HtmlTestRunner is imported, and test class inheritance is correct.Final Answer:
Empty output string causes report not to save -> Option AQuick Check:
output='' disables report folder, causing save issues [OK]
- Thinking assertEqual(1, 2) is syntax error
- Believing test class must inherit HtmlTestRunner
- Ignoring output parameter effect
Solution
Step 1: Identify correct parameter for output folder
HtmlTestRunner uses 'output' parameter to specify the folder for saving reports.Step 2: Check other parameter names
'report', 'folder', and 'path' are not valid parameters for HtmlTestRunner's HTMLTestRunner class.Final Answer:
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_reports')) -> Option AQuick Check:
Use output='foldername' to save reports correctly [OK]
- Using wrong parameter names like report or folder
- Not specifying output folder at all
- Confusing HtmlTestRunner options with other packages
Ready to prove it?
Pass the test and this pattern moves unknown
