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 time
import unittest
class TestWaits(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('https://example.com/delayed_button')
def test_time_sleep_wait(self):
driver = self.driver
# Using time.sleep to wait for button
time.sleep(5) # Wait fixed 5 seconds
button = driver.find_element(By.ID, 'delayed-btn')
button.click()
message = driver.find_element(By.ID, 'message').text
self.assertEqual(message, 'Button clicked!')
def test_proper_wait(self):
driver = self.driver
# Using WebDriverWait to wait until button is clickable
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, 'delayed-btn')))
button.click()
message = wait.until(EC.visibility_of_element_located((By.ID, 'message'))).text
self.assertEqual(message, 'Button clicked!')
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()