0
0
Testing Fundamentalstesting~15 mins

Test environment setup in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify test environment setup for web application testing
Preconditions (3)
Step 1: Open the test automation tool
Step 2: Launch the web browser using the automation tool
Step 3: Navigate to the test application URL 'https://example.com/login'
Step 4: Verify the login page loads successfully by checking the page title is 'Login - ExampleApp'
Step 5: Close the browser
✅ Expected Result: The login page loads successfully with the correct title, confirming the test environment is set up correctly
Automation Requirements - Selenium WebDriver with Python
Assertions Needed:
Verify browser launches successfully
Verify page title matches 'Login - ExampleApp'
Verify browser closes without errors
Best Practices:
Use explicit waits to wait for page load
Use proper locators for elements
Handle browser setup and teardown in setup and teardown methods
Keep test code clean and readable
Automated Solution
Testing Fundamentals
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

class TestEnvironmentSetup(unittest.TestCase):
    def setUp(self):
        # Setup Chrome WebDriver
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

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

        # Wait until the title is as expected
        WebDriverWait(driver, 10).until(EC.title_is('Login - ExampleApp'))

        # Assert the page title
        self.assertEqual(driver.title, 'Login - ExampleApp')

    def tearDown(self):
        # Close the browser
        self.driver.quit()

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

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

setUp() method initializes the Chrome browser and maximizes the window to ensure consistent view.

The test test_login_page_loads navigates to the login page URL and waits explicitly for the page title to be 'Login - ExampleApp' to confirm the page loaded correctly.

It then asserts the page title to verify the correct page is displayed.

The tearDown() method closes the browser to clean up after the test.

Explicit waits are used to avoid timing issues, and the code is structured for clarity and maintainability.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It causes unnecessary delays or flaky tests if the page loads faster or slower than the sleep time.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected conditions to wait only as long as needed."}
Not closing the browser after test
Hardcoding element locators without validation
Bonus Challenge

Now add data-driven testing to verify the login page loads for three different URLs (e.g., staging, production, and test environment URLs).

Show Hint