Test Overview
This test opens a webpage with a dropdown menu and selects options using three methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.
This test opens a webpage with a dropdown menu and selects options using three methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.
from selenium import webdriver from selenium.webdriver.support.ui import Select from selenium.webdriver.common.by import By import unittest class TestSelectMethods(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select') self.driver.switch_to.frame('iframeResult') def test_select_methods(self): select_element = self.driver.find_element(By.ID, 'cars') select = Select(select_element) # Select by value select.select_by_value('volvo') selected_option = select.first_selected_option self.assertEqual(selected_option.get_attribute('value'), 'volvo') # Select by visible text select.select_by_visible_text('Saab') selected_option = select.first_selected_option self.assertEqual(selected_option.text, 'Saab') # Select by index select.select_by_index(3) # Index 3 corresponds to 'Audi' selected_option = select.first_selected_option self.assertEqual(selected_option.text, 'Audi') 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 is open with blank page | - | PASS |
| 2 | Navigates to 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select' | Page loads with iframe containing dropdown | - | PASS |
| 3 | Switches to iframe 'iframeResult' | Inside iframe, dropdown with id 'cars' is visible | - | PASS |
| 4 | Finds dropdown element by ID 'cars' | Dropdown element located | - | PASS |
| 5 | Selects option by value 'volvo' | Dropdown option 'Volvo' is selected | Check selected option's value is 'volvo' | PASS |
| 6 | Selects option by visible text 'Saab' | Dropdown option 'Saab' is selected | Check selected option's text is 'Saab' | PASS |
| 7 | Selects option by index 3 | Dropdown option 'Audi' is selected | Check selected option's text is 'Audi' | PASS |
| 8 | Test ends and browser closes | Browser closed | - | PASS |