package br.gov.serpro.cte.common; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.text.MessageFormat; import java.util.Properties; import org.junit.After; import org.junit.Before; import br.gov.serpro.cte.connection.DAOSelenium; import com.thoughtworks.selenium.SeleneseTestCase; /** * Contem metodos comuns a todos os casos de teste do Expresso. * * @author L.F.Estivalet (Serpro) * * Created on Nov 12, 2010 at 11:50:55 AM * */ public class BaseTestCase extends SeleneseTestCase { private Properties config; private Properties mensagens; private Properties campos; private Properties valores; @Before public void setUp() throws Exception { this.config = this.load("br/gov/serpro/cte/common/config.properties"); this.mensagens = this .load("br/gov/serpro/cte/common/mensagens.properties"); this.campos = this.load("br/gov/serpro/cte/common/campos.properties"); this.valores = this.load("br/gov/serpro/cte/common/valores.properties"); String url = getConfig("url"); DAOSelenium conn = new DAOSelenium("localhost", 4444, "*firefox", url); selenium = conn.newConnection(); } public String getConfig(String key) { return this.config.getProperty(key); } public String getMensagem(String key) { return this.mensagens.getProperty(key); } public String getCampo(String key) { return this.campos.getProperty(key); } public String getCampo(String key, Object... args) { return MessageFormat.format(getCampo(key), args); } public String getValor(String key) { return this.valores.getProperty(key); } private Properties load(String propsName) throws Exception { Properties props = new Properties(); URL url = ClassLoader.getSystemResource(propsName); props.load(url.openStream()); return props; } /** * Executa o login no Expresso. */ public void login() { selenium.open("/login.php"); selenium.type("user", getConfig("user")); selenium.type("passwd", getConfig("passwd")); selenium.click("submitit"); selenium.waitForPageToLoad(getConfig("waitPage")); } /** * Executa o logout do Expresso. */ public void logout() { selenium.click("logout_id"); selenium.waitForPageToLoad(getConfig("waitPage")); } /** * Verifica se a mensagem ao usuario foi informada pelo Expresso. * * @param field * Campo onde a mensagem vai ser exibida. * @param message * Mensagem a verificar. * @throws Exception */ public void assertMessage(String field, String message) throws Exception { // Espera pela mensagem de erro. this.waitForElement(field); assertEquals(message, selenium.getText(field)); } /** * Aguarda por um elemento aparecer na pagina. * * @param element * Elemento a aguardar. * @throws Exception * Elemento nao encontrado */ public void waitForElement(String element) throws Exception { waitForElement(element, false); } /** * Aguarda por um elemento aparecer na pagina. * * @param element * Elemento a aguardar. * @param click * Se elemento encontrado, deseja clicar sobre o mesmo? * @throws Exception * Elemento nao encontrado */ public void waitForElement(String element, boolean click) throws Exception { for (int second = 0;; second++) { if (second >= Integer.parseInt(getConfig("timeout"))) fail("timeout"); try { if (selenium.isElementPresent(element)) break; } catch (Exception e) { } Thread.sleep(100); } if (click) { selenium.click(element); } } /** * TODO Rever esse metodo. Foi o unico jeito que consegui fazer pegar o * numero total de mensagens importantes. O metodo apenas aguarda 5 segundos * antes de continuar a execucao. * * @throws Exception * * @see br.gov.serpro.cte.email.listar.ListarEmailsImportantesTestCase */ public void dummyWait() throws Exception { dummyWait(5); } /** * @param seconds * @throws Exception */ public void dummyWait(int seconds) throws Exception { for (int second = 0;; second++) { if (second >= seconds) { System.out.println("timeout"); return; } Thread.sleep(1000); } } public void executeTestCase(Class clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { Object obj = clazz.newInstance(); Method method = clazz.getMethod("setUp"); method.invoke(obj); method = clazz.getMethod("testa"); method.invoke(obj); method = clazz.getMethod("tearDown"); method.invoke(obj); } public void executeTestCase(Class clazz, Class[] constructor, Object[] arguments) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { Constructor c = clazz.getConstructor(constructor); Object obj = createObject(c, arguments); Method method = clazz.getMethod("setUp"); method.invoke(obj); method = clazz.getMethod("testa"); method.invoke(obj); method = clazz.getMethod("tearDown"); method.invoke(obj); } private Object createObject(Constructor constructor, Object[] arguments) { Object object = null; try { object = constructor.newInstance(arguments); return object; } catch (InstantiationException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (IllegalArgumentException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } return object; } @After public void tearDown() throws Exception { selenium.stop(); } }