Wednesday 3 July 2013

Sending special characters and key events to WebDriver using sendKeys() method


There are times at which we would like to send special characters (Enter , F5, Ctrl, Alt etc..) to webdriver from our script. This can be done by using sendKeys method itself. For this purpose we will use the Keys  method as parameter to the sendKeys method.

Syntax :
  1. //Sending F5 key  
  2. driver.findElement(By.id("name")).sendKeys(Keys.F5);  
  3.   
  4. //Sending arrow down key   
  5. driver.findElement(By.id("name")).sendKeys(Keys.ARROW_DOWN);  
  6.   
  7. //sending pagedown key from keyboard  
  8. driver.findElement(By.id("name")).sendKeys(Keys.PAGE_DOWN);  
  9.   
  10. //sending space key   
  11. driver.findElement(By.id("name")).sendKeys(Keys.SPACE);  
  12.   
  13. //sending tab key  
  14. driver.findElement(By.id("name")).sendKeys(Keys.TAB);  
  15.   
  16. //sending alt key  
  17. driver.findElement(By.id("name")).sendKeys(Keys.ALT);  
We can also send the pressable keys as Unicode PUA(Privtae User Area)  format . So the above samples can be rewritten as below :
  1. sendKeys(Keys.F5) == sendKeys("\uE035")  
  2.   
  3. sendKeys(Keys.PAGE_DOWN) == sendKeys("\uE00F")  
  4.   
  5. sendKeys(Keys.ARROW_DOWN) == sendKeys("\uE015")  
  6.   
  7. sendKeys(Keys.SPACE) == sendKeys("\uE00D")  
  8.   
  9. sendKeys(Keys.TAB) == sendKeys("\uE004")  
  10.   
  11. sendKeys(Keys.ALT) == sendKeys("\uE00A")  
Here is the sample program for logging into Facebook :
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.Keys;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.testng.annotations.BeforeTest;  
  6. import org.testng.annotations.Test;  
  7.   
  8. public class Sendkeys {  
  9.    
  10. WebDriver driver;  
  11.    
  12. @BeforeTest  
  13.  public void start(){  
  14.   driver = new FirefoxDriver();  
  15.  }  
  16.   
  17. @Test  
  18. public void sendkeysmethod(){  
  19.  //Load facebook login page  
  20.  driver.get("https://facebook.com");  
  21.    
  22.  //Refresh the page   
  23.  //We can also refresh like below   
  24.  //driver.findElement(By.name("email")).sendKeys("\uE035")  
  25.  driver.findElement(By.name("email")).sendKeys(Keys.F5);  
  26.    
  27.  //Fillup Emailadress and Password fields  
  28.  driver.findElement(By.name("email")).sendKeys("EmailAddress");  
  29.  driver.findElement(By.name("pass")).sendKeys("password");  
  30.    
  31.  //Sending Enter key so that facebook login credentials will be authenticated  
  32.  driver.findElement(By.name("pass")).sendKeys(Keys.ENTER);  
  33.    
  34.     }  
  35. }  

3 comments:

  1. What would the code be if you wanted to do ALT symbol sequences? i.e. ALT NUMPAD 2, NUMPAD2, NUMPAD7 for the Pi character? I've tried a variety of things and can't seem to get it to work.

    For example:
    public void enterPi(){
    Actions actions = new Actions(driver);
    actions.keyDown(Keys.ALT);
    StringBuilder keys = new StringBuilder();
    keys.append(Keys.NUMPAD2);
    keys.append(Keys.NUMPAD2);
    keys.append(Keys.NUMPAD7);
    enterText(Keys.chord(keys.toString()));
    actions.keyUp(Keys.ALT);
    }

    ReplyDelete
    Replies
    1. thank you very much, you save my head, i was looking for this thing

      Delete
  2. Selenium Home: Sending Special Characters And Key Events To Webdriver Using Sendkeys() Method >>>>> Download Now

    >>>>> Download Full

    Selenium Home: Sending Special Characters And Key Events To Webdriver Using Sendkeys() Method >>>>> Download LINK

    >>>>> Download Now

    Selenium Home: Sending Special Characters And Key Events To Webdriver Using Sendkeys() Method >>>>> Download Full

    >>>>> Download LINK Jq

    ReplyDelete

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...