PAGE 11 : MOUSE HOVER ON WEB ELEMENT IN SELENIUM WEB DRIVER


In Mouse action, we use Actions(driver), object.moveToElement(), build(). and perform().
Action class is used to perform keyboard operation and mouse hover operations.
The build() method is used to compile all the listed actions into a single step.

Let me explain you with an example:

First of all we will write the scenario of what we will be going to do. Here we will automate the http://www.flipkart.com/ and will perform the mouse hover actions.
  1. Open A firefox Browser.
  2. Navigate to the URL : http://www.flipkart.com/.
  3. Maximize the window.
  4. Now hover on the Men’s tab present in top navigational bar.
  5. Select Fossil 
  6. Select the 1st item and add it to the shopping cart.
  7. Find the number of items added to the cart
  8. Close the browser.

package mouseHover;

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;

public class MouseHoverCommand {

@Test
public void mouseHoverExample() throws InterruptedException {
WebDriver driver = new FirefoxDriver();

// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

// Go to URL
driver.get("http://www.flipkart.com/");

// Maximize Window
driver.manage().window().maximize();

// Mouse Over On " Men link "
Actions a1 = new Actions(driver);
a1.moveToElement(driver.findElement(By.xpath("//li[@data-key='men']/a/span"))).build().perform();
Thread.sleep(3000L);

// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

// Click on "Fossil" link
driver.findElement(By.xpath("//li[@class='menu-item']/a[@data-tracking-id='0_Fossil']")).click();

// Click on the selected watch
driver.findElement(By.xpath("//div[contains(@class,'pu-visual-section')]/a/img")).click();

// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

driver.findElement(By.xpath("//form[@name='buy-now-form']/input[@value='Add to Cart' and @data-is-pin-serviceable='true']")).click();

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// closing current driver window

String elementInCart = driver.findElement(By.xpath("//span[@id='item_count_in_cart_top_displayed']"))
.getText();
System.out.println("Number of elements in cart are :  " + elementInCart);

driver.close();
}
}


Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Mouse Hover Command Test Suite">
<test name="Mouse Hover Test ">
<classes>
<class name="mouseHover.MouseHoverCommand" />
</classes>
</test>
</suite>




Example-2:

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;

public class mousehover{
       public static void main(String args[]) throws InterruptedException
       {
              WebDriver driver;
              driver = new FirefoxDriver();
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
           driver.get("http://www.facebook.com");
           Thread.sleep(7000);
           WebElement element1=driver.findElement(By.xpath(".//*[@id='blueBarNAXAnchor']/div/div/div/div[1]/h1/a"));
           Actions acnew Actions(driver);
           ac.moveToElement(element1).build().perform();
           String hovertext=element1.getAttribute("title");
           System.out.println(hovertext);
       }

}

















2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

can you provide logout selenium script for flipkart.

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com