Skip to main content

Actions and Action in selenium

    

    Actions are a class in selenium architecture. It is used to perform mouse and keyboard actions and Action is an interface. The main focus of these classes is to provide methods to simulate the actions done through mouse and keyboard during manual testing. Below are the methods that are mostly used in the scripts

Commands to perform mouse Actions

moveToElement(WebElement Element).doubleClick().perform()

    moveToElement method is used to navigate the mouse pointer to the middle of the element. and Doubleclick method is used to perform double click action in the mouse. While performing double click action first mouse point should be moved to that element using the moveToElement method. For all the actions performed on these actions class perform method needs to be used at last. 

        actions.moveToElement(Element).doubleClick().build().perform();

Example

        actions.moveToElement(driver.findElement(By.xpath("//a[starts-with(@title,'Go to Facebook home')]"))).doubleClick().perform();

moveToElement(WebElement Element).contextClick().perform()

    contextClick method is used to right click on the element. 

        actions.moveToElement(driver.findElement(Element).contextClick().perform();

Example

        actions.moveToElement(driver.findElement(By.xpath("//a[starts-with(@title,'Go to Facebook home')]"))).contextClick().perform();

dragAndDrop(WebElement SourceWebElement target)

    This method is used to drag specific web element and drop in the desired location. In this method, we need to mention the 2 locations. One is the element location as the source and another parameter is the drop location of that element as a target.

        Actions actions = new Actions(driver);
        actions.dragAndDrop(Element source, target location).perform();

dragAndDropBy(WebElement element, int X axis location, int Y axis location)

    There are some methods that can be used to get the axis information of a specific element. Below are the methods

        element.getLocation().getX() - This method is used to get the x-axis location of the element
        element.getLocation().getY() - This method is used to get the y-axis location of the element

    These methods will return a pixel value of int type. 

Steps to perform drag and drop using axises
1. Navigate to the page that needs to perform drag and drop operation
2. Get the X and Y axis from the source element. Source - Element location that needs to be dragged
3. Get the X and Y axis from the target location. 
4. To get the offset value, subtract the pixels from the target axis to the source axis
5. Based on the location of your application position add the desired amount of pixel to the offset value
6. Pass the source element and xoffset and yoffset value to the dragAndDropBy method.

            WebDriver driver = new ChromeDriver();
            WebElemet source = driver.findElemet(By.id("abc"));
            WebElement target = driver.findElemet(By.id("abc"));

            //Get the location details of source
            int sourceXoffset = source.getLocation().getX();
            int sourceYoffset = source.getLocation().getY();

            //Get the location details of target
            int targetXoffset = target.getLocation().getX();
            int targetYoffset = target.getLocation().getY();

            //Subtract the offset value of source value from target to get offset value
            targetXoffset  =  (targetXoffset - sourceXoffset) + Add additional value if required
            targetYoffset  =  (targetYoffset  - sourceYoffset ) + Add additional value if required
            Actions action = new Actions(driver);

            //Pass the values to the method
            action.dragAndDropBy(source, targetXoffset , targetYoffset).perform();

This method will do the action that what we will do by manual. It first clicks and hold the source element and drag that to the target location and then it will drop the element in the target location.

Commands to perform keyboard Actions

sendKeys("String value")

    This is the most commonly used method in selenium webdriver. It is used to pass the String value to the fields.

        driver.findElement(By.name("email")).sendKeys("userName");

keyDown(element,Keys key) or keyDown(Keys key)

    This is used to press the specified key. It is like in the manual we will be using the Shift key to make the letters as capital. This method will do the function of that scenario. 

        action.keyDown(By.name("email"),Keys.SHIFT).sendKeys("text To sent").perform();

keyUp(element, Keys key) or keyUp(Keys key)

    This method is used to release the pressed key. This should be used along with the keyDown method.

        action.keyDown(By.name("email"),Keys.SHIFT).sendKeys("text To         sent").keyUp(Keys.SHIFT).perform();

These are the most commonly used methods in Actions class. Check other classes in the below link
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html

Comments