package Techniques.ChildWindowHandling;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.HashSet;
import java.util.Iterator;

public class AssigmentExample {
    public static void main(String[] args) {
        WebDriver obj = new FirefoxDriver();
        obj.get("https://the-internet.herokuapp.com/");



        // creating web element of that li locator.
        WebElement text = obj.findElement(By.cssSelector("a[href='/windows']"));

        // click on that link
        text.click();

        // clicking to move on new page:
        WebElement text2= obj.findElement(By.cssSelector("div a[href='/windows/new']"));

        text2.click();

        HashSet windows = new HashSet<>(obj.getWindowHandles());

        Iterator<String> it = windows.iterator();

        String parentWindow = it.next();
        String childWindow = it.next();

        WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(8));

        obj.switchTo().window(childWindow);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class='example'] h3")));

        String dummyText2 = obj.findElement(By.cssSelector("div[class='example'] h3")).getText();

        System.out.println(dummyText2);

        obj.switchTo().window(parentWindow);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[id='content'] div h3")));

        String dummyText = obj.findElement(By.cssSelector("div[id='content'] div h3")).getText();
        System.out.println(dummyText);





    }
}
