Objective

Our objective in this small project is to do a mouse over on a particular link. We are not suppose to actually click it, only to hover our move on it.

We are going to use Actions available in Selenium and chain them together since there is no separate “move hover/ mouse over” action available in Selenium.

We will open a company’s website. Hover over the “About Us” option and select “Our Team” from the dropdown.

Here the complication is, you cannot navigate to “Our Team” page without opening the dropdown or directly using the url.

Our steps will be:

  1. Open “www.spartez.com”
  2. Hover on “About” option in the nav menu.
  3. Click on “Our Team” from the dropdown.

Project Setup

As always, the good practice to start a project is to start it as a Maven project. This way we can quickly get the dependencies in the project which we need. Add following dependencies to your pom.xml file.

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
</dependency>

Always make sure that Auto Import is enabled for Maven.

MouseOverTest.java

Create a java class file MouseOverTest.java. This will contain our test. Add following code to it.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MouseOverTest {
    WebDriver driver;
    Actions action;

    @BeforeTest
    public void setup(){
        System.setProperty("webdriver.chrome.driver", "pathtochromedriver/chromedriver.exe");
        driver = new ChromeDriver();
        action = new Actions(driver);
        driver.get("https://spartez.com/");
        driver.findElement(By.xpath("html/body/div[3]/div/div/a")).click();
    }

    @Test
    public void Test1(){
        WebElement about = driver.findElement(By.xpath(".//*[@id='page-header']/div/nav/ul/li[2]/a"));
        action.moveToElement(about).perform();
        driver.findElement(By.xpath(".//*[@id='page-header']/div/nav/ul/li[2]/ul/li[4]/a")).click();
    }

    @AfterTest
    public void tearDown(){
        driver.close();
    }
}

Here, we have used Actions, and created an object “action” for it. It needs to initialized with the WebDriver else you’ll end up with Null Pointer exception.

Next we identify the “About Us” button and store as a WebElement.

Next we use the action “moveToElement” to navigate to the WebElement and “perform” to execute the action. This way, the dropdown menu will be invoked.

Last step is to select the “Our Team” option from the menu and we are done!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s