package test.java.com.example.UntitledTestSuite;


import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.time.Duration;


public class UntitledTestCase_old {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  JavascriptExecutor js;
  @Before
  public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\WebDrivers");
    driver = new ChromeDriver();
    baseUrl = "https://www.google.com/";
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
    js = (JavascriptExecutor) driver;
  }

  @Test
  public void testUntitledTestCase() throws Exception {
    driver.get("https://blazedemo.com/");
    driver.findElement(By.name("fromPort")).click();
    new Select(driver.findElement(By.name("fromPort"))).selectByVisibleText("Philadelphia");
    driver.findElement(By.name("toPort")).click();
    new Select(driver.findElement(By.name("toPort"))).selectByVisibleText("New York");
    driver.findElement(By.xpath("//input[@value='Find Flights']")).click();
    driver.findElement(By.xpath("//input[@value='Choose This Flight']")).click();
    driver.findElement(By.id("inputName")).click();
    driver.findElement(By.id("inputName")).clear();
    driver.findElement(By.id("inputName")).sendKeys("sdsd");
    driver.findElement(By.id("address")).click();
    driver.findElement(By.id("address")).clear();
    driver.findElement(By.id("address")).sendKeys("sdsd");
    driver.findElement(By.id("city")).click();
    driver.findElement(By.id("city")).clear();
    driver.findElement(By.id("city")).sendKeys("sdsds");
    driver.findElement(By.id("state")).click();
    driver.findElement(By.id("state")).clear();
    driver.findElement(By.id("state")).sendKeys("sdf");
    driver.findElement(By.id("zipCode")).click();
    driver.findElement(By.id("zipCode")).clear();
    driver.findElement(By.id("zipCode")).sendKeys("1234");
    driver.findElement(By.id("cardType")).click();
    new Select(driver.findElement(By.id("cardType"))).selectByVisibleText("American Express");
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Card Type'])[1]/following::div[2]")).click();
    driver.findElement(By.id("creditCardNumber")).click();
    driver.findElement(By.id("creditCardNumber")).clear();
    driver.findElement(By.id("creditCardNumber")).sendKeys("as34341413");
    driver.findElement(By.id("creditCardMonth")).click();
    driver.findElement(By.xpath("//input[@value='Purchase Flight']")).click();
    //ERROR: Caught exception [unknown command []]
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='home'])[1]/following::h1[1]")).click();
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='home'])[1]/following::h1[1]")).click();
    assertEquals("Thank you for your purchase today!", driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='home'])[1]/following::h1[1]")).getText());
    assertEquals("BlazeDemo Confirmation", driver.getTitle());
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
