Test Overview
This test opens a webpage, finds a button that is not clickable by normal Selenium click, and clicks it using JavaScript. It then verifies that the click triggered the expected change on the page.
This test opens a webpage, finds a button that is not clickable by normal Selenium click, and clicks it using JavaScript. It then verifies that the click triggered the expected change on the page.
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 unittest class TestClickWithJavaScript(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_click_button_with_javascript(self): driver = self.driver wait = WebDriverWait(driver, 10) # Wait until button is present button = wait.until(EC.presence_of_element_located((By.ID, 'js-click-btn'))) # Click button using JavaScript driver.execute_script('arguments[0].click();', button) # Verify that clicking the button changed the text result_text = wait.until(EC.visibility_of_element_located((By.ID, 'result'))).text self.assertEqual(result_text, 'Button clicked!') 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 window is open and ready | - | PASS |
| 2 | Browser navigates to 'https://example.com/testpage' | Page loads with a button having ID 'js-click-btn' and a result area with ID 'result' | - | PASS |
| 3 | Wait until button with ID 'js-click-btn' is present in DOM | Button element is found in the page DOM | Button presence confirmed | PASS |
| 4 | Click the button using JavaScript executor | Button is clicked by JavaScript despite normal click being blocked | - | PASS |
| 5 | Wait until element with ID 'result' is visible and get its text | Result element is visible with updated text | Text equals 'Button clicked!' | PASS |
| 6 | Test ends and browser closes | Browser window closed | - | PASS |