Saturday, September 14, 2019

Post#60.Actions Class in Webdriver


Actions Class in Webdriver
org.openqa.selenium.interactions.Actions or simply Actions class supports user to perform all the operations related with mouse and keyboard keys.
Actions class extends Object class directly and TouchActions class extends Actions class. It follows multilevel inheritance. Actions class will not help in window switch and frame switching operations
Implements the builder pattern: Builds a Composite Action containing all actions specified by the method calls.

Composite Actions: User can create series of operations using Actions class and force them work one by one continuously using a actions builder. build() method from the Actions class helps the user to perform the pattern building.
Explanation: The Actions class allows us to build a chain of actions that we would like to perform.
This means that we can build up a nice sequence of operations, for example, "Press Shift key and type something and then release", or if we want to work checkboxes, we can press CTRL and then click the required checkboxes

How to use Actions class in Selenium webdriver
1. Open Browser by setting system Property for the driver server exe file.
// set chrome driver exe path
System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
WebDriver driver = new ChromeDriver();

2. Open required webpage url : 
driver.get("https://www.google.com/");
3. Create object to Actions class to access all the non-static methods, Actions class must be imported from import org.openqa.selenium.interactions.Actions;
Actions act = new Actions(driver);

5. Use required method from actions class and pass element as parameter if parameter required

Operations Supported by Actions Class

Mouse Actions:
Mouse Actions on Selenium WebDriver helps user to perform all the operations related with mouse.
Mouse Operations like: clicking, dragging, moving, clicking and dragging, hovering, double clicking, right clicking.
1. build()
2. click()
3. clickAndHold()
4. contextClick()
5. doubleClick()
6. dragAndDrop(WebElement source, WebElement target)
7. moveByOffset(int xOffset, int yOffset)
8. moveToElement(WebElement target)
9. moveToElement(WebElement target, int xOffset, int yOffset)
10. pause(long pause)
11. perform()
12. release()

KeyBoard Actions
Selenium Webdriver Provides Api for keyboard as well through action class, with help of it we can emulate keyboard but here selenium WebDriver limits the keys to only CTRL, ALT, Shift with keyUp, KeyDown methods.
But with help of Sendkeys method we can still automate all the keys present in the keyboard.
1. sendKeys(java.lang.CharSequence... keys)
2. keyUp(java.lang.CharSequence key)
3. keyDown(java.lang.CharSequence key)

No comments:

Post a Comment