Selenium actions

The next topic, that we need to discuss in this selenium tutorial is selenium Actions. Selenium has a built-in ability to handle the various types of keyboard and mouse events. To handle these events, this tool uses the org.openqa.selenium.interaction. This package is an alternative for the Keyboard and mouse events.  Moreover, this API includes several actions like drag and drop, Clicking multiple elements.

Visit Selenium elements to know the methods, it can implement. Moreover, to implement these methods we need to import the actions and action classes.

import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;

Besides, we need to instantiate the Action object

Actions  builder = new Actions(driver);

Now instantiate the action using the action object. For instance, we were going to use moveToElement() methods. The build method is the final method used. to list all the actions complied in a single step.

Action mouseOverHome = builder.moveToElement(link_home).build();

Moreover, we use perform () to execute the action object.

Since in the previous sections, we have seen the methods it can perform, now its time to execute these methods. And there are some situations like background color change before and after the actions like mouse over. So now let us see how to check the background color using and after the action element(Mouse over)

Code:

package newproject;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import Actions
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class PG7 {

public static void main(String[] args) {
String baseUrl = “URL”;
System.setProperty(“webdriver.firefox.marionette”,”C:\\geckodriver.exe”);
WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);
WebElement link_Home = driver.findElement(By.linkText(“Home”));
WebElement td_Home = driver.findElement(By.xpath(“//html/body/div”+ “/table/tbody/tr/td”+ “/table/tbody/tr/td”+ “/table/tbody/tr/td”+ “/table/tbody/tr”));

Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(link_Home)
.build();

String bgColor = td_Home.getCssValue(“background-color”);
System.out.println(“Before hover: ” + bgColor);
mouseOverHome.perform();
bgColor = td_Home.getCssValue(“background-color”);
System.out.println(“After hover: ” + bgColor);
driver.close();
}

And in it training, the next topic, that we need to discuss is drag and drop elements.

Drag and drop using Action Class:

In some situations, we may face some scenarios like drag and drop the elements from one location to the other. And the basic Elements cannot perform these kinds of tasks. So selenium uses the action classes to perform these functions. So execute the following code in your IDE  to drag and drop an application

code :

package softwareTestingMaterial;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
// import drivers
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import actions classes
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
// import wait methods
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class ActionsClass {

@Test
public void actionsClass() throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, “D:\\Selenium Environment\\Drivers\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
//Create an object ‘action’
Actions action = new Actions(driver);
//navigate to the required url where we could do drag and drop action
driver.get(“http://jqueryui.com/droppable/”);
//WebdriverWait is used to wait for a frame to be available. Once it is availble we switch to the frame to achieve our task
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(“.demo-frame”)));
//To get source locator
WebElement sourceLocator = driver.findElement(By.cssSelector(“#draggable”));
//To get target locator
WebElement targetLocator = driver.findElement(By.cssSelector(“#droppable”));
//dragAndDrop(source, target) method accepts two parameters source and locator.
//used dragAndDrop method to drag and drop the source locator to target locator
action.dragAndDrop(sourceLocator, targetLocator).build().perform();
}
}

And in this selenium tutorial , next we will discuss about the

Right click Option:

People , when seen the certain element cannot get the inner details exactly. i.e the frames / other element objects used . So at this moment, we people use to right click the element and check the methods / functions used in it . So how do you perform the same thing using Selenium Web driver? Follow the steps stated below to perform the actions

1)Launch the browser and open the application

2)Select the required element and right click on the given element

3)Go to Copy option and get the text of it and print it

4)Close  the browser to end the program

Moreover, you can implement these steps in your IDE through  the following code :

Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
// launch  web drivers
.import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
// import Action statements
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import org.testng.annotations.Test;

public class ActionsClass {

@Test
public void textInCaps() throws InterruptedException{
//Instantiating the WebDriver interface.
System.setProperty(“webdriver.chrome.driver”, “D:\\Selenium Environment\\Drivers\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
//Open the required URL
driver.get(“http://swisnl.github.io/jQuery-contextMenu/demo.html”);
//To maximize the browser
driver.manage().window().maximize();
//Create an object ‘action’ of an Actions class
Actions action = new Actions(driver);
By locator = By.cssSelector(“.context-menu-one”);
//Wait for the element. Used Explicit wait
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
WebElement rightClickElement=driver.findElement(locator);
//contextClick() method to do right click on the element
action.contextClick(rightClickElement).build().perform();
WebElement getCopyText =driver.findElement(By.cssSelector(“.context-menu-icon-copy”));
//getText() method to get the text value
String GetText = getCopyText.getText();
//To print the value
System.out.println(GetText);
//To close the browser
driver.close();
}
}

Also check best selenium user interactions of 2019

All the web pages today dont have a same length . Some web pages were greater than a single window . Then in such cases We people , usually scroll the web pages using the mouse scroller. And we can do this even through Selenium web driver. And OnlineITGuru to perform the following steps :

1)Launch the browser and open the application

2)Scroll down

3)Scroll up

4)Close the application

Code:

package softwareTestingMaterial;

import org.openqa.selenium.Keys;
// launch drivers
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// launch actions
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class ActionsClass {

@Test
public void actionsClass() throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, “D:\\Selenium Environment\\Drivers\\chromedriver.exe”);
//creating an object ‘driver’
WebDriver driver = new ChromeDriver();
//Creating an object ‘action’
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(“https://onlineitguru.com/onlinetutorials/selenium”);
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();
//driver.close();
}
}

So far i have given many examples for you. Now, I would like to  give you an assignment

Assignment:

How to perform double click action using Selenium?

Try to execute the above problem. And if you people struck up anywhere feel free to contact selenium training

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).


100% Secure Payments. All major credit & debit cards accepted.