package agilpizza.selenium;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.server.SeleniumServer;

public class SeleniumTestCase {
	protected static SmartSelenium sel;
	protected static final String SERVER_HOST;
	protected static final String BROWSER;
	protected static final String URL;
	protected static final String CONTEXTO;
	
	static {
		SERVER_HOST = System.getProperty("server", "localhost");
		BROWSER = System.getProperty("browser", "*custom /usr/bin/mozilla");
		URL = System.getProperty("url", "http://localhost:8180/");
		CONTEXTO = System.getProperty("contexto", "/AgilPizza");
		sel = new SmartSelenium(SERVER_HOST, SeleniumServer.DEFAULT_PORT, BROWSER, URL);
	}
	
	/**
	 * Cria uma instancia do Selenium, abre o browser. 
	 * @param serverHost serverHost
	 * @param browserStartCommand browserStartCommand
	 * @param browserUrl browserUrl
	 * @param context context
	 * @throws Exception Caso ocorra algum problema ao abrir o browser ou ao fazer o login.
	 */
	//@Parameters({"serverHost", "browserStartCommand", "browserUrl", "context"})
	//@BeforeSuite(alwaysRun = true)
	@BeforeClass
	public static void start() throws Exception {
		try {
			sel.start();
		} catch (Exception e) {
			e.printStackTrace();
			stop();
			throw e;
		}
	}
	
	/**
	 * Fecha o browser. 
	 * @throws Exception Falha ao fechar o browser.
	 */
	//@AfterSuite(alwaysRun = true)
	@AfterClass
	public static void stop() throws Exception {
		if(sel != null) {
			sel.stop();
			sel = null;
		}
	}
	
	@Override
	public String toString() {
		return 
			"SERVER_HOST: " + SERVER_HOST + "\n" +
			"BROWSER: " + BROWSER + "\n" +
			"URL: " + URL + "\n" +
			"CONTEXTO: " + CONTEXTO + "\n";
	}

}
