source: devel/testlink/automation2.0/src/test/java/org/expressolivre/cte/common/BaseTestCase.java @ 5099

Revision 5099, 4.6 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1771 - Novas alteracoes nos metodos basicos para evitar timeouts

Line 
1package org.expressolivre.cte.common;
2
3import java.io.IOException;
4
5import org.expressolivre.cte.pages.common.LoginPage;
6import org.expressolivre.cte.pages.common.PreferencesPage;
7import org.openqa.selenium.WebDriver;
8import org.openqa.selenium.firefox.FirefoxDriver;
9import org.openqa.selenium.support.PageFactory;
10import org.testng.SkipException;
11import org.testng.annotations.AfterClass;
12import org.testng.annotations.AfterSuite;
13import org.testng.annotations.BeforeClass;
14import org.testng.annotations.BeforeSuite;
15import org.testng.annotations.Optional;
16import org.testng.annotations.Parameters;
17
18/**
19 * @author L.F.Estivalet (Serpro)
20 *
21 *         Created on Jan 4, 2011 at 3:53:19 PM
22 *
23 */
24public class BaseTestCase implements Constants {
25
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.
34         *
35         */
36        @BeforeSuite
37        public void beforeSuite() {
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
48                driver = new FirefoxDriver();
49                driver.get(URL);
50        }
51
52        /**
53         * Antes de cada classe de teste o login na aplicacao eh realizado.
54         *
55         * @throws Exception
56         */
57        @Parameters({ "usuario" })
58        @BeforeClass
59        public void login(@Optional("user") String usuario) throws Exception {
60                LoginPage page = PageFactory.initElements(driver, LoginPage.class);
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                }
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                }
86        }
87
88        /**
89         * Ao final de cada classe de teste o logout na aplicacao eh realizado.
90         */
91        @AfterClass
92        public void logout() {
93                driver.get(Constants.URL + "/logout.php");
94        }
95
96        /**
97         * Ao final da suite de testes fecha-se o driver (navegador).
98         */
99        @AfterSuite
100        public void afterSuite() {
101                // driver.close();
102                driver.quit();
103        }
104
105        /**
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        /**
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        /**
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) {
156                                System.out.println("WARNING Timeout");
157                                return;
158                        }
159                        Thread.sleep(1000);
160                }
161
162        }
163
164}
Note: See TracBrowser for help on using the repository browser.