Friday, May 22, 2020

MOUSE HOVER ACTIONS IN SELENIUM WEBDRIVER

A mouse hover is also called as hover. 
Mouse hover action is basically an action where a user places a mouse over a designated area like a hyperlink. It can cause some event to get triggered.
Selenium WebDriver has a provision to perform different mouse operations like Mouse hover, Right Click, Double-click, Drag and Drop etc



EXPLANATION- Initial few lines of code would remain same i.e. launching browser of your choice say chrome, maximising the window, deleting all cookies etc.

Mouse Hover using Action Class:
In mouser action, we use Actions(driver), object.moveToElement(), build(). and perform() to hover on any WebElement.
Action class is used to perform multiple keyboard operation and mouse hover operations.
The build() method is used to compile all the listed actions into a single step to perform any task.
Steps to Handle Mouse Hover Action & Click on Element:
Create object of Actions class to Handle Mouse Hover Action.
Move cursor on Mouse Hover WebElement.
Then click on Hover Element.

Scenario:
Feature: Launch FireFox browser and learn dropdown
Scenario: Launch ebay browser
Given User navigates to URL “http://www.ebay.in”
When User clicks “Shop by Catagory” on the homepage.
Then User hover mouse over “Shop by Catagory” on the homepage.
And User Hover mouse over “Kids toy”
Then User clicks on the “Kids toy”
Then User prints the Kids toy catagory header section and total Kids toy result

package com.automation.eng.AutomationProject;

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;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class MouseHover {

public WebDriver driver;

@Test
public void mouseHover() throws InterruptedException{

//setup the firefoxdriver using WebDriverManager
WebDriverManager.firefoxdriver().setup();
//Create driver object for Chrome
driver = new FirefoxDriver();
String url = "http://www.ebay.in";
//To maximise the browser
driver.manage().window().maximize();
//To delete all cookies
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//To invoke URL
driver.get(url);

Actions action = new Actions(driver);
WebElement shopcatagory = driver.findElement(By.xpath("//button[text()='Shop by category']"));
shopcatagory.click();

action.moveToElement(shopcatagory).build().perform();
// hover and click Kids toys
WebElement sonyElement = driver.findElement(By.linkText("Kids toys"));

action.moveToElement(sonyElement).click().build().perform();

// printing category
WebElement catagory = driver.findElement(By.xpath(" //span[text()='Kids Toys & Hobbies']"));
String PrintCatagory = catagory.getText();
// Counting total item
WebElement totalCount = driver.findElement(By.xpath("//h2[@class='srp-controls__count-heading']"));
String totolToy = totalCount.getText();

System.out.println("Total Item : " + PrintCatagory + " : " + totolToy);
}
}
/*
* Output:
* Total Item : Kids Toys & Hobbies : 1-48 of 3,062,670 Results
PASSED: mouseHover
* /
*/

No comments:

Post a Comment