package is.kings;

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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TicTacToeWebTest{

	static WebDriver driver;
  	static String baseUrl;
  	static boolean acceptNextAlert = true;
 	 static StringBuffer verificationErrors = new StringBuffer();

  	@Before
  	public void setUp() throws Exception {
    		driver = new FirefoxDriver();
    		baseUrl = "http://amazinggames.herokuapp.com/";
    		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  	}

  	@Test
  	public void testSeleniumH1() throws Exception {
    		driver.get(baseUrl);
    		driver.findElement(By.linkText("New Game")).click();
    		assertEquals("Play the amazing Tic Tac Toe game", driver.findElement(By.cssSelector("h1")).getText());
  	}

	@Test
  	public void testSelenuimParagraph() throws Exception {
    		driver.get(baseUrl);
    		driver.findElement(By.linkText("New Game")).click();
    		assertEquals("Enter a number between 1-9", driver.findElement(By.cssSelector("p")).getText());
  	}

  	@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;
    		}
  	}

}

@@ -1236,18 +1236,456 @@
 ;%0A  %09%7D%0A%0A
-  
+%09@Test%0A        public void testSelenuimInputChar() throws Exception %7B%0A%09%09driver.get(baseUrl);%0A%09%09driver.findElement(By.linkText(%22New Game%22)).click();%0A%09%09driver.findElement(By.id(%22number%22)).clear();%0A%09%09driver.findElement(By.id(%22number%22)).sendKeys(%22a%22);%0A%09%09driver.findElement(By.cssSelector(%22button%5Btype=%5C%22submit%5C%22%5D%22)).click();%0A%09%09assertEquals(%22Invalid move , try again%22, driver.findElement(By.xpath(%22//div%5B@id='results'%5D/pre%5B2%5D%22)).getText());%0A%09%7D%0A%0A
 %09@After%0A

test selenium for inputing char