Action Class is an advance webdriver API . By using action class methods user can interact below events:
Mouse Movement
Right Click
Drag & Drop
Double Click
Keyboard operation
Mouse Movement
Right Click
Drag & Drop
Double Click
Keyboard operation
Steps to follow Mouse movement operation:
- Find a web element in UI where you need to perform mouseover operation.
- Create an object of Action class & pass driver instance as an argument.
Ex:
Actions act = new Actions(driver);
- Perform mouse movement operation using "MoveToElement()" method.
- Also we can use Perform() method.
Ex:
Actions actions = new Actions(driver);
WebElement ele = driver.findElement(By.Id("dropdown ID"));
actions.moveToElement(ele);
WebElement ele1 = driver.findElement(By.Id("sub option ID"));
actions.moveToElement(ele1);
actions.click().build().perform();
Working with Keyboard operation :
WebDriver driver = new FirefoxDriver();
driver.get("www.facebook.com");
driver.findElement(By.Id("username")).sendKeys("xxxx");
driver.findElement(By.Id("password")).sendKeys("xxxx");
Actions actions = new Actions(driver);
actions.sendKeys(Keys.Enter).Perform();
driver.get("www.facebook.com");
driver.findElement(By.Id("username")).sendKeys("xxxx");
driver.findElement(By.Id("password")).sendKeys("xxxx");
Actions actions = new Actions(driver);
actions.sendKeys(Keys.Enter).Perform();
Question
1. Without using Click() ,how to click the buttons?
Ans. By using Submit() method.
Drag & Drop scenario :
public class DragNDropSample {
WebDriver driver;
@Test
public void DragAndDrop() {
driver = new FirefoxDriver();
driver.navigate().to("www.someurl.com");
WebElement Source = driver.findElement(By.cssSelector("source place"));
WebElement target = driver.findElement(By.cssSelector("destination place"));
dragAndDrop(Source,target);
}
}
WebDriver driver;
@Test
public void DragAndDrop() {
driver = new FirefoxDriver();
driver.navigate().to("www.someurl.com");
WebElement Source = driver.findElement(By.cssSelector("source place"));
WebElement target = driver.findElement(By.cssSelector("destination place"));
dragAndDrop(Source,target);
}
}
Some More Action Class functions:
clickAndHold()
contextClick()
doubleClick()