Hi, in this page, we will discuss about Actions(class) and Action(interface) and mouse events example in selenium with some key take away notes.
For, keyboard events tutorial, click this link.
clickAndHold(),
clickAndHold(WebElement target),
moveToElement(WebElement target),
moveToElement(WebElement target, int xOffset, int yOffset),
release() ,
release(WebElement target) and etc.
by the method calls
ready to be performed (and resets the internal builder state,
so subsequent calls to build() will contain fresh sequences).
For, keyboard events tutorial, click this link.
Actions
- It is a class and the package is org.openqa.selenium.interactions.Actions
- It represents collection of individual Action that you want to perform.
- Using this class we can handle keyboard and mouse events. i.e.,
- Keyboard interface methods
- Mouse interface methods
Action
- Action is an interface
- It represents single user interaction.
- Using this interface, on the Actions object we perform series of actions.
- Most widely and must use method is perform() after creating series of actions and storing in Action
clickAndHold(),
clickAndHold(WebElement target),
moveToElement(WebElement target),
moveToElement(WebElement target, int xOffset, int yOffset),
release() ,
release(WebElement target) and etc.
- Take a look at all the methods availabe for this class
https://www.selenium.dev/selenium/docs/api/java/index.html?org/openqa/selenium/interactions/package-tree.html
- Definition by the book: The user-facing API for emulating complex user gestures. Use this class rather than using the Keyboard or Mouse directly.
by the method calls
- build() method
- The build() method is always the final method used
- All the listed actions will be compiled into a single step.
- Definition by the book:
ready to be performed (and resets the internal builder state,
so subsequent calls to build() will contain fresh sequences).
Use case : drag and drop images from source location to target
- Locate the source/from element which you want to drag and store it in a WebElement variable.
- Locate the target/to element where you want to drop the source element and store it in a WebElement variable.
- We work with Actions(class) & Action(interface) together so let's first create Actions object "builder" by attaching "driver" object.
- Build the series of actions on "builder" object and store it in "Action" interface variable say "drop1Image1".
NOTE : Must apply build() method at the end. - Perform the series actions using "perform()" method on the above created Action variable say "drop1Image1"
WebElement drag1FromImage1 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The peaks of High Tatras')]")); WebElement dropImagesTo = driver.findElement(By.xpath("//div[@id='trash']")); Actions builder = new Actions(driver); Action drop1Image1 = builder.clickAndHold(drag1FromImage1) .moveToElement(dropImagesTo) .release(dropImagesTo) .build(); drop1Image1.perform();
Demo site credits and courtesy : https://www.globalsqa.com/demo-site/draganddrop/
Click on the images to enlarge:
Before mouse events:
After mouse events:
Watch this space for walk through video tutorial for the example
MouseEventsActionsDragAndDropImagesDemo.java
//drag and drop - mouse events demo package mouse.event.drag.and.drop.elements; 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.Actions; import org.openqa.selenium.interactions.Action; public class MouseEventsActionsDragAndDropImagesDemo { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver_83.exe"); System.setProperty("webdriver.chrome.silentOutput", "true"); driver = new ChromeDriver(); driver.get("https://www.globalsqa.com/demo-site/draganddrop/"); driver.manage().window().maximize(); //drag and drop image from one location to another on web-page // switch to Frame driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame lazyloaded']"))); // source/from location WebElement drag1FromImage1 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The peaks of High Tatras')]")); WebElement drag2FromImage2 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'The chalet at the Green mountain lake')]")); WebElement drag3FromImage3 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'Planning the ascent')]")); WebElement drag4FromImage4 = driver.findElement(By.xpath("//*[@id='gallery']//img[contains(@alt,'On top of Kozi kopka')]")); // target/to location WebElement dropImagesTo = driver.findElement(By.xpath("//div[@id='trash']")); //Object of Actions class Actions builder = new Actions(driver); //Building the series of actions Action drop1Image1 = builder.clickAndHold(drag1FromImage1) // without releasing clicks the the image source location .moveToElement(dropImagesTo) //moves the mouse to the middle of target location .release(dropImagesTo) //releases the mouse at the current mouse i.e, at target location .build(); // build all the above 3 actions //Performing the built actions drop1Image1.perform(); Action drop2Image2 = builder.clickAndHold(drag2FromImage2) .moveToElement(dropImagesTo) .release(dropImagesTo) .build(); drop2Image2.perform(); Action drop3Image3 = builder.clickAndHold(drag3FromImage3) .moveToElement(dropImagesTo) .release(dropImagesTo) .build(); drop3Image3.perform(); Action drop4Image4 = builder.clickAndHold(drag4FromImage4) .moveToElement(dropImagesTo) .release(dropImagesTo) .build(); drop4Image4.perform(); Thread.sleep(5000); driver.quit(); } }
Stay tuned for more automation!
- Sadakar Pochampalli
No comments:
Post a Comment