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