package com.agile611.testng.webdriver;


import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
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 DynamicControlsTest extends BaseTest {

    @Test
    public void titleTest() throws InterruptedException {
        driver.navigate().to("http://the-internet.herokuapp.com/dynamic_controls");
        WebElement checkbox = driver.findElement(By.id("checkbox"));
        assertTrue(checkbox.isDisplayed());
        checkbox.click();
        WebElement removeBtn = driver.findElement(By.xpath("//*[@id='checkbox-example']/button"));
        removeBtn.click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
        wait.until(ExpectedConditions.or(
                ExpectedConditions.presenceOfElementLocated(By.id("loading")),
                ExpectedConditions.presenceOfElementLocated(By.id("message"))));
        WebElement message = driver.findElement(By.id("message"));
        assertTrue(message.isDisplayed());  

        WebElement enableBtn = driver.findElement(By.xpath("//*[@id='input-example']/button"));
        enableBtn.click();
        wait.until(ExpectedConditions.or(
                ExpectedConditions.presenceOfElementLocated(By.id("loading")),
                ExpectedConditions.presenceOfElementLocated(By.id("message"))));
        message = driver.findElement(By.id("message"));
        assertTrue(message.isDisplayed()); 

    }
}