0
0
Selenium Pythontesting~10 mins

Key combinations (key_down, key_up) in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if pressing and releasing key combinations (like Ctrl + A) works correctly on a text input field. It verifies that the text is selected after the key combination.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestKeyCombinations(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_text')
        self.driver.switch_to.frame('iframeResult')

    def test_ctrl_a_select_all(self):
        driver = self.driver
        input_box = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'fname'))
        )
        input_box.clear()
        input_box.send_keys('Hello World')

        actions = ActionChains(driver)
        actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

        selected_text = driver.execute_script("return arguments[0].value.substring(arguments[0].selectionStart, arguments[0].selectionEnd);", input_box)
        self.assertEqual(selected_text, 'Hello World')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened with URL https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_text loaded-PASS
2Switch to iframe 'iframeResult' to access input fieldInside iframe with input field visibleCheck iframe is switched successfullyPASS
3Wait until input box with id 'fname' is presentInput box is present and visibleInput box located successfullyPASS
4Clear input box and type 'Hello World'Input box contains text 'Hello World'Input box text is 'Hello World'PASS
5Perform key combination: hold Ctrl, press 'a', release CtrlText 'Hello World' is selected in input boxSelected text equals 'Hello World'PASS
6Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Input box not found or key combination does not select text
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after performing the Ctrl + A key combination?
AThat the input box is cleared
BThat all text in the input box is selected
CThat the input box is disabled
DThat the browser navigates to a new page
Key Result
Always switch to the correct iframe before interacting with elements inside it, and use ActionChains for complex keyboard actions like key combinations.