Weekend Specials Offer - Upto 50% Off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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 Actionsimport 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 driversimport org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;//import actions classesimport org.openqa.selenium.interactions.Actions;import org.openqa.selenium.support.ui.ExpectedConditions;// import wait methodsimport org.openqa.selenium.support.ui.WebDriverWait;import org.testng.annotations.Test;

public class ActionsClass {

@Testpublic 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 actiondriver.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 taskWebDriverWait wait = new WebDriverWait(driver, 5);wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));//To get source locatorWebElement sourceLocator = driver.findElement(By.cssSelector("#draggable"));//To get target locatorWebElement 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 locatoraction.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 statementsimport org.openqa.selenium.interactions.Actions;import org.openqa.selenium.support.ui.ExpectedConditions;

import org.testng.annotations.Test;

public class ActionsClass {

@Testpublic 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 URLdriver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");//To maximize the browserdriver.manage().window().maximize();//Create an object 'action' of an Actions classActions action = new Actions(driver);By locator = By.cssSelector(".context-menu-one");//Wait for the element. Used Explicit waitWebDriverWait wait = new WebDriverWait(driver, 5);wait.until(ExpectedConditions.presenceOfElementLocated(locator));WebElement rightClickElement=driver.findElement(locator);//contextClick() method to do right click on the elementaction.contextClick(rightClickElement).build().perform();WebElement getCopyText =driver.findElement(By.cssSelector(".context-menu-icon-copy"));//getText() method to get the text valueString GetText = getCopyText.getText();//To print the valueSystem.out.println(GetText);//To close the browserdriver.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 driversimport org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;// launch actionsimport org.openqa.selenium.interactions.Actions;import org.testng.annotations.Test;

public class ActionsClass {@Testpublic 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.comdriver.get("https://onlineitguru.com/onlinetutorials/selenium");//sleep for 3secs to load the pageThread.sleep(3000);//SCROLL DOWNaction.sendKeys(Keys.PAGE_DOWN).build().perform();Thread.sleep(3000);//SCROLL UPaction.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