import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;



public class MyTest {
	static private WebDriver driver;

	@BeforeClass
	static public void launchFirefox() throws Exception {
		System.setProperty("webdriver.firefox.bin",
				"D:\\Tools\\firefox\\firefox.exe");
		driver = new FirefoxDriver();
	}

	@AfterClass
	static public void closeFirefox() throws Exception {
		driver.close();
	}
	
	@Before
	public void setup(){
		driver.get("file:///W:/TestIHMWeb/example/index.html");
	}

	@Test
	public void testDisplaysDefaultText() {
		assertTrue(driver.getPageSource().contains("Hello I'm the test page"));
	}
	
	@Test
	public void testDisplays0InTextZone() {
		assertEquals("0", getTextFieldValue());
	}

	private String getTextFieldValue() {
		WebElement element = driver.findElement(By.name("test_text"));
		return element.getValue();
	}

	@Test
	public void testCanUseAddButton() {
		click_inc();
		assertEquals("1", getTextFieldValue());
	}

	@Test
	public void testCanUseDecButton() {
		click_dec();
		assertEquals("-1", getTextFieldValue());
	}
	
	@Test
	public void testCanPlayWithButtons() {
		click_dec();
		click_dec();
		click_dec();
		click_inc();
		click_inc();
		click_inc();
		click_dec();
		assertEquals("-1", getTextFieldValue());
	}
	
	private void click_inc() {
		click("test_button_inc");
	}

	private void click_dec() {
		click("test_button_dec");
	}
	
	private void click(String name) {
		WebElement element = driver.findElement(By.name(name));
		element.click();
	}

}
