Thursday 4 February 2016

How to perform control s operations in selenium using chord() and Robot class

How to perform control s operations in selenium(control+s):
=============================================

We can perform control+s operations by using chord() method available in Keys enums class using Actions class sendKeys() method.(Using Advanced User Interactions API)

Below is the sample example:
======================
1.Using chord() method:
-------------------------------
package blog;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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 IeDemo {

WebDriver webDriver;
Actions actions;

@BeforeSuite
public void launchingIEBrowser() {

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);
  actions.sendKeys(Keys.chord(Keys.CONTROL,"s")).build().perform();
  Thread.sleep(10000);

}

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

}


}

2.Using Robot class:
---------------------------

package blog;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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 RobotControl {

WebDriver webDriver;
Actions actions;
@BeforeSuite
public void launchingIEBrowser() {
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, AWTException {
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();
  
/*//1st way using chord() method
  actions = new Actions(webDriver);
  actions.sendKeys(Keys.chord(Keys.CONTROL,"s")).build().perform();
  */
  
//using Robot class
Robot robot = new Robot();
// press Ctrl+S the Robot's way
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_S);
// press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(10000);

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

}
}



No comments:

Post a Comment