package com.agile611.testng.webdriver;

import java.time.Duration;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class HoversImprovedTest extends BaseTest {

    @Test
    public void hoversTest() throws InterruptedException {
        driver.get("http://the-internet.herokuapp.com/hovers");
        // Find and hover over desired element
        List<WebElement> avatars = driver.findElements(By.xpath("//*[@id=\'content\']/div/div"));
        
        for(int i= 1; i <= avatars.size(); i++){
            WebElement avatar = driver.findElement(By.xpath("//*[@id=\'content\']/div/div["+i+"]/img"));
            Actions builder = new Actions(driver);
            builder.moveToElement(avatar).build().perform();
             // Wait until the hover appears
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'content\']/div/div["+i+"]/div/h5")));
            // Assert that the hover displayed
            assertTrue(driver.findElement(By.xpath("//*[@id=\'content\']/div/div["+i+"]/div/h5")).isDisplayed());
            assertTrue(driver.findElement(By.xpath("//*[@id=\'content\']/div/div["+i+"]/div/h5")).getText().contains("name: user"+i));
        }
    }
}