In this article, I would like to share my learning related to emulation of key combination press actions in Selenium 1 & 2. In our day to day activities, we would use the modifier keys number of times while working with the application. We would use ‘Alt+F4’ key to close the window, ‘Ctrl+C’ to copy the contents and several other key combinations for various activities.
In the aforesaid key combination (say for example-Ctrl+C), first part (Ctrl) of the combination is called as modifier key – as this key modifies the intended function of the second part of the key combination.
In the leading commercial test automation tools such as QTP and Silktest – required combination of key strokes can be easily achieved with the use of designated methods such as ‘type’ method and by using the appropriate constant that corresponds to the individual keys.
But in Selenium, using these key combination requires some better understanding about the intricacies of Selenium. So, in this article – I would explain about how to use these key combinations in ‘Selenium 1 & 2’.
In Selenium 1 – we can simulate the key combination action in a relatively simple way by using the following methods: KeyPress, KeyUp and KeyDown.
We just need to use the above mentioned methods in the instance of ‘DefaultSelenium’ object, with the appropriate ascii value of the key.
In case of more than 2 key combinations, such as ‘Shift+Alt+T’ - we can use the KeyDown and KeyUp methods to achieve the required key combinations, as below:
DefaultSelenium.KeyDown(<asciivalue of the key1>);
DefaultSelenium.KeyDown(<asciivalue of the key2>);
DefaultSelenium.KeyPress(<asciivalue of the key3>);
You can refer the below mentioned link to find the ascii value of various keys: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html
In Selenium 2 – to simulate the above mentioned key combinations, we would be required to use the ‘Actions’ class of Selenium Webdriver as below:
FirefoxDriver driver = new FirefoxDriver();
Actions kpress = new Actions(driver);
Here the objective is to simulate the press action of the key combination ‘Alt+Shift+T’. To perform this action, we are using the keyDown and SendKeys method of Selenium Action class.
I would post another article about F4 keys usage and action builder quite soon. If you have any questions in the above mentioned concepts, please let me know.