package com.agile611.testng.webdriver;

import java.io.File;
import java.time.Duration;
import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.Test;

public class UploadTest extends BaseTest {

    @Test
    public void testApp() throws InterruptedException {
        // Controla el ritme per a Safari
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(15)).pollingEvery(Duration.ofSeconds(1)).ignoring(NoSuchElementException.class);

        driver.navigate().to("https://the-internet.herokuapp.com/upload");
        WebElement fileUpload = driver.findElement(By.id("file-upload"));
        File file = new File("src" + File.separator + "test"
                + File.separator + "resources" + File.separator + "2-logo-B_activa.png");
        fileUpload.sendKeys(file.getAbsolutePath());
        WebElement buttonUpload = driver.findElement(By.id("file-submit"));
        buttonUpload.click();

        // Espera explícita para Safari
        if (System.getProperty("browser").equalsIgnoreCase("safari")) {
            Thread.sleep(2000); // Espera para Safari
        }

        WebElement uploadedFiles = driver.findElement(By.id("uploaded-files"));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("uploaded-files")));
        assertTrue(uploadedFiles.isDisplayed());
    }

}