0
0
Selenium Pythontesting~15 mins

HTML report generation in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality and generate HTML report
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Enter 'testuser' in the username input field with id 'username'
Step 3: Enter 'Test@1234' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait until the URL changes to 'https://example.com/dashboard'
Step 6: Verify that the page title is 'Dashboard - Example'
Step 7: Close the browser
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with correct title. An HTML report is generated showing the test steps and pass/fail status.
Automation Requirements - Selenium with Python unittest and HtmlTestRunner
Assertions Needed:
Assert current URL is 'https://example.com/dashboard'
Assert page title is 'Dashboard - Example'
Best Practices:
Use explicit waits to wait for URL change
Use Page Object Model to separate page locators and actions
Generate HTML report using HtmlTestRunner
Use setUp and tearDown methods for browser setup and cleanup
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding locators inside test methods
Not closing the browser after test
Not generating or checking HTML reports
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations to verify login success or failure.

Show Hint