/*****************************************************************************************
* Root class for all Tiki Selenium Tests.
*****************************************************************************************/

import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
import com.saucelabs.junit.SauceOnDemandTestWatcher;

import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import static org.junit.Assert.*;

import static junit.framework.Assert.assertEquals;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.*;
import static org.openqa.selenium.OutputType.*;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.io.File;

public class TikiRootWebDriverTest implements SauceOnDemandSessionIdProvider {

	/* Set to true if you want the tests to run on saucelabs.com */
	private boolean runTestsOnSauceLab = true;
	
	/**
	 * Replace this by the information about the browser you want to use.
	 *
	 * Note that if you are running the tests LOCALLY (i.e., if you set 
	 * runTestsOnSauceLab = false), then you must have that browser on your local machine.
	 **/
	private String browserName = "firefox";
	private String browserVersion = "5";
	
	/**
	 * If you are running the tests on saucelabs (i.e., if you set 
	 * runTestsOnSauceLab = true), then you must specify the OS that you want
	 * saucelab to test on.
	 **/
	private String platformName = "UNIX";

    /**
     * Constructs a {@link SauceOnDemandAuthentication} instance.
     * 
     * It uses the SauceLabs credentials specified in the config file ~/.sauce-ondemand,
     * which should have the following format:
	 * 
	 * username=YOUR_SAUCE_USER_NAME
	 * key=YOUR_SAUCE_ACCESS_KEY
	 *
     **/
     
	public SauceOnDemandAuthentication authentication;
	
    /**
     * JUnit Rule which will mark the Sauce Job as passed/failed when the test succeeds or fails.
     */
    public @Rule
    SauceOnDemandTestWatcher resultReportingTestWatcher;

    /**
     * JUnit Rule which will record the test name of the current test.  This is referenced when creating the {@link DesiredCapabilities},
     * so that the Sauce Job is created with the test name.
     */
    public @Rule TestName testName= new TestName();

    protected WebDriver wd;

    private String sessionId;
    
    
    public TikiRootWebDriverTest() {
    	if (runTestsOnSauceLab) {
    		authentication = new SauceOnDemandAuthentication();
    		resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);
    	}
    }

    @Before
    public void setUp() throws Exception {

	    if (runTestsOnSauceLab) {
	    	/* Configure wd for running tests on SauceLabs */
        	DesiredCapabilities capabillities;
        	if (browserName == "firefox") {
         		capabillities = DesiredCapabilities.firefox();
       	 	} else if  (browserName == "chrome") {
        		capabillities = DesiredCapabilities.chrome();
       	 	} else if  (browserName == "safari") {
        		capabillities = DesiredCapabilities.safari();        		
        	} else  {
        		throw new Exception("Unknown browser name: " + browserName);
        	}
        
        	capabillities.setCapability("version", browserVersion);
        
        	if (platformName == "XP") {
        		capabillities.setCapability("platform", Platform.XP);
        	} else if (platformName == "MAC") {
        		capabillities.setCapability("platform", Platform.MAC);
        	} else if (platformName == "UNIX") {
        		capabillities.setCapability("platform", Platform.UNIX);        		
        	} else {
        		throw new Exception("Unknown platform name: " + platformName);
        	}
        
        	capabillities.setCapability("name",  testName.getMethodName());
        	this.wd = new RemoteWebDriver(
                	new URL("http://" + authentication.getUsername() + ":" + authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"),
                	capabillities);
        	this.sessionId = ((RemoteWebDriver)wd).getSessionId().toString();
        } else {
        	/* Configure wd for running tests locally */
        	if (browserName == "firefox") {
        		wd = new FirefoxDriver();
        	} else if  (browserName == "chrome") {
        		wd = new ChromeDriver();
        	} else if  (browserName == "safari") {
        		wd = new SafariDriver();
        	} else  {
        		throw new Exception("Unknown browser name: " + browserName);
        	}
        	wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        }
    }

    @Override
    public String getSessionId() {
        return sessionId;
    }  

    @After
    public void tearDown() throws Exception {
        wd.quit();
    }

}
