package Locaters_Practice;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import java.time.Duration;


public class AllTypeLocators {
    public static void main(String[] args) throws InterruptedException {
        WebDriver obj = new FirefoxDriver();

        //adding waiting  to avoid to fail due to timeout issue.

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




        obj.get("https://rahulshettyacademy.com/locatorspractice/"); // to open that url.

        // using id locators.

        obj.findElement(By.id("inputUsername")).sendKeys("Zain");


        // using name locator.

        obj.findElement(By.name("inputPassword")).sendKeys("123456");


        // using css locator.

        obj.findElement(By.className("signInBtn")).click(); // tag name.className

        // extracting text using locator.

        String text = obj.findElement(By.className("error")).getText(); //  if we will not add implicit time it will not get bcz sprit emd time is less then its display time.

        System.out.println("Extracted text is:"+text);

        // linktext locator.

        obj.findElement(By.linkText("Forgot your password?")).click();

        //using locator as css selector
        obj.findElement(By.cssSelector("input[placeholder='Name']")).sendKeys("Qazi");

        // using locator as xpath selector.
        obj.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys("k201038@nu.edu.pk");

        // clearing the email box using  xpath absolute path.
        obj.findElement(By.xpath("//form/input[2]")).clear();

        // Re entering value of email using css selector regular expression.
        obj.findElement(By.cssSelector("input[placeholder*='Em']")).sendKeys("qazizain@gmail.com");


        //Entering value of phone no using xpath regular expression.
        obj.findElement(By.xpath("//input[contains(@placeholder,'Phone')]")).sendKeys("03313467894");



        WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(20));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.overlay-panel.overlay-right")));


        // clicking on reset button
        obj.findElement(By.cssSelector("button[class*='reset']")).click();


       String pass= obj.findElement(By.cssSelector("p.infoMsg")).getText();

       String password = pass.split("'")[1];

        System.out.println("Password is:"+password);

       obj.findElement(By.cssSelector("button[class*='login']")).click();


       obj.findElement(By.cssSelector("input#inputUsername")).sendKeys("Qazi");

       obj.findElement(By.cssSelector("input[placeholder='Password']")).sendKeys(password);


    Thread.sleep(2000);  // using it to make elements clickable.

    obj.findElement(By.cssSelector("input#chkboxOne")).click();

    obj.findElement(By.cssSelector("input#chkboxTwo")).click();

    obj.findElement(By.cssSelector("button[type='submit']")).click();

    String dilouge = obj.findElement(By.xpath("//div[@class='login-container']/h2")).getText();

    System.out.println("After login :"+dilouge);

    Assert.assertEquals(obj.findElement(By.xpath("//div[@class='login-container']/h2")).getText(),"Hello Qazi");  // to test the actual vs expected.



    }
}