1 | package br.gov.serpro.cte.common; |
---|
2 | |
---|
3 | import java.lang.reflect.Constructor; |
---|
4 | import java.lang.reflect.InvocationTargetException; |
---|
5 | import java.lang.reflect.Method; |
---|
6 | import java.net.URL; |
---|
7 | import java.text.MessageFormat; |
---|
8 | import java.util.Properties; |
---|
9 | |
---|
10 | import org.junit.After; |
---|
11 | import org.junit.Before; |
---|
12 | |
---|
13 | import br.gov.serpro.cte.connection.DAOSelenium; |
---|
14 | |
---|
15 | import 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 | */ |
---|
25 | public class BaseTestCase extends SeleneseTestCase { |
---|
26 | private Properties mensagens; |
---|
27 | private Properties campos; |
---|
28 | private Properties valores; |
---|
29 | |
---|
30 | @Before |
---|
31 | public void setUp() throws Exception { |
---|
32 | this.mensagens = this |
---|
33 | .load("br/gov/serpro/cte/common/mensagens.properties"); |
---|
34 | this.campos = this.load("br/gov/serpro/cte/common/campos.properties"); |
---|
35 | this.valores = this.load("br/gov/serpro/cte/common/valores.properties"); |
---|
36 | String url = "https://cte.serpro.gov.br"; |
---|
37 | DAOSelenium conn = new DAOSelenium("localhost", 4444, "*firefox", url); |
---|
38 | selenium = conn.newConnection(); |
---|
39 | } |
---|
40 | |
---|
41 | public String getMensagem(String key) { |
---|
42 | return this.mensagens.getProperty(key); |
---|
43 | } |
---|
44 | |
---|
45 | public String getCampo(String key) { |
---|
46 | return this.campos.getProperty(key); |
---|
47 | } |
---|
48 | |
---|
49 | public String getCampo(String key, Object... args) { |
---|
50 | return MessageFormat.format(getCampo("email.responder.todos"), args); |
---|
51 | } |
---|
52 | |
---|
53 | public String getValor(String key) { |
---|
54 | return this.valores.getProperty(key); |
---|
55 | } |
---|
56 | |
---|
57 | private Properties load(String propsName) throws Exception { |
---|
58 | Properties props = new Properties(); |
---|
59 | URL url = ClassLoader.getSystemResource(propsName); |
---|
60 | props.load(url.openStream()); |
---|
61 | return props; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Executa o login no Expresso. |
---|
66 | */ |
---|
67 | public void login() { |
---|
68 | selenium.open("/login.php"); |
---|
69 | selenium.type("user", "luiz-fernando.estivalet"); |
---|
70 | selenium.type("passwd", "senha"); |
---|
71 | selenium.click("submitit"); |
---|
72 | selenium.waitForPageToLoad("30000"); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Executa o logout do Expresso. |
---|
77 | */ |
---|
78 | public void logout() { |
---|
79 | selenium.click("logout_id"); |
---|
80 | selenium.waitForPageToLoad("30000"); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Verifica se a mensagem ao usuario foi informada pelo Expresso. |
---|
85 | * |
---|
86 | * @param field |
---|
87 | * Campo onde a mensagem vai ser exibida. |
---|
88 | * @param message |
---|
89 | * Mensagem a verificar. |
---|
90 | * @throws Exception |
---|
91 | */ |
---|
92 | public void assertMessage(String field, String message) throws Exception { |
---|
93 | // Espera pela mensagem de erro. |
---|
94 | this.waitForElement(field); |
---|
95 | assertEquals(message, selenium.getText(field)); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Aguarda por um elemento aparecer na pagina. |
---|
100 | * |
---|
101 | * @param element |
---|
102 | * Elemento a aguardar. |
---|
103 | * @throws Exception |
---|
104 | * Elemento nao encontrado |
---|
105 | */ |
---|
106 | public void waitForElement(String element) throws Exception { |
---|
107 | waitForElement(element, false); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Aguarda por um elemento aparecer na pagina. |
---|
112 | * |
---|
113 | * @param element |
---|
114 | * Elemento a aguardar. |
---|
115 | * @param click |
---|
116 | * Se elemento encontrado, deseja clicar sobre o mesmo? |
---|
117 | * @throws Exception |
---|
118 | * Elemento nao encontrado |
---|
119 | */ |
---|
120 | public void waitForElement(String element, boolean click) throws Exception { |
---|
121 | for (int second = 0;; second++) { |
---|
122 | if (second >= 60) |
---|
123 | fail("timeout"); |
---|
124 | try { |
---|
125 | if (selenium.isElementPresent(element)) |
---|
126 | break; |
---|
127 | } catch (Exception e) { |
---|
128 | } |
---|
129 | Thread.sleep(1000); |
---|
130 | } |
---|
131 | if (click) { |
---|
132 | selenium.click(element); |
---|
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("timeout"); |
---|
157 | return; |
---|
158 | } |
---|
159 | Thread.sleep(1000); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | public void executeTestCase(Class<? extends BaseTestCase> clazz) |
---|
165 | throws InstantiationException, IllegalAccessException, |
---|
166 | SecurityException, NoSuchMethodException, IllegalArgumentException, |
---|
167 | InvocationTargetException { |
---|
168 | Object obj = clazz.newInstance(); |
---|
169 | Method method = clazz.getMethod("setUp"); |
---|
170 | method.invoke(obj); |
---|
171 | method = clazz.getMethod("testa"); |
---|
172 | method.invoke(obj); |
---|
173 | method = clazz.getMethod("tearDown"); |
---|
174 | method.invoke(obj); |
---|
175 | } |
---|
176 | |
---|
177 | public void executeTestCase(Class<? extends BaseTestCase> clazz, |
---|
178 | Class<?>[] constructor, Object[] arguments) |
---|
179 | throws InstantiationException, IllegalAccessException, |
---|
180 | SecurityException, NoSuchMethodException, IllegalArgumentException, |
---|
181 | InvocationTargetException { |
---|
182 | |
---|
183 | Constructor<?> c = clazz.getConstructor(constructor); |
---|
184 | Object obj = createObject(c, arguments); |
---|
185 | |
---|
186 | Method method = clazz.getMethod("setUp"); |
---|
187 | method.invoke(obj); |
---|
188 | method = clazz.getMethod("testa"); |
---|
189 | method.invoke(obj); |
---|
190 | method = clazz.getMethod("tearDown"); |
---|
191 | method.invoke(obj); |
---|
192 | } |
---|
193 | |
---|
194 | private Object createObject(Constructor<?> constructor, Object[] arguments) { |
---|
195 | |
---|
196 | System.out.println("Constructor: " + constructor.toString()); |
---|
197 | Object object = null; |
---|
198 | |
---|
199 | try { |
---|
200 | object = constructor.newInstance(arguments); |
---|
201 | System.out.println("Object: " + object.toString()); |
---|
202 | return object; |
---|
203 | } catch (InstantiationException e) { |
---|
204 | System.out.println(e); |
---|
205 | } catch (IllegalAccessException e) { |
---|
206 | System.out.println(e); |
---|
207 | } catch (IllegalArgumentException e) { |
---|
208 | System.out.println(e); |
---|
209 | } catch (InvocationTargetException e) { |
---|
210 | System.out.println(e); |
---|
211 | } |
---|
212 | return object; |
---|
213 | } |
---|
214 | |
---|
215 | @After |
---|
216 | public void tearDown() throws Exception { |
---|
217 | selenium.stop(); |
---|
218 | } |
---|
219 | |
---|
220 | } |
---|