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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser opened at https://example.com | - | PASS |
| 2 | Enable network emulation and set slow network conditions | Network throttling set: 2s latency, 500 kbps throughput | - | PASS |
| 3 | Refresh the page to apply network conditions | Page reloads with slow network | - | PASS |
| 4 | Wait for loading message element with ID 'loading-message' | Loading message visible on page | Verify loading message text is 'Loading...' | PASS |
| 5 | Enable network emulation and set offline mode | Network set to offline | - | PASS |
| 6 | Refresh the page to apply offline condition | Page reloads with no network | - | PASS |
| 7 | Wait for error message element with ID 'error-message' | Error message visible on page | Verify error message text is 'No internet connection' | PASS |
| 8 | Close browser and end test | Browser closed | - | PASS |