Actions class in Selenium

import org.openqa.selenium.interactions.Actions;

Actions class can be found under the selenium’s interactions package.
Below are the methods available under Actions clas

  1. click(element)
  2. clickAndHold(element)
  3. contextClick(element)
  4. contextClick()
  5. doubleClick(element)
  6. dragAndDrop(WebElement source, WebElement target)
  7. dragAndDropBy(WebElement source, int xOffset, int yOffset)
  8. keyDown(CharSequence key)
  9. keyDown(WebElement source, CharSequence key)
  10. keyUp(CharSequence key)
  11. keyUp(WebElement source, CharSequence key)
  12. moveToElement(WebElement toElement)
  13. moveToElement(WebElement toElement, int xOffset, int yOffset)

First Example

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class ActionsDemo {

public static void main(String[] args) throws IOException {    

System.setProperty("webdriver.chrome.driver","Path+\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get("");

    /** Actions Class */

    Actions action= new Actions(driver);

    /*-------------------/

    driver.findElement(By.name("userName")).sendKeys(ReusableMethods.getConfiguration("userID"));

    driver.findElement(By.name("password")).sendKeys(ReusableMethods.getConfiguration("pwd"));

    //driver.findElement(By.name("login")).click();

    //action.click(driver.findElement(By.name("login"))).build().perform();

    /*Actions a1=action.click(driver.findElement(By.name("login")));
    Action  a=a1.build();
    a.perform();*/

    Action  builder=action.click(driver.findElement(By.name("login"))).build();

    builder.perform();


    action.moveToElement(driver.findElement(By.name("login"))).click().build().perform();;



}
}

Second Example

package org.program;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.utils.ReusableMethods;
public class ActionsExample2 {

public static void main(int[] args) throws IOException, InterruptedException {

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Ankush\\Desktop\\SSITraining\\automation\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get(ReusableMethods.getConfiguration("register"));

    /** Elements */

    WebElement firstName = driver.findElement(By.name("firstName"));

    WebElement lastName = driver.findElement(By.name("lastName"));

    /** Actions methods use -----Example 1 */

    Actions build = new Actions(driver);

    Action firstAction = build.sendKeys(firstName, "Ankush").build();

    firstAction.perform();

    // -------------

    Action secondAction = build.sendKeys(lastName, "Sahu").build();

    secondAction.perform();

    Thread.sleep(5000);

}

public static void main(double[] args) throws IOException, InterruptedException {

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Ankush\\Desktop\\SSITraining\\automation\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get(ReusableMethods.getConfiguration("register"));

    /** Elements */

    WebElement firstName = driver.findElement(By.name("firstName"));

    WebElement lastName = driver.findElement(By.name("lastName"));

    /** Actions methods use -----Example 2 */

    Actions build = new Actions(driver);

    Action actions = build.sendKeys(firstName, "Ankush").sendKeys(lastName, "Sahu").build();

    actions.perform();

    Thread.sleep(5000);

}

public static void main(String[] args) throws IOException, InterruptedException {

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Ankush\\Desktop\\SSITraining\\automation\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get(ReusableMethods.getConfiguration("register"));

    /** Elements */

    WebElement firstName = driver.findElement(By.name("firstName"));

    WebElement lastName = driver.findElement(By.name("lastName"));

    /** Actions methods use -----Example 3 */

    Actions build = new Actions(driver);

    //Action actions = build.sendKeys(firstName, "Ankush").sendKeys(lastName, "Sahu").build();

    //Action actions = build.contextClick().sendKeys(firstName, "Ankush").sendKeys(lastName, "Sahu").build();

    Action actions = build.contextClick(firstName).sendKeys(firstName, "Ankush").sendKeys(lastName, "Sahu").build();

    actions.perform();

    Thread.sleep(5000);

}}

Design a site like this with WordPress.com
Get started