0
0
Testing Fundamentalstesting~15 mins

Continuous Delivery testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify automated deployment and testing in Continuous Delivery pipeline
Preconditions (4)
Step 1: Commit and push a code change to the main branch
Step 2: Wait for the CI pipeline to trigger automatically
Step 3: Verify that the build completes successfully
Step 4: Verify that automated tests run and pass in the CI pipeline
Step 5: Verify that the CD pipeline deploys the new build to the staging environment automatically
Step 6: Access the staging environment URL
Step 7: Verify that the deployed application is running and the new changes are visible
Step 8: Run a smoke test on the staging environment to confirm basic functionality
✅ Expected Result: The code change triggers the CI pipeline which builds and tests the code successfully. The CD pipeline automatically deploys the build to staging. The staging environment shows the updated application running correctly and passes the smoke test.
Automation Requirements - Python with Selenium and requests
Assertions Needed:
Build success status is confirmed from CI API
Test results indicate all tests passed
Deployment success status is confirmed from CD API
Staging environment URL is reachable
Application page contains expected updated content
Smoke test actions complete without errors
Best Practices:
Use API calls to verify CI/CD pipeline statuses instead of UI only
Use explicit waits for environment readiness
Separate test logic for build verification, deployment verification, and UI smoke test
Use Page Object Model for UI interactions
Handle network errors gracefully
Automated Solution
Testing Fundamentals
import requests
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 StagingPage:
    def __init__(self, driver):
        self.driver = driver
    def open(self, url):
        self.driver.get(url)
    def is_updated_content_visible(self, expected_text):
        try:
            element = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, f"//*[contains(text(), '{expected_text}')]"))
            )
            return True
        except:
            return False

def verify_ci_build(ci_api_url, build_id, headers):
    response = requests.get(f"{ci_api_url}/builds/{build_id}", headers=headers)
    response.raise_for_status()
    data = response.json()
    return data.get('status') == 'success'

def verify_ci_tests(ci_api_url, build_id, headers):
    response = requests.get(f"{ci_api_url}/builds/{build_id}/tests", headers=headers)
    response.raise_for_status()
    data = response.json()
    return all(test['status'] == 'passed' for test in data.get('tests', []))

def verify_cd_deployment(cd_api_url, deployment_id, headers):
    response = requests.get(f"{cd_api_url}/deployments/{deployment_id}", headers=headers)
    response.raise_for_status()
    data = response.json()
    return data.get('status') == 'deployed'

def smoke_test_staging(staging_url):
    driver = webdriver.Chrome()
    page = StagingPage(driver)
    try:
        page.open(staging_url)
        if not page.is_updated_content_visible('New Feature'):
            return False
        # Additional smoke test steps can be added here
        return True
    finally:
        driver.quit()

# Example usage
ci_api_url = 'https://ci.example.com/api'
cd_api_url = 'https://cd.example.com/api'
headers = {'Authorization': 'Bearer your_token_here'}
build_id = '12345'
deployment_id = '67890'
staging_url = 'https://staging.example.com'

assert verify_ci_build(ci_api_url, build_id, headers), 'CI build failed'
assert verify_ci_tests(ci_api_url, build_id, headers), 'CI tests failed'
assert verify_cd_deployment(cd_api_url, deployment_id, headers), 'CD deployment failed'
assert smoke_test_staging(staging_url), 'Smoke test failed on staging environment'

This script automates the verification of a Continuous Delivery pipeline.

First, it uses requests to call the CI API and check if the build succeeded and all tests passed. Then it calls the CD API to confirm the deployment status.

For the staging environment, it uses Selenium WebDriver to open the application URL and check if the updated content is visible, simulating a smoke test.

The code uses explicit waits to ensure elements are loaded before checking. It separates concerns by having functions for each verification step and a Page Object for UI interaction.

Assertions ensure the test fails clearly if any step does not meet expectations.

Common Mistakes - 4 Pitfalls
Checking deployment success only by UI without verifying CI/CD pipeline status via API
Using fixed sleep delays instead of explicit waits for page elements
Combining all test steps in one large function without separation
Not handling network errors or API failures gracefully
Bonus Challenge

Now add data-driven testing with 3 different build and deployment IDs to verify multiple pipeline runs

Show Hint