Selenium actions

Click to rate this post!
[Total: 0 Average: 0]

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
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification 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.


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

Call Now Button