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;
import java.util.Set;

public class ChildWindow
{
    public static void main(String[] args)
    {
        WebDriver obj = new FirefoxDriver();
        obj.get("https://rahulshettyacademy.com/loginpagePractise/#");

        obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        WebElement Link = obj.findElement(By.className("blinkingText"));

        Link.click();

        // now use window methods.

        //create set.

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

        Iterator<String> it = windows.iterator(); // set iterator on window.

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


        // now switch to child window.

        obj.switchTo().window(childWindow);

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

        WebElement paragraph = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("p.im-para.red")));

        String email = paragraph.getText();


        obj.switchTo().window(parentWindow);

        WebElement emailBox = obj.findElement(By.id("username"));

        String []emailFinal = email.split(" ");
        emailFinal[4].trim();
        emailBox.sendKeys(emailFinal[4]);


    }
}
