What is actions class in Selenium WebDriver?

Actions class in Selenium WebDriver –

In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.

In order to perform action events, we need to use org.openqa.selenium.interactions.Actionsclass.
We need to use perform() to execute the action.
Using Action API, keyboard interactions are simple to do with webdriver. In Advanced User Interactions API, interaction with element is possible either by clicking on element or sending a Keys using sendKeys()
To use mouse actions, we need to use current location of the element and then perform the action.
The following are the regularly used mouse and keyboard events:
Method :clickAndHold()
Purpose: Clicks without releasing the current mouse location

Method : contextClick()
Purpose: Performs a context-click at the current mouse location (double click).

Method: doubleClick()
Purpose: Performs a double click at the current mouse location

Method: dragAndDrop(source,target)
Parameters: Source and Target
Purpose: Performs click and hold at the location of the source element and moves to the location of the target element then releases the mouse.

Method : dragAndDropBy(source,x-offset,y-offset)
Parameters: Source, xOffset – horizontal move, y-Offset – vertical move Offset
Purpose: Performs click and hold at the location of the source element moves by a given off set, then releases the mouse.

Method: keyDown(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a modifier key press, doesn’t release the modifier key. Subsequent interactions may assume it’s kept pressed

Method: keyUp(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a key release.

Method: moveByOffset(x-offset, y-offset)
Parameters: X-Offset , Horizontal offset, a negative value means moving the mouse to left side.
Y-Offset, vertical offset, a negative value means moving the mouse to up.
Purpose: Moves the mouse position from its current position by the given offset.

Method: moveToElement(toElement)
Parameters: toElement – Element to Move to
Purpose: It moves the Mouse to the middle of the element.

Method: release()
Purpose: It releases the left mouse button at the current mouse location.

Method: sendKeys(onElement, charSequence)
Parameters: onElement, which will receive the keyStrokes, usually text field.
charsequence- any string value representing the sequence of keyStrokes to be sent.
Purpose: It sends a series of keyStrokes onto the element

As mentioned above, we can use Actions class for following actions –

Hope This helps !!!

MouseHover action in selenium WebDriver

Mousehover action in selenium WebDriver –

In the Actions class, the user, can perform one action, by simply calling it into the driver instance, followed by the method perform().

MouseHover using Actions Class in Selenium WebDriver

Let us see about mousehover action in selenium. If the menu bar or anywhere on the page, the menu has dropdown, and the dropdown options appear only when you hover the mouse on that menu.

So, in this case, we can use mouse hover action of the Actions class in selenium

Let us see, hot this can be achieved.

In my blog (URL: http://automationtalks.com/), suppose you want to select the sub-menu (Selenium WebDriver) of Menu: Selenium.

See below screen.
MouseHover using Actions Class in Selenium WebDriver
To Achieve this, follow below mentioned steps.
  1. Open the browser.
2.    Navigate to the given URL (http://automationtalks.com/).
  1. Mouse Hover on “Selenium Tutorial” tab present in the top navigational bar.
  2. Select the Menu which you want to click on (let us consider here as – “Selenium WebDriver”.
  3. Click on the “Selenium WebDriver”.
  4. Close the browser.
First define actions class –

                        Actions act = new Actions(driver);

Then, mousehover using actions class, syntax below –

act.moveToElement(driver.findElement(“webelement_Menu_location”)).build().perform();

Now, the submenu should be visible, Then click on the submenu –

driver.findElement(“WebElement_submenu”).click();

Below is the example how can be mouse hover achieved in selenium WebDriver.

package demoPackage1;

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.Actions;
import org.testng.annotations.Test;

public class ActionsClass {
  
  @Test
  public void TestCase() throws InterruptedException{
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://automationtalks.com/");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    Actions act = new Actions(driver);

    //Define actions class & move to webelement  (Hove mouse to webelement)
    act.moveToElement(driver.findElement(By.xpath("//span[contains(.,'Selenium Tutorial')]"))).build().perform();
    
    //After mouse hover, element form dropdown is appeared, then click on it.
    driver.findElement(By.xpath("//a[contains(.,'Selenium WebDriver')]")).click();
    
    Thread.sleep(3000);
    
    
    driver.close();
    driver.quit();
    
    

    
  }

}

Hope This Helps !!!!

Right Click (Context Click) in Selenium

Right click or Context click in selenium WebDriver using Actions class-

Sometimes you’ll run into an app that has functionality hidden behind a right-click menu (a.k.a. a context menu). These menus tend to be system level menus that are untouchable by Selenium. So how do you test this functionality?

Right Click (Context Click ) using Actions Class using Selenium WebDriver
We can then select an option from the menu by traversing it with keyboard arrow keys (which we can issue with the Action Builder’s send_keys command).
In Selenium WebDriver to perform right click on any web element is not a complex process. All you have to do is create an instance of Action class and call the methods defined in it to perform the actions you want. Follow the below steps in order to perform right click on any web element:

Below are the Steps we have followed in the example:

    Identify the element
    Wait for the presence of Element
    Now perform Context click
    After that we need to select the required link.

Example –

package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class RightClickInSelenium {
 
 public static void main(String[] args){
  
  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://automationtalks.com/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  Actions act = new Actions(driver);
  act.moveToElement(driver.findElement(By.xpath("//*[@id='mbtnav']/li[3]/a"))).build().perform();
   act.contextClick(driver.findElement(By.xpath("//*[@id='mbtnav']/li[3]/ul/li[2]/a"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
  //Switching between tabs using CTRL + tab keys.
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    //Switch to current selected tab's content.
    driver.switchTo().defaultContent();  
 }

}

Here the method sendKeys(Keys.ARROW_DOWN) is used to select an option from the list. If you will not add this method, the right click on the web element will be performed and the option list which appears after the right click will get disappeared without selecting any option.

We can perform the right click on any web element using the Robot class too. Here you first need to move the mouse to the particular web element and then perform the required action…!!!

Actions class in Selenium WebDrivr

How to Verify ToolTip text using Selenium WebDriver?

What is ToolTip Text Message?

ToolTip message is the hint to GUI user. Below Screenshot shows ToolTip message.

There are two ways to read and verify tooltip message.

Verify ToolTip  text using Actions Class in Selenium WebDriver

1. Using Actions class

Whenever you focus on element and a tool-tip appears it is displayed inside a “div” which is made visible on mouse focus.

  1. Move to element using actions classActions action = new Actions(driver); action.moveToElement(‘element_to_be_focused’).build().perform();
  1. Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned.
  2. Now simply write code to wait for visibility of element having tooltip text.
new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(‘element_locator’));
  1. Get text from tooltip element using getText()
 Below is sample code  –
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class VerifyToolTipMessage {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.get("https://jqueryui.com/tooltip/");
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  
  new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
  
  boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
  
  if (isToolTipDisplayed) {
  
   String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
  System.out.println("Tooltip Text:- " + tooltipText);
  }
  driver.switchTo().defaultContent();
  
  driver.quit();
  
 }

}

2. Using “title” Attribute 

Whenever you will have tooltip then “title” attribute will be there

  package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class VerifyToolTipMessage1 {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
  String ToolTipText = driver.findElement(By.xpath(".//*[@id='mbtnav']/li[1]/a")).getAttribute("title");
  System.out.println("ToolTip message is: "+ToolTipText);
  driver.quit();
 }

}

Refer below links to learn more about Actions Class in Selenium WebDriver –

Hope this helps !!!

Perform series of actions using Actions class in selenium WebDriver?

You can build a perform series of actions using the Action and Actions classes. Just remember to close the series with the build() method. Consider the sample code below.

Perform Series of Actions Using Actions class in selenium WebDriver
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class PerformSeriesOfActions {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.navigate().to("https://www.facebook.com/");
  
  WebElement element = driver.findElement(By.xpath("//*[@id='email']"));
  
  Actions act = new Actions(driver);
  //Below line sends some text to field by converting it to uppercase, then double click the text so that it will select all, then do right click
  act.moveToElement(element).click().keyDown(element, Keys.SHIFT).sendKeys("panarkhede").keyUp(element, Keys.SHIFT).doubleClick(element).contextClick(element).build().perform();
  driver.quit();
 }

}
Observe below screen shot to see the series of actions performed by using above code –
Firstly, Clicked on text box.
then, sent text to text box by converting it to upper case.
then double clicked on the entered test, so that text is selected.
then right clicked on the text.

Hope This Helps !!!

How to use tab key (button) in selenium WebDriver?

There are multiple ways to perform Tab key functionality. Using Actions Class, Using SendKeys() method & few other. Let us talk about them one by one.

TAB Key using Actions Class in Selenium WebDriver

1. Using function sendKeys(Keys.Tab).

    WebElement inputField = driver.findElement(By.Locator("LocatorValue"));
    inputField.sendKeys(Keys.TAB);

2. If you are using Java then you can use Java ROBOT class to perform keyboard actions.

Robot robot = new Robot();
// Simulate key Events
robot.keyPress(keyEvent.VK_TAB);
robot.keyRelease(keyEvent.VK_TAB);

3. You can use Ascii value in send key function like sendKeys(//0061); I am not sure the ASCII value of Tag it’s just an example.

4. Using Actions class. –

Basically, you need to have instance of Actions class and then call sendKeys with TAB as TAB key.
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class TabKeyUsingActionsClass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.navigate().to("http://automationtalks.com/");
  
  WebElement element = driver.findElement(By.xpath("//input[@class='search']"));
  element.sendKeys("text to be searched");
   
   Actions act = new Actions(driver);
   
   act.sendKeys(Keys.TAB).build().perform();
   act.sendKeys(Keys.RETURN).build().perform();
   
 }

}

Hope This Helps !!!!

How to perform Drag Drop operation using selenium WebDriver?

Drag Drop operation can be implemented in Selenium WebDriver by using Actions class.

Using below 4 steps can explain how drag drop can be achieved using actions class.

perform Drag Drop using Actions Class using Selenium WebDriver
Step1: Create object of Actions class
Step2: Find out dragable Element.
Step3: Find out dropable Element.
Step4: Perform Drag and Drop using object of the actions class.
Syntax for drag and drop:
Actions action = new Actions(driver);
action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();
Observe below screen shot. We have one draggable element and one droppable element.
Below program illustrate drag and drop operation –
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();

  driver.navigate().to("http://jqueryui.com/resources/demos/droppable/default.html");

  WebElement draggableElement = driver.findElement(By.xpath("//*[@id='draggable']"));

  WebElement droppableElement = driver.findElement(By.xpath("//*[@id='droppable']"));

  Actions act = new Actions(driver);

  act.dragAndDrop(draggableElement, droppableElement).build().perform();

 }

}
Drag and Drop is done, refer below screenshot –
Hope this Helps!!!!

How to send text in capital letters into a text-box using Actions class in Selenium WebDriver?

We can send the text in capital letters (Upper case) in to a text box by using actions class of the selenium. Very simple.  First press the SHIFT key and then send the text which you want to convert to capital letter before sending to text box.
Send Capital letters to textbox using actions class in Selenium WebDriver
Below program illustrates the same –
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class SendTextInCapitalLetter {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  WebDriver driver = new FirefoxDriver();

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();

  driver.navigate().to("http://automationtalks.com");

  String TextToBeConvertAndSendInUpperCase = "Synchronization in Selenium";

  WebElement element = driver.findElement(By.xpath("//input[@class='search']"));

  Actions act = new Actions(driver);

  act.keyDown(element, Keys.SHIFT).sendKeys(TextToBeConvertAndSendInUpperCase).keyDown(element, Keys.SHIFT).build().perform();

 }

}
Original Text was:  “Synchronization in Selenium”;
The text sent to the text box –
Hope this helps !!! post your questions if any.

How to refresh webpage using Action class in selenium WebDriver?

Refresh webpage using Actions Class in Selenium WebDriver –
There are various ways to refresh webpage in selenium webdriver. One of them using Actions class.
Refresh WebPage using Actions class in Selenium WebDriver
Application can be refreshed in multiple ways. We can use

driver.navigate().refresh();
Method, or can send key F5.
Here, Web Page refresh can be achieved by using Actions class of the selenium WebDriver.
Below program explain this –
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class RefreshUsingActionsClass {

 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  
  WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
  
  //sleep the website for 4 seconds so that we can notice the refresh of web page
  Thread.sleep(4000);
  
  Actions act = new Actions(driver);
  
  //using below command we can refresh the webpage
  act.keyDown(Keys.CONTROL).sendKeys(Keys.F5).build().perform();
 }

}