0
0
Selenium Pythontesting~15 mins

Selenium vs Cypress vs Playwright comparison in Selenium Python - Automation Approaches Compared

Choose your learning style9 modes available
Compare login functionality automation using Selenium, Cypress, and Playwright
Preconditions (3)
Step 1: Open the login page
Step 2: Enter 'testuser' in the username field
Step 3: Enter 'Test@1234' in the password field
Step 4: Click the login button
Step 5: Verify that the URL changes to the dashboard page URL
Step 6: Verify that a welcome message with text 'Welcome, testuser!' is visible on the dashboard
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with the welcome message displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message text is 'Welcome, testuser!'
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use By.ID or By.NAME locators instead of brittle XPath
Structure code with setup and teardown methods
Use clear and descriptive assertion messages
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
import unittest

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 10)

    def test_login_success(self):
        driver = self.driver
        wait = self.wait
        driver.get('https://example.com/login')

        # Enter username
        username_field = wait.until(EC.visibility_of_element_located((By.ID, 'username')))
        username_field.clear()
        username_field.send_keys('testuser')

        # Enter password
        password_field = wait.until(EC.visibility_of_element_located((By.ID, 'password')))
        password_field.clear()
        password_field.send_keys('Test@1234')

        # Click login button
        login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn')))
        login_button.click()

        # Verify URL
        wait.until(EC.url_to_be('https://example.com/dashboard'))
        current_url = driver.current_url
        self.assertEqual(current_url, 'https://example.com/dashboard', 'URL after login should be dashboard URL')

        # Verify welcome message
        welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcomeMsg')))
        self.assertEqual(welcome_message.text, 'Welcome, testuser!', 'Welcome message text should match')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This test script uses Selenium WebDriver with Python's unittest framework.

setUp: Initializes the Chrome browser and sets an explicit wait of 10 seconds.

test_login_success: Automates the login steps precisely as per the manual test case:

  • Opens the login page URL.
  • Waits for username and password fields to be visible, then enters credentials.
  • Waits for the login button to be clickable and clicks it.
  • Waits until the URL changes to the dashboard URL and asserts it.
  • Waits for the welcome message element and asserts its text.

Explicit waits ensure the test waits only as long as needed for elements or URL changes, avoiding flaky tests.

tearDown: Closes the browser after the test finishes.

Locators use By.ID for reliability and readability. Assertions have clear messages to help identify failures.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Not closing the browser after test
Bonus Challenge

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

Show Hint