0
0
Testing Fundamentalstesting~10 mins

Network condition testing in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks how a web application behaves when the network is slow or disconnected. It verifies that the app shows a loading message during slow network and an error message when offline.

Test Code - Selenium
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 NetworkConditionTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_slow_network_loading(self):
        # Simulate slow network using Chrome DevTools Protocol
        self.driver.execute_cdp_cmd('Network.enable', {})
        self.driver.execute_cdp_cmd('Network.emulateNetworkConditions', {
            'offline': False,
            'latency': 2000,  # 2 seconds delay
            'downloadThroughput': 500 * 1024 / 8,  # 500 kbps
            'uploadThroughput': 500 * 1024 / 8
        })

        self.driver.refresh()

        # Wait for loading message
        loading = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'loading-message'))
        )
        self.assertEqual(loading.text, 'Loading...')

    def test_offline_error_message(self):
        # Simulate offline mode
        self.driver.execute_cdp_cmd('Network.enable', {})
        self.driver.execute_cdp_cmd('Network.emulateNetworkConditions', {
            'offline': True,
            'latency': 0,
            'downloadThroughput': 0,
            'uploadThroughput': 0
        })

        self.driver.refresh()

        # Wait for error message
        error = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'error-message'))
        )
        self.assertEqual(error.text, 'No internet connection')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened at https://example.com-PASS
2Enable network emulation and set slow network conditionsNetwork throttling set: 2s latency, 500 kbps throughput-PASS
3Refresh the page to apply network conditionsPage reloads with slow network-PASS
4Wait for loading message element with ID 'loading-message'Loading message visible on pageVerify loading message text is 'Loading...'PASS
5Enable network emulation and set offline modeNetwork set to offline-PASS
6Refresh the page to apply offline conditionPage reloads with no network-PASS
7Wait for error message element with ID 'error-message'Error message visible on pageVerify error message text is 'No internet connection'PASS
8Close browser and end testBrowser closed-PASS
Failure Scenario
Failing Condition: The network emulation commands fail or the expected elements do not appear within timeout
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify when the network is slow?
AThe page loads instantly
BThe page shows an error message
CThe page shows a loading message
DThe page redirects to another site
Key Result
Using network emulation in tests helps verify app behavior under real-world slow or offline conditions, improving user experience reliability.