0
0
Selenium Pythontesting~15 mins

Action methods in page class in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Login to the web application using action methods in page class
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Enter 'testuser' in the username input field
Step 3: Enter 'Test@1234' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
✅ Expected Result: User is successfully logged in and the dashboard page URL contains '/dashboard'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the login button is clickable before clicking
Verify username and password fields are present before entering text
Best Practices:
Use a Page Object Model with action methods encapsulating interactions
Use explicit waits to wait for elements to be interactable
Use Selenium's By locators with descriptive names
Keep test logic separate from page interaction logic
Automated Solution
Selenium Python
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

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: str):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.username_input)
        ).clear()
        self.driver.find_element(*self.username_input).send_keys(username)

    def enter_password(self, password: str):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.password_input)
        ).clear()
        self.driver.find_element(*self.password_input).send_keys(password)

    def click_login(self):
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.login_button)
        ).click()


def test_login():
    driver = webdriver.Chrome()
    driver.get('https://example.com/login')

    login_page = LoginPage(driver)
    login_page.enter_username('testuser')
    login_page.enter_password('Test@1234')
    login_page.click_login()

    WebDriverWait(driver, 10).until(
        EC.url_contains('/dashboard')
    )

    assert '/dashboard' in driver.current_url, 'Dashboard URL not found after login'

    driver.quit()

This script uses Selenium with Python to automate the login process.

The LoginPage class is a page object that holds locators and action methods for the login page. Each method waits explicitly for the element to be ready before interacting with it. This avoids timing issues.

The test_login function opens the browser, navigates to the login page, and uses the page object's methods to enter username and password, then click login.

After clicking login, it waits until the URL contains '/dashboard' to confirm successful login, then asserts this condition.

Finally, the browser is closed with driver.quit().

This structure keeps test steps clear and separates page details from test logic, following best practices.

Common Mistakes - 3 Pitfalls
Using hardcoded sleeps like time.sleep() instead of explicit waits
Locating elements inside test functions instead of page class
Not verifying elements are present or clickable before interacting
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations

Show Hint