Skip to main content

_java

Example

Our example application is available here on the-internet. It has a few avatars displayed in a grid layout. When you hover over each of them, they display additional user information and a link to view a full profile.

Let's write a test that will hover over the first avatar and make sure that this additional information appears.

First we'll import our requisite classes (for annotations (e.g., org.junit.After, etc.), driving the browser with Selenium (e.g., org.openqa.selenium.WebDriver, etc.), and matchers for our assertions (e.g., org.hamcrest.CoreMatchers, etc.)) and start our class with some setup and teardown methods.

// filename: Hovers.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class Hovers {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
}

@After
public void tearDown() throws Exception {
driver.quit();
}
// ...

Now let's write our test.

// ...
@Test
public void hoversTest() {
driver.get("http://the-internet.herokuapp.com/hovers");

WebElement avatar = driver.findElement(By.className("figure"));
Actions builder = new Actions(driver);
builder.moveToElement(avatar).build().perform();

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("figcaption")));

assertThat(driver.findElement(By.className("figcaption")).isDisplayed(), is(Boolean.TRUE));
}

}

After visiting the page we find the first avatar and store it in a variable (avatar). We then use Selenium's action.moveToElement method and pass the avatar variable to it (which triggers the hover) and check to see if the additional user information displayed.

Expected Behavior

When you save this file and run it (e.g., mvn clean test from the command-line) here is what will happen:

  • Open the browser
  • Visit the page
  • Hover over the first avatar
  • Assert that the caption displayed to the user
  • Close the browser

Summary

Hopefully this will help you handle more complex user interactions like hovers.

Thanks to Roman Isko for contributing the initial Java code for this tip.

Happy Testing!