[3782] | 1 | package org.expressolivre.cte.common; |
---|
[3663] | 2 | |
---|
[4780] | 3 | import java.io.IOException; |
---|
| 4 | |
---|
[3782] | 5 | import org.expressolivre.cte.pages.common.LoginPage; |
---|
| 6 | import org.expressolivre.cte.pages.common.PreferencesPage; |
---|
[3663] | 7 | import org.openqa.selenium.WebDriver; |
---|
| 8 | import org.openqa.selenium.firefox.FirefoxDriver; |
---|
| 9 | import org.openqa.selenium.support.PageFactory; |
---|
[4780] | 10 | import org.testng.SkipException; |
---|
[3663] | 11 | import org.testng.annotations.AfterClass; |
---|
[3766] | 12 | import org.testng.annotations.AfterSuite; |
---|
[3663] | 13 | import org.testng.annotations.BeforeClass; |
---|
[3766] | 14 | import org.testng.annotations.BeforeSuite; |
---|
[4962] | 15 | import org.testng.annotations.Optional; |
---|
| 16 | import org.testng.annotations.Parameters; |
---|
[3663] | 17 | |
---|
| 18 | /** |
---|
| 19 | * @author L.F.Estivalet (Serpro) |
---|
| 20 | * |
---|
| 21 | * Created on Jan 4, 2011 at 3:53:19 PM |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | public class BaseTestCase implements Constants { |
---|
| 25 | |
---|
[3766] | 26 | /** Driver utilizado para rodar os testes. */ |
---|
| 27 | protected static WebDriver driver; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Antes de comecar a suite de testes abrir o navegador. |
---|
| 31 | * |
---|
| 32 | * TODO Parametrizar o driver de forma a abrir outros navegadores como o |
---|
| 33 | * Internet Explorer por exemplo. |
---|
[3789] | 34 | * |
---|
[3766] | 35 | */ |
---|
| 36 | @BeforeSuite |
---|
| 37 | public void beforeSuite() { |
---|
[4864] | 38 | // Use code below to run through a proxy. |
---|
| 39 | // Value for network.proxy.http_port should be integer (no quotes should |
---|
| 40 | // be used) and network.proxy.type should be set as 1 (ProxyType.MANUAL, |
---|
| 41 | // Manual proxy settings) |
---|
| 42 | // FirefoxProfile profile = new FirefoxProfile(); |
---|
| 43 | // profile.setPreference("network.proxy.type", 1); |
---|
| 44 | // profile.setPreference("network.proxy.http", "10.200.113.61"); |
---|
| 45 | // profile.setPreference("network.proxy.http_port", 3128); |
---|
| 46 | // driver = new FirefoxDriver(profile); |
---|
| 47 | |
---|
[3663] | 48 | driver = new FirefoxDriver(); |
---|
| 49 | driver.get(URL); |
---|
| 50 | } |
---|
| 51 | |
---|
[3766] | 52 | /** |
---|
| 53 | * Antes de cada classe de teste o login na aplicacao eh realizado. |
---|
[5099] | 54 | * |
---|
| 55 | * @throws Exception |
---|
[3766] | 56 | */ |
---|
[4962] | 57 | @Parameters({ "usuario" }) |
---|
[3766] | 58 | @BeforeClass |
---|
[5099] | 59 | public void login(@Optional("user") String usuario) throws Exception { |
---|
[3663] | 60 | LoginPage page = PageFactory.initElements(driver, LoginPage.class); |
---|
[4962] | 61 | // Se nao foi informado um usuario para logar, usar o usuario padrao. |
---|
| 62 | if ("user".equals(usuario)) { |
---|
| 63 | page.login(USER, PASS); |
---|
| 64 | } else { |
---|
| 65 | // Caso contrario busca o login e senha do usuario passado por |
---|
| 66 | // parametro. |
---|
| 67 | String user = Config.getInstance().getConfig(usuario); |
---|
| 68 | String passwd = Config.getInstance().getConfig(usuario + ".passwd"); |
---|
| 69 | page.login(user, passwd); |
---|
| 70 | } |
---|
[5099] | 71 | |
---|
| 72 | // Muitas vezes o login nao acontece gerando uma mensagem |
---|
| 73 | // "Sua sessao expirou". Para evitar isso, ficar rodando esse metodo ate |
---|
| 74 | // o login for concluido ou entao ate o numero maximo de tentativas for |
---|
| 75 | // atingido. |
---|
| 76 | if ("Sua sessão expirou".equals(page.getMessage())) { |
---|
| 77 | System.out |
---|
| 78 | .println("WARNING Sessão expirada... tentando logar novamente"); |
---|
| 79 | // tries++; |
---|
| 80 | // if (tries > 5) { |
---|
| 81 | // throw new Exception("Problema ao logar!"); |
---|
| 82 | // } |
---|
| 83 | login(usuario); |
---|
| 84 | |
---|
| 85 | } |
---|
[3663] | 86 | } |
---|
| 87 | |
---|
[3766] | 88 | /** |
---|
| 89 | * Ao final de cada classe de teste o logout na aplicacao eh realizado. |
---|
| 90 | */ |
---|
| 91 | @AfterClass |
---|
| 92 | public void logout() { |
---|
[4917] | 93 | driver.get(Constants.URL + "/logout.php"); |
---|
[3745] | 94 | } |
---|
| 95 | |
---|
[3766] | 96 | /** |
---|
| 97 | * Ao final da suite de testes fecha-se o driver (navegador). |
---|
| 98 | */ |
---|
| 99 | @AfterSuite |
---|
| 100 | public void afterSuite() { |
---|
[5025] | 101 | // driver.close(); |
---|
| 102 | driver.quit(); |
---|
[3663] | 103 | } |
---|
| 104 | |
---|
[3708] | 105 | /** |
---|
[3766] | 106 | * Abre a pagina de preferencias do usuario. |
---|
| 107 | * |
---|
| 108 | * @return |
---|
| 109 | */ |
---|
| 110 | public PreferencesPage openPreferencesPage() { |
---|
| 111 | driver.get(URL_PREFERENCES); |
---|
| 112 | return PageFactory.initElements(driver, PreferencesPage.class); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
[4780] | 116 | * Verifica a presenca de uma preferencia. |
---|
| 117 | * |
---|
| 118 | * @param preference |
---|
| 119 | * Preferencia a verificar |
---|
| 120 | * @throws IOException |
---|
| 121 | * Problema ao ler arquivo temporario das preferencias |
---|
| 122 | * @throws SkipException |
---|
| 123 | * Se a preferencia procurada nao existir, o caso de teste nao |
---|
| 124 | * deve ser executado pois a funcionalidade referente nao esta |
---|
| 125 | * disponivel na configuracao. |
---|
| 126 | */ |
---|
| 127 | public void checkPreference(String preference) throws IOException, |
---|
| 128 | SkipException { |
---|
| 129 | String contents = IOUtil.readFully("temppref.txt"); |
---|
| 130 | if (!contents.contains(preference)) { |
---|
| 131 | throw new SkipException("TESTE NÃO EXECUTADO. Preferência " |
---|
| 132 | + preference + " não está disponÃvel"); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
[3708] | 137 | * TODO Rever esse metodo. Foi o unico jeito que consegui fazer pegar o |
---|
| 138 | * numero total de mensagens importantes. O metodo apenas aguarda 5 segundos |
---|
| 139 | * antes de continuar a execucao. |
---|
| 140 | * |
---|
| 141 | * @throws Exception |
---|
| 142 | * |
---|
| 143 | * @see br.gov.serpro.cte.email.listar.ListarEmailsImportantesTestCase |
---|
| 144 | */ |
---|
| 145 | public void dummyWait() throws Exception { |
---|
| 146 | dummyWait(5); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * @param seconds |
---|
| 151 | * @throws Exception |
---|
| 152 | */ |
---|
| 153 | public void dummyWait(int seconds) throws Exception { |
---|
| 154 | for (int second = 0;; second++) { |
---|
| 155 | if (second >= seconds) { |
---|
[5099] | 156 | System.out.println("WARNING Timeout"); |
---|
[3708] | 157 | return; |
---|
| 158 | } |
---|
| 159 | Thread.sleep(1000); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
[3663] | 164 | } |
---|