package org.openmrs.reference;

import static org.junit.Assert.assertEquals;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.SystemUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openmrs.reference.helper.TestProperties;
import org.openmrs.reference.page.GenericPage;
import org.openmrs.reference.page.Page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestBase {
	
	public enum WebDriverType {chrome, firefox};	// only these two for now
	
    protected static WebDriver driver;

    @BeforeClass
    public static void startWebDriver() {
        final TestProperties properties = TestProperties.instance();
        final String webDriverName = properties.getWebDriver();
        final WebDriverType webDriverType = WebDriverType.valueOf(webDriverName);
        switch (webDriverType) {
			case chrome:
				driver = setupChromeDriver();
				break;
			case firefox:
				driver = setupFirefoxDriver();
				break;
			default:
				// shrug, choose chrome as default
				driver = setupChromeDriver();
				break;
		}
        currentPage().gotoPage("/login.htm");
    }

    @AfterClass
    public static void stopWebDriver() {
        driver.quit();
    }
    
    static WebDriver setupFirefoxDriver() {
    	driver = new FirefoxDriver();
    	return driver;
    }

    static WebDriver setupChromeDriver() {
        URL resource = null;
        ClassLoader classLoader = TestBase.class.getClassLoader();

        if(SystemUtils.IS_OS_MAC_OSX) {
            resource = classLoader.getResource("chromedriver/mac/chromedriver");
        } else if(SystemUtils.IS_OS_LINUX) {
            resource = classLoader.getResource("chromedriver/linux/chromedriver");
        } else if(SystemUtils.IS_OS_WINDOWS) {
            resource = classLoader.getResource("chromedriver/windows/chromedriver.exe");
        }
        System.setProperty("webdriver.chrome.driver", resource.getPath());
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
        return driver;
    }

    /**
     * Return a Page that represents the current page, so that all the convenient
     * methods in Page can be used.
     * 
     * @return a Page
     */
    public static Page currentPage() {
    	return new GenericPage(driver);
    }

    /**
     * Assert we're on the expected page. For now, it just checks the <title> tag.
     * 
     * @param expected page
     */
	public void assertPage(Page expected) {
	    assertEquals(expected.expectedUrlPath(), currentPage().urlPath());
    }


}

@@ -71,16 +71,65 @@
 quals;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -180,16 +180,56 @@
 eUnit;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import o
@@ -320,24 +320,153 @@
 eforeClass;%0A
+import org.junit.Rule;%0Aimport org.junit.rules.TestRule;%0Aimport org.junit.rules.TestWatcher;%0Aimport org.junit.runner.Description;%0A
 import org.o
@@ -588,24 +588,107 @@
 .page.Page;%0A
+import org.openqa.selenium.OutputType;%0Aimport org.openqa.selenium.TakesScreenshot;%0A
 import org.o
@@ -1627,16 +1627,574 @@
  %7D%0A    %0A
+    // This takes a screen (well, browser) snapshot whenever there's a failure%0A    // and stores it in a %22screenshots%22 directory.%0A    @Rule%0A    public TestRule testWatcher = new TestWatcher() %7B%0A        @Override%0A        public void failed(Throwable t, Description test) %7B%0A            File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);%0A            try %7B%0A                FileUtils.copyFile(tempFile, new File(%22screenshots/%22 + test.getDisplayName() + %22.png%22));%0A            %7D catch (IOException e) %7B%0A            %7D%0A        %7D%0A    %7D;%0A    %0A
     stat

store a screenshot png when a test fails