Selenium-11 || Use of Actions and Action Classes.

Actions and Action Classes used to handle the keyboard and mouse events. These are the inbuilt features of the Selenium library.

Methods in WebDriver object

Normally, we can handle few events via WebDriver object like click(), clear(), and sendKeys() as mentioned below:

  1. click() : To Left click on any web-element to activate it.
  2. clear(): To delete all the content from a text field.
  3. sendKeys(): To pass any key via keyboard or to type in a text field.

Code Example

public static void main(String[] args) {
		
		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
				
		  String googleURL="https://www.google.com/"; driver.get(googleURL);
		  
		  By searchXpath=By.name("q");
		  
		// Normal methods in WebDriver for input (Keyboard/Mouse)
		  // Click on Search text field
		  driver.findElement(searchXpath).click();
		  
                 //Type text in lower case
	  driver.findElement(searchXpath).sendKeys("computer");
		  
		//Type text in lower case
  driver.findElement(searchXpath).sendKeys(Keys.SHIFT+"computer");
		  
		  // We can press any key via keyboard as below we are uing Enter key.		  driver.findElement(searchXpath).sendKeys(Keys.ENTER);
		  
		  // To clear text from the Text field.
		  //driver.findElement(searchXpath).clear();
}

Actions and Action Classes

But there are scenarios where we have to use Events like Right-click via Mouse, Drag and Drop, Mouse Hover, Double click, Keyboard Keypress, Key release, sending keys to a text field. We can handle all these via Actions and Action Classes.

Steps to use Actions and Action Classes

  1. First, we have to create an object of the Actions class and associate it with the WebDriver Object.
  2. Then we have to call required methods via Actions or Action object.
  3. Now, call the build() method to create a chain of multiple actions or methods.
  4. In the end, we have to call perform() to execute the chain of multiple actions that we have created via build().

In this blog, will learn basic methods from Actions and Action classes.

MethodFeature
moveToElement(element)To mouse hover over any element to view tooltip text.
contextClick(element)To right-click on any element.
sendKeys(String)To type content in the text field.
doubleClick(element)Double click to select text.
keyDown(Keys.SHIFT)To press any modifier key like Control, Shift, etc.
dragAndDrop(sourceOption, destHolder)To perform Drag and drop activity.
clickAndHold(element)Click on any element and Hold to drag.
release()To release any element.

Mouse Hover over a web element

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String googleURL = "https://www.google.com/";
		driver.get(googleURL);

		By searchXpath = By.name("q");
		
		WebElement element=driver.findElement(searchXpath);
		
		Actions actions=new Actions(driver); 
		
		//Mouser Hover over element
		Action mouseHover=                    actions.moveToElement(element).build();
		mouseHover.perform();
}

Right Click on a web element

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String googleURL = "https://www.google.com/";
		driver.get(googleURL);

		Actions actions=new Actions(driver); 
		// Right-click via mouse 
		WebElement googleLogo=driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/img"));
		  Action rightClick=actions.contextClick(googleLogo)
		.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN)
				  .sendKeys(Keys.ENTER).build();
		  
		  rightClick.perform();
		
		  // if .sendKeys(Keys.ARROW_DOWN) doesn't work we can use Robot class as below. 
		  Robot robot=new Robot(); robot.keyPress(KeyEvent.VK_DOWN);
		  robot.keyPress(KeyEvent.VK_ENTER);
}

Type text in the text field

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String googleURL = "https://www.google.com/";
		driver.get(googleURL);

		By searchXpath = By.name("q");
		
		WebElement element=driver.findElement(searchXpath);
		
		Actions actions=new Actions(driver); 
		 // Type text in search field 
			Action textEntry=actions.keyDown(element,Keys.SHIFT)
					.sendKeys("computer")
					.sendKeys(Keys.ENTER)
					.build();
			textEntry.perform();
}

Double Click to select content in text field

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String googleURL = "https://www.google.com/";
		driver.get(googleURL);

		By searchXpath = By.name("q");
		
		WebElement element=driver.findElement(searchXpath);
		
		Actions actions=new Actions(driver); 
		// Type text in search field 
			Action textEntry=actions.keyDown(element,Keys.SHIFT)
					.sendKeys("computer")
					.sendKeys(Keys.ENTER)
					.build();
			textEntry.perform();
		 
			WebElement newSearchField=driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[2]/div/div[2]/input"));
			Action doubleClick=actions.doubleClick(newSearchField)
					.build();
			doubleClick.perform();
}

Long press of any Modifier Key

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String googleURL = "https://www.google.com/";
		driver.get(googleURL);

		By searchXpath = By.name("q");
		
		WebElement element=driver.findElement(searchXpath);
		
		Actions actions=new Actions(driver); 
			
		// Long Press of any Modifier key via Keypress method
		// Here we long press SHIFT key to type in upper case
		Action longpress=actions.keyDown(element, Keys.SHIFT)
				.sendKeys("india")
				.sendKeys(Keys.SPACE)
				.sendKeys("delhi")
				.sendKeys(Keys.SPACE)
				.keyUp(Keys.SHIFT)
				.sendKeys(Keys.SPACE)
				.sendKeys("mumbai")
				.build();
		longpress.perform();
}

Drag and Drop

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String w3SURL="https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop";
		driver.get(w3SURL);
		
		driver.switchTo().frame("iframeResult");
		WebElement sourceOption=driver.findElement(By.xpath("//*[@id=\"drag1\"]"));
		WebElement destHolder=driver.findElement(By.xpath("//*[@id=\"div1\"]"));
		
		System.out.println(sourceOption.getAttribute("src"));
		System.out.println(destHolder.getAttribute("ondrop"));
		
		Actions actions=new Actions(driver);
		actions.clickAndHold(sourceOption).build().perform();
				actions.moveToElement(destHolder).build().perform();
				actions.release().build().perform();
				
				// We can also use dragAndDrop method from actions class
		//actions.dragAndDrop(sourceOption, destHolder).build().perform();
		
		System.out.println("completed");
}

Selenium Tutorials:

TestNG:

Computer Basics:

Java Basics:
Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

JIRA:

Agile:


Leave a Comment

Your email address will not be published. Required fields are marked *

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.