Thursday 4 February 2016

How to perform keyboard operations in selenium webdriver

How to perform keyboard operations in selenium web driver:
===============================================

We can perform keyboard operations using Actions class.

see the following code


package blog;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class KeyBoardOperations {

WebDriver webDriver;
Actions actions;

@BeforeSuite
public void launchingBrowser() {

System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new InternetExplorerDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@Test
public void testScript() throws InterruptedException {
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();

actions=new Actions(webDriver);

//performing refresh operation using F5 button
actions.sendKeys(Keys.F5).build().perform();
  

actions.sendKeys(Keys.ARROW_DOWN).build().perform();

actions.sendKeys(Keys.ARROW_UP).build().perform();


actions.sendKeys(Keys.END).build().perform();

actions.sendKeys(Keys.HOME).build().perform();

actions.sendKeys(Keys.F3).build().perform();

}

@AfterSuite
public void closeBrowser() {
webDriver.quit();

}


}

No comments:

Post a Comment