source: devel/testlink/automation/src/test/java/br/gov/serpro/cte/common/BaseTestCase.java @ 3592

Revision 3592, 5.7 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Campos, mensagens e valores estao em properties

Line 
1package br.gov.serpro.cte.common;
2
3import java.lang.reflect.Constructor;
4import java.lang.reflect.InvocationTargetException;
5import java.lang.reflect.Method;
6import java.net.URL;
7import java.text.MessageFormat;
8import java.util.Properties;
9
10import org.junit.After;
11import org.junit.Before;
12
13import br.gov.serpro.cte.connection.DAOSelenium;
14
15import com.thoughtworks.selenium.SeleneseTestCase;
16
17/**
18 * Contem metodos comuns a todos os casos de teste do Expresso.
19 *
20 * @author L.F.Estivalet (Serpro)
21 *
22 *         Created on Nov 12, 2010 at 11:50:55 AM
23 *
24 */
25public class BaseTestCase extends SeleneseTestCase {
26        private Properties config;
27        private Properties mensagens;
28        private Properties campos;
29        private Properties valores;
30
31        @Before
32        public void setUp() throws Exception {
33                this.config = this.load("br/gov/serpro/cte/common/config.properties");
34                this.mensagens = this
35                                .load("br/gov/serpro/cte/common/mensagens.properties");
36                this.campos = this.load("br/gov/serpro/cte/common/campos.properties");
37                this.valores = this.load("br/gov/serpro/cte/common/valores.properties");
38                String url = getConfig("url");
39                DAOSelenium conn = new DAOSelenium("localhost", 4444, "*firefox", url);
40                selenium = conn.newConnection();
41        }
42
43        public String getConfig(String key) {
44                return this.config.getProperty(key);
45        }
46
47        public String getMensagem(String key) {
48                return this.mensagens.getProperty(key);
49        }
50
51        public String getCampo(String key) {
52                return this.campos.getProperty(key);
53        }
54
55        public String getCampo(String key, Object... args) {
56                return MessageFormat.format(getCampo(key), args);
57        }
58
59        public String getValor(String key) {
60                return this.valores.getProperty(key);
61        }
62
63        private Properties load(String propsName) throws Exception {
64                Properties props = new Properties();
65                URL url = ClassLoader.getSystemResource(propsName);
66                props.load(url.openStream());
67                return props;
68        }
69
70        /**
71         * Executa o login no Expresso.
72         */
73        public void login() {
74                selenium.open("/login.php");
75                selenium.type("user", getConfig("user"));
76                selenium.type("passwd", getConfig("passwd"));
77                selenium.click("submitit");
78                selenium.waitForPageToLoad(getConfig("waitPage"));
79        }
80
81        /**
82         * Executa o logout do Expresso.
83         */
84        public void logout() {
85                selenium.click("logout_id");
86                selenium.waitForPageToLoad(getConfig("waitPage"));
87        }
88
89        /**
90         * Verifica se a mensagem ao usuario foi informada pelo Expresso.
91         *
92         * @param field
93         *            Campo onde a mensagem vai ser exibida.
94         * @param message
95         *            Mensagem a verificar.
96         * @throws Exception
97         */
98        public void assertMessage(String field, String message) throws Exception {
99                // Espera pela mensagem de erro.
100                this.waitForElement(field);
101                assertEquals(message, selenium.getText(field));
102        }
103
104        /**
105         * Aguarda por um elemento aparecer na pagina.
106         *
107         * @param element
108         *            Elemento a aguardar.
109         * @throws Exception
110         *             Elemento nao encontrado
111         */
112        public void waitForElement(String element) throws Exception {
113                waitForElement(element, false);
114        }
115
116        /**
117         * Aguarda por um elemento aparecer na pagina.
118         *
119         * @param element
120         *            Elemento a aguardar.
121         * @param click
122         *            Se elemento encontrado, deseja clicar sobre o mesmo?
123         * @throws Exception
124         *             Elemento nao encontrado
125         */
126        public void waitForElement(String element, boolean click) throws Exception {
127                for (int second = 0;; second++) {
128                        if (second >= Integer.parseInt(getConfig("timeout")))
129                                fail("timeout");
130                        try {
131                                if (selenium.isElementPresent(element))
132                                        break;
133                        } catch (Exception e) {
134                        }
135                        Thread.sleep(100);
136                }
137                if (click) {
138                        selenium.click(element);
139                }
140        }
141
142        /**
143         * TODO Rever esse metodo. Foi o unico jeito que consegui fazer pegar o
144         * numero total de mensagens importantes. O metodo apenas aguarda 5 segundos
145         * antes de continuar a execucao.
146         *
147         * @throws Exception
148         *
149         * @see br.gov.serpro.cte.email.listar.ListarEmailsImportantesTestCase
150         */
151        public void dummyWait() throws Exception {
152                dummyWait(5);
153        }
154
155        /**
156         * @param seconds
157         * @throws Exception
158         */
159        public void dummyWait(int seconds) throws Exception {
160                for (int second = 0;; second++) {
161                        if (second >= seconds) {
162                                System.out.println("timeout");
163                                return;
164                        }
165                        Thread.sleep(1000);
166                }
167
168        }
169
170        public void executeTestCase(Class<? extends BaseTestCase> clazz)
171                        throws InstantiationException, IllegalAccessException,
172                        SecurityException, NoSuchMethodException, IllegalArgumentException,
173                        InvocationTargetException {
174                Object obj = clazz.newInstance();
175                Method method = clazz.getMethod("setUp");
176                method.invoke(obj);
177                method = clazz.getMethod("testa");
178                method.invoke(obj);
179                method = clazz.getMethod("tearDown");
180                method.invoke(obj);
181        }
182
183        public void executeTestCase(Class<? extends BaseTestCase> clazz,
184                        Class<?>[] constructor, Object[] arguments)
185                        throws InstantiationException, IllegalAccessException,
186                        SecurityException, NoSuchMethodException, IllegalArgumentException,
187                        InvocationTargetException {
188
189                Constructor<?> c = clazz.getConstructor(constructor);
190                Object obj = createObject(c, arguments);
191
192                Method method = clazz.getMethod("setUp");
193                method.invoke(obj);
194                method = clazz.getMethod("testa");
195                method.invoke(obj);
196                method = clazz.getMethod("tearDown");
197                method.invoke(obj);
198        }
199
200        private Object createObject(Constructor<?> constructor, Object[] arguments) {
201                Object object = null;
202
203                try {
204                        object = constructor.newInstance(arguments);
205                        return object;
206                } catch (InstantiationException e) {
207                        System.out.println(e);
208                } catch (IllegalAccessException e) {
209                        System.out.println(e);
210                } catch (IllegalArgumentException e) {
211                        System.out.println(e);
212                } catch (InvocationTargetException e) {
213                        System.out.println(e);
214                }
215                return object;
216        }
217
218        @After
219        public void tearDown() throws Exception {
220                selenium.stop();
221        }
222
223}
Note: See TracBrowser for help on using the repository browser.