package org.expressolivre.cte.common; import java.io.IOException; import org.expressolivre.cte.pages.common.LoginPage; import org.expressolivre.cte.pages.common.PreferencesPage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.PageFactory; import org.testng.SkipException; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeSuite; /** * @author L.F.Estivalet (Serpro) * * Created on Jan 4, 2011 at 3:53:19 PM * */ public class BaseTestCase implements Constants { /** Driver utilizado para rodar os testes. */ protected static WebDriver driver; /** * Antes de comecar a suite de testes abrir o navegador. * * TODO Parametrizar o driver de forma a abrir outros navegadores como o * Internet Explorer por exemplo. * */ @BeforeSuite public void beforeSuite() { // Use code below to run through a proxy. // Value for network.proxy.http_port should be integer (no quotes should // be used) and network.proxy.type should be set as 1 (ProxyType.MANUAL, // Manual proxy settings) // FirefoxProfile profile = new FirefoxProfile(); // profile.setPreference("network.proxy.type", 1); // profile.setPreference("network.proxy.http", "10.200.113.61"); // profile.setPreference("network.proxy.http_port", 3128); // driver = new FirefoxDriver(profile); driver = new FirefoxDriver(); driver.get(URL); } /** * Antes de cada classe de teste o login na aplicacao eh realizado. */ @BeforeClass public void login() { LoginPage page = PageFactory.initElements(driver, LoginPage.class); page.login(USER, PASS); } /** * Ao final de cada classe de teste o logout na aplicacao eh realizado. */ @AfterClass public void logout() { driver.get(Constants.URL + "/logout.php"); } /** * Ao final da suite de testes fecha-se o driver (navegador). */ @AfterSuite public void afterSuite() { driver.close(); } /** * Abre a pagina de preferencias do usuario. * * @return */ public PreferencesPage openPreferencesPage() { driver.get(URL_PREFERENCES); return PageFactory.initElements(driver, PreferencesPage.class); } /** * Verifica a presenca de uma preferencia. * * @param preference * Preferencia a verificar * @throws IOException * Problema ao ler arquivo temporario das preferencias * @throws SkipException * Se a preferencia procurada nao existir, o caso de teste nao * deve ser executado pois a funcionalidade referente nao esta * disponivel na configuracao. */ public void checkPreference(String preference) throws IOException, SkipException { String contents = IOUtil.readFully("temppref.txt"); if (!contents.contains(preference)) { throw new SkipException("TESTE NÃO EXECUTADO. Preferência " + preference + " não está disponível"); } } /** * 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); } } }