// The following tests are what we discussed and created together in class on 09/11. 

package demo;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.Select;

class maintenanceTest 
{
   private WebDriver driver;
   private String url = "https://www.cs.virginia.edu/~up3f/cs3250/assigns/sut/maintenance-request.php";

   // fixture - controllability
   @BeforeEach
   public void setUp() 
   {
      System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
      driver = new FirefoxDriver();
      driver.get(url);      		   	   
   }

   @AfterEach
   public void teardown()
   { 
      driver.close();   
   }

   @Test
   public void test_pageTitle()
   {
      assertEquals(driver.getTitle(), "Maintenance Services");	
   }
   
   @Test
   public void test_pageContainRequestedDate()
   {
      assertTrue(driver.getPageSource().contains("Description of work/repair:"));
   }  
   
   @Test
   public void test_normalRequest()
   {
//	   WebElement ele = driver.findElement(By.id("roomNo")); 
//	   ele.sendKeys("10");

	   // setup
	   driver.findElement(By.id("requestedDate")).clear();
	   driver.findElement(By.id("requestedDate")).sendKeys("09/14/2026");
	   driver.findElement(By.id("roomNo")).sendKeys("10");
	   driver.findElement(By.id("requestedBy")).sendKeys("Humpty");
	   driver.findElement(By.id("requestDesc")).sendKeys("door jam");
	   
	   Select priorityOption = new Select(driver.findElement(By.id("priority_option")));
	   priorityOption.selectByValue("high");
	   // html drop down is selected by a "select" attribute
	   
	   // exe
	   driver.findElement(By.id("submitBtn")).click();
	   
	   // verify
	   // test oracle: what & how
	   
//	   System.out.println(driver.findElement(By.name("confirm-msg")).getAttribute("innerText"));	   
	   assertTrue(driver.getPageSource().contains("09/14/2026 for room# 10 has been recorded. You entered door jam with high priority"));
   }
   
   
   @Test
   public void test_BackButton()
   {
	   // setup: input: series of submissions 
	   // 1st submission 
	   driver.findElement(By.id("requestedDate")).clear();
	   driver.findElement(By.id("requestedDate")).sendKeys("09/14/2026");
	   driver.findElement(By.id("roomNo")).sendKeys("10");
	   driver.findElement(By.id("requestedBy")).sendKeys("Humpty");
	   driver.findElement(By.id("requestDesc")).sendKeys("door jam");	   
	   Select priorityOption = new Select(driver.findElement(By.id("priority_option")));
	   priorityOption.selectByValue("high");
	   driver.findElement(By.id("submitBtn")).click();
	   assertTrue(driver.getPageSource().contains("09/14/2026 for room# 10 has been recorded. You entered door jam with high priority"));

	   // 2nd submission 
	   driver.findElement(By.id("roomNo")).sendKeys("99");
	   driver.findElement(By.id("requestedBy")).sendKeys("Humpty");
	   driver.findElement(By.id("requestDesc")).sendKeys("door jam");	   
	   priorityOption = new Select(driver.findElement(By.id("priority_option")));
	   priorityOption.selectByValue("high");
	   driver.findElement(By.id("submitBtn")).click();

	   // exe
	   driver.navigate().back();
	   
	   // assert
	   assertTrue(driver.getPageSource().contains("09/14/2026 for room# 10 has been recorded. You entered door jam with high priority."));
   }

}
