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

Revision 3532, 16.7 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Adicionado suporte para entrar com o conteudo do email

Line 
1package br.gov.serpro.cte.common;
2
3import org.apache.commons.lang.StringUtils;
4
5/**
6 * Contem metodos comuns a todos os casos de teste do ExpressoMail.
7 *
8 * @author L.F.Estivalet (Serpro)
9 *
10 *         Created on Nov 12, 2010 at 11:52:43 AM
11 *
12 */
13public class ExpressoMailTestCase extends BaseTestCase {
14
15        /**
16         * Entra no modulo ExpressoMail.
17         *
18         * @throws Exception
19         */
20        public void enterExpressoMail() throws Exception {
21                super.login();
22                selenium.open("/expressoMail1_2/index.php");
23                selenium.waitForPageToLoad("30000");
24                // Espera a caixa de entrada carregar.
25                this.waitForElement("divScrollMain_0");
26        }
27
28        /**
29         * Procura um email pelo seu assunto.
30         *
31         * @param subject
32         *            Assunto a ser procurado.
33         * @return "id" do email.
34         */
35        public String findEmailIdBySuject(String subject) {
36                return findEmailIdBySuject(subject, false);
37        }
38
39        /**
40         * Procura um email pelo seu assunto.
41         *
42         * @param subject
43         *            Assunto a ser procurado.
44         * @param select
45         *            Se email encontrado, deseja seleciona-lo?
46         * @return "id" do email.
47         */
48        public String findEmailIdBySuject(String subject, boolean select) {
49                String html = selenium.getHtmlSource();
50                String part1 = StringUtils.substringBefore(html, subject);
51                int start = StringUtils.lastIndexOf(part1, "id=\"") + 4;
52                int last = StringUtils.indexOf(part1, "\">", start);
53                String id = StringUtils.substring(part1, start, last);
54                if (select) {
55                        // Marca checkbox correspondente ao id da mensagem.
56                        selenium.click("check_box" + id.substring(1));
57                }
58
59                // Retorna somente o "id" do email.
60                // id = id.substring(id.lastIndexOf("_"));
61
62                return id;
63        }
64
65        private void writeBody(String text) {
66                selenium.getEval("selenium.browserbot.getCurrentWindow().document.getElementById('body_1').contentWindow.document.body.innerHTML='"
67                                + text + "';");
68        }
69
70        /**
71         * Cria um novo email informando destinatario, assunto e conteudo do email.
72         *
73         * @param to
74         *            Destinatário (campo Para:).
75         * @param subject
76         *            Assunto do email.
77         * @param text
78         *            Conteudo do email.
79         * @throws Exception
80         */
81        public void composeEmail(String to, String subject, String text)
82                        throws Exception {
83                this.composeEmail();
84                selenium.type(Campo.PARA_1.getValue(), to);
85                selenium.type(Campo.ASSUNTO_1.getValue(), subject);
86                this.writeBody(text);
87
88        }
89
90        /**
91         * Cria um novo email informando destinatario, assunto e conteudo do email.
92         *
93         * @param to
94         *            Destinatário (campo Para:).
95         * @param subject
96         *            Assunto do email.
97         * @throws Exception
98         */
99        public void composeEmail(String to, String subject) throws Exception {
100                this.composeEmail();
101                selenium.type(Campo.PARA_1.getValue(), to);
102                selenium.type(Campo.ASSUNTO_1.getValue(), subject);
103        }
104
105        /**
106         * Cria um novo email.
107         *
108         * @throws Exception
109         */
110        public void composeEmail() throws Exception {
111                this.enterExpressoMail();
112                selenium.click(Campo.NOVA_MENSAGEM.getValue());
113                super.waitForElement(Campo.PARA_1.getValue());
114        }
115
116        /**
117         * @param to
118         *            Destinatário (campo Para:).
119         * @param subject
120         *            Assunto do email.
121         * @param text
122         *            Conteudo do email.
123         * @param remove
124         *            Se <code>true</code>, remove o logo apos o envio da Caixa de
125         *            Entrada, itens enviados e lixeira. Simula o envio de email ao
126         *            pressionar o botao Enviar.
127         */
128        public void sendEmail(String to, String subject, String text, boolean remove)
129                        throws Exception {
130                this.composeEmail(to, subject, text);
131                selenium.click(Campo.ENVIAR_EMAIL_1.getValue());
132                this.assertMessage(Campo.MENSAGEM.value, Mensagem.EMAIL_ENVIADO.value);
133                if (remove) {
134                        this.removeEmail(subject);
135                }
136        }
137
138        /**
139         * Pressiona opcao para enviar o email.
140         */
141        public void sendEmail() {
142                selenium.click(Campo.ENVIAR_EMAIL_1.getValue());
143        }
144
145        /**
146         * Procura o email pelo assunto e abre para leitura.
147         *
148         * @param subject
149         *            Assunto do email.
150         * @return "id" do email.
151         * @throws Exception
152         */
153        public String readEmail(String subject) throws Exception {
154                this.enterExpressoMail();
155                String id = this.findEmailIdBySuject(subject);
156                selenium.click(id);
157                // Espera ate aba da mensagem abrir.
158                super.waitForElement("//div[@id='exmail_main_body']/table[1]/tbody[@id='border_tbody']/tr[1]/td[2]/table/tbody/tr/td[1]");
159
160                // Retorna somente o "id" do email.
161                id = id.substring(id.lastIndexOf("_"));
162
163                return id;
164        }
165
166        /**
167         * Apaga o email previamente selecionado.
168         */
169        public void deleteEmail() throws Exception {
170                selenium.click("//div[@id='footer_menu']/table[@id='footer_box']/tbody/tr[@id='table_message']/td[@id='span_options']/span[1]/span");
171                this.assertMessage(Campo.MENSAGEM.getValue(),
172                                Mensagem.APAGAR_UMA_MENSAGEM.getValue());
173        }
174
175        /**
176         * Apaga o email de acordo com o assunto.
177         *
178         * @param subject
179         *            Assunto a ser pesquisado. No caso da pesquisa encontrar varios
180         *            emails, apenas o primeiro sera selecionado e apagado.
181         * @throws Exception
182         */
183        public void deleteEmail(String subject) throws Exception {
184                this.findEmailIdBySuject(subject, true);
185                this.deleteEmail();
186        }
187
188        /**
189         * Apaga email de uma pasta de acordo com o assunto.
190         *
191         * @param folder
192         *            Pasta a ser pesquisada.
193         * @param subject
194         *            Assunto a ser pesquisado.
195         * @throws Exception
196         */
197        public void deleteEmail(String folder, String subject) throws Exception {
198                // Apaga email da pasta enviados.
199                this.openFolder(folder);
200                // Espera pelo primeiro checkbox da pasta aparecer.
201                super.waitForElement(Campo.CHECKBOX_EMAIL.getValue());
202                this.deleteEmail(subject);
203                this.waitForElement(Campo.MSG_INFO.getValue());
204                assertTrue(Mensagem.PASTA_VAZIA.getValue().equals(
205                                selenium.getText(Campo.MSG_INFO.getValue())));
206        }
207
208        /**
209         * Remove o email de acordo com o assunto da Caixa de Entrada, dos itens
210         * enviados e da lixeira.
211         *
212         * @param subject
213         *            Assunto a ser pesquisado. No caso da pesquisa encontrar varios
214         *            emails, apenas o primeiro sera selecionado e apagado.
215         * @throws Exception
216         */
217        public void removeEmail(String subject) throws Exception {
218                // Faz logout para dar tempo para o email chegar na Caixa de Entrada.
219                super.logout();
220
221                // Entra novamente no ExpressoMail e apaga o email da Caixa de Entrada.
222                this.enterExpressoMail();
223                this.deleteEmail(subject);
224
225                // Apaga email da pasta enviados.
226                this.deleteEmail(Campo.PASTA_ENVIADOS.getValue(), subject);
227
228                // Limpa a lixeira.
229                super.logout();
230                this.enterExpressoMail();
231                this.cleanupTrash();
232        }
233
234        /**
235         * Limpa a lixeira.
236         *
237         * @throws Exception
238         */
239        public void cleanupTrash() throws Exception {
240                this.openFolder(Campo.PASTA_LIXEIRA.getValue());
241                selenium.click(Campo.LIMPAR_LIXEIRA.getValue());
242                assertTrue(selenium.getConfirmation().matches(
243                                Mensagem.LIMPAR_LIXEIRA.getValue()));
244                super.waitForElement(Campo.MENSAGEM.getValue());
245                assertEquals(Mensagem.LIXEIRA_LIMPA.getValue(),
246                                selenium.getText(Campo.MENSAGEM.getValue()));
247                // Garantir que nao tem nenhum email na lixeira.
248                assertEquals("0", this.getTotalEmails());
249        }
250
251        /**
252         * @return Numero de emails na pasta selecionada.
253         */
254        public int getTotalEmails() {
255                return Integer
256                                .valueOf(selenium
257                                                .getText("//div[@id='exmail_main_body']/table[@id='border_table']/tbody[@id='border_tbody']/tr[@id='border_tr']/td[@id='border_id_0']/font/span[@id='tot_m']"));
258        }
259
260        /**
261         * @return Ids de todos os emails da pasta selecionada.
262         */
263        public String[] getEmailIds() {
264                String[] ids = new String[this.getTotalEmails()];
265                for (int i = 1; i <= this.getTotalEmails(); i++) {
266                        String id = selenium
267                                        .getAttribute("//div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr["
268                                                        + i + "]/td[1]/input[1]@id");
269                        ids[i - 1] = id.substring(id.lastIndexOf("_") + 1);
270                }
271                return ids;
272        }
273
274        /**
275         * @param emailId
276         *            Id do email.
277         * @return <code>true</code> se email importante, <code>false</code> caso
278         *         contrario.
279         */
280        public boolean isImportant(String emailId) {
281                return selenium
282                                .isElementPresent("//div[@id='exmail_main_body']/div[@id='content_id_0']/div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[@id='"
283                                                + emailId + "']/td[5]/img");
284        }
285
286        /**
287         * @param emailId
288         *            Id do email.
289         * @return <code>true</code> se email lido, <code>false</code> caso
290         *         contrario.
291         */
292        public boolean isRead(String emailId) {
293                return "Lida"
294                                .equals(selenium
295                                                .getAttribute("//div[@id='exmail_main_body']/div[@id='content_id_0']/div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[@id='"
296                                                                + emailId + "']/td[7]/img@title"));
297        }
298
299        /**
300         * Abre uma pasta de email especifica.
301         *
302         * @param folder
303         *            Pasta de email a ser aberta.
304         * @throws Exception
305         */
306        public void openFolder(String folder) throws Exception {
307                selenium.click(folder);
308                this.waitForElement("divScrollMain_0");
309        }
310
311        /**
312         * Responder email.
313         *
314         * @param id
315         *            Id do email a ser respondido.
316         * @throws Exception
317         */
318        public void replyEmail(String id) throws Exception {
319                // Clicar na opcao para responder.
320                selenium.click(Campo.RESPONDER_EMAIL.getValue());
321                this.reply(id);
322        }
323
324        /**
325         * Responder email a todos destinatarios.
326         *
327         * @param id
328         *            Id do email a ser respondido.
329         * @throws Exception
330         */
331        public void replyAllEmail(String id) throws Exception {
332                // Clicar na opcao para responder.
333                selenium.click("msg_opt_reply_options" + id + "_r");
334                selenium.click("//span[@onclick='new_message(\"reply_to_all_with_history\",\""
335                                + id.substring(1) + "_r\");']");
336
337                this.reply(id);
338        }
339
340        /**
341         * Responder email a todos destinatarios sem historico.
342         *
343         * @param id
344         *            Id do email a ser respondido.
345         * @throws Exception
346         */
347        public void replyAllEmailWithoutHistory(String id) throws Exception {
348                // Clicar na opcao para responder.
349                selenium.click("msg_opt_reply_options" + id + "_r");
350                selenium.click("//span[@onclick='new_message(\"reply_without_history\",\""
351                                + id.substring(1) + "_r\");']");
352
353                this.reply(id);
354        }
355
356        /**
357         * Responder email sem historico.
358         *
359         * @param id
360         *            Id do email a ser respondido.
361         * @throws Exception
362         */
363        public void replyEmailWithoutHistory(String id) throws Exception {
364                // Clicar na opcao para responder.
365                selenium.click("//span[@onclick='new_message(\"reply_without_history\",\""
366                                + id.substring(1) + "_r\");']");
367                this.reply(id);
368        }
369
370        /**
371         * Responder email, serve apenas para clicar na opcao "Enviar".
372         *
373         * @param id
374         *            Id do email a ser respondido.
375         * @throws Exception
376         */
377        private void reply(String id) throws Exception {
378                // Espera carregar aba para responder email.
379                super.waitForElement("to" + id);
380                selenium.click("send_button" + id);
381        }
382
383        /**
384         * Encaminha uma mensagem.
385         *
386         * @param id
387         *            Id da mensagem a ser encaminhada
388         * @param to
389         *            Destinatario
390         * @throws Exception
391         */
392        public void forwardEmail(String id, String to) throws Exception {
393                // Clicar na opcao para encaminhar.
394                selenium.click(Campo.ENCAMINHAR_EMAIL.getValue());
395
396                // Espera carregar aba para encaminhar email.
397                super.waitForElement("to" + id);
398                selenium.type("to" + id, to);
399
400                selenium.click("send_button" + id);
401        }
402
403        public enum Mensagem {
404                PASTA_VAZIA("Esta pasta está vazia"), //
405                SEM_DESTINATARIO(
406                                "Você deve fornecer pelo menos um endereço de destinatário de email."), //
407                SEM_ASSUNTO("^Enviar esta mensagem sem assunto[\\s\\S]$"), //
408                EMAIL_ENVIADO("Sua mensagem foi enviada e salva."), //
409                DESTINATARIO_INVALIDO(
410                                "Erro de SMTP: Os endereços de destinatário a seguir falharam: bla@"), //
411                SALVA_RASCUNHO(
412                                "Sua mensagem foi salva como rascunho na pasta Rascunhos."), //
413                LIMPAR_LIXEIRA(
414                                "^Você tem certeza que deseja limpar sua lixeira[\\s\\S]$"), //
415                LIXEIRA_LIMPA("Sua pasta Lixeira foi limpa."), //
416                APAGAR_UMA_MENSAGEM("Mensagem movida para a pasta Lixeira"), //
417                MARCAR_IMPORTANTE("Mensagem marcada como Importante"), //
418                MARCAR_NORMAL("Mensagem marcada como Normal"), //
419                MARCAR_NAO_LIDO("Mensagem marcada como Não lida"), //
420                APAGAR_MENSAGEM("Mensagens movidas para pasta Lixeira");
421                private String value;
422
423                private Mensagem(String value) {
424                        this.value = value;
425                }
426
427                public String getValue() {
428                        return value;
429                }
430
431        };
432
433        public enum Campo {
434                ADICIONAR_ANEXO("link=exact:Anexos: adicionar+"), //
435                ANEXO_1("inputFile_1_1"), //
436                ANEXO_2("inputFile_1_2"), //
437                NOVA_MENSAGEM(
438                                "//table[@id='folders_tbl']/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span"), //
439                PARA_1("to_1"), //
440                ASSUNTO_1("subject_1"), //
441                ENVIAR_EMAIL_1("send_button_1"), //
442                CHECKBOX_EMAIL(
443                                "//div[@id='exmail_main_body']/div[@id='content_id_0']/div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[1]/td[1]/input[1]"), //
444                CONFIRMACAO_LEITURA("return_receipt_1"), //
445                IMPORTANTE(
446                                "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"), //
447                NORMAL(
448                                "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"), //
449                NAO_LIDO(
450                                "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[2]"), //
451                IMPORTANTE_CHECKBOX("important_message_1"), //
452                COPIA_1("a_cc_link_1"), //
453                SALVAR("save_message_options_1"), //
454                COPIA_CAMPO_1("cc_1"), //
455                COPIA_OCULTA_1("a_cco_link_1"), //
456                COPIA_OCULTA_CAMPO_1("cco_1"), //
457                PASTA_LIXEIRA("lINBOX/Trashtree_folders"), //
458                PASTA_ENVIADOS("lINBOX/Senttree_folders"), //
459                LIMPAR_LIXEIRA("empty_trash"), //
460                SELECIONAR_TODOS_EMAILS("chk_box_select_all_messages"), //
461                ENCAMINHAR_EMAIL(
462                                "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[3]"), //
463                RESPONDER_EMAIL(
464                                "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[5]"), //
465                MOVER_EMAIL("//span[@onclick='wfolders.makeWindow(\"\",\"move_to\")']"), //
466                MSG_INFO("msg_info"), //
467                MENSAGEM("em_div_write_msg");
468                private String value;
469
470                private Campo(String value) {
471                        this.value = value;
472                }
473
474                public String getValue() {
475                        return value;
476                }
477
478        };
479
480        public enum Valor {
481                EMAIL_TESTE_1("luiz-fernando.estivalet@cte.serpro.gov.br"), //
482                EMAIL_TESTE_2("luiz.f.estivalet@gmail.com"), //
483                EMAIL_TESTE_3("luizfernando_estivalet@yahoo.com"), //
484                EMAIL_INVALIDO("bla"), //
485                EMAIL_TESTE_MULTIPLOS(
486                                "luiz-fernando.estivalet@cte.serpro.gov.br, luizfernando_estivalet@yahoo.com"), //
487                ANEXO_1("/home/luiz/Desktop/arquivo.txt"), //
488                ANEXO_2("/home/luiz/Desktop/arquivo2.txt"), //
489                ASSUNTO("[SELENIUM] Teste envio mensagem"), //
490                ASSUNTO_SALVAR("[SELENIUM] Teste salvar mensagem"), //
491                ASSUNTO_EMAIL_INVALIDO(
492                                "[SELENIUM] Teste envio mensagem com destinatário inválido"), //
493                ASSUNTO_SEM_DESTINATARIO(
494                                "[SELENIUM] Teste envio mensagem sem destinatário"), //
495                ASSUNTO_MULTIPLOS_DESTINATARIOS(
496                                "[SELENIUM] Teste múltiplos destinatários"), //
497                ASSUNTO_ANEXO("[SELENIUM] Teste envio mensagem com anexo"), //
498                ASSUNTO_IMPORTANTE("[SELENIUM] Teste envio mensagem importante"), //
499                ASSUNTO_MULTIPLOS_ANEXOS("[SELENIUM] Teste com múltiplos anexos"), //
500                ASSUNTO_CONFIRMACAO_LEITURA(
501                                "[SELENIUM] Teste com confirmação de leitura"), //
502                ASSUNTO_COPIA("[SELENIUM] Teste envio mensagem com cópia"), //
503                ASSUNTO_COPIA_OCULTA("[SELENIUM] Teste envio mensagem com cópia oculta"), ASSUNTO_SOMENTE_COPIA(
504                                "[SELENIUM] Teste envio mensagem somente cópia"), //
505                ASSUNTO_SOMENTE_COPIA_OCULTA(
506                                "[SELENIUM] Teste mensagem somente cópia oculta"), //
507                TEXTO_EMAIL(
508                                "<p>Email enviado a partir do Selenium!</p><p>Favor ignorar!</p>"), //
509                TEXTO_EMAIL_CONFIRMACAO_LEITURA(
510                                "<p>Email enviado a partir do Selenium!</p><p>Teste de confirmação de leitura</p><p>Favor ignorar!</p>"), //
511                TEXTO_EMAIL_COPIA_OCULTA(
512                                "<p>Email enviado a partir do Selenium!</p><p>Teste de cópia oculta</p><p>Favor ignorar!</p>"), //
513                TEXTO_EMAIL_COPIA(
514                                "<p>Email enviado a partir do Selenium!</p><p>Teste de cópia</p><p>Favor ignorar!</p>"), //
515                TEXTO_EMAIL_IMPORTANTE(
516                                "<p>Email enviado a partir do Selenium!</p><p>Teste de email importante</p><p>Favor ignorar!</p>"), //
517                TEXTO_EMAIL_MULTIPLOS_ANEXOS(
518                                "<p>Email enviado a partir do Selenium!</p><p>Teste de email com múltiplos anexos</p><p>Favor ignorar!</p>"), //
519                TEXTO_EMAIL_MULTIPLOS_DESTINATARIOS(
520                                "<p>Email enviado a partir do Selenium!</p><p>Teste de email com múltiplos destinatários</p><p>Favor ignorar!</p>"), //
521                TEXTO_EMAIL_SEM_ASSUNTO(
522                                "<p>Email enviado a partir do Selenium!</p><p>Teste de email sem assunto</p><p>Favor ignorar!</p>"), //
523                TEXTO_EMAIL_SALVO(
524                                "<p>Email enviado a partir do Selenium!</p><p>Teste para salvar um email na pasta rascunhos</p><p>Favor ignorar!</p>"), //
525                TEXTO_EMAIL_ANEXO(
526                                "<p>Email enviado a partir do Selenium!</p><p>Teste de anexo</p><p>Favor ignorar!</p>");
527                private String value;
528
529                private Valor(String value) {
530                        this.value = value;
531                }
532
533                public String getValue() {
534                        return value;
535                }
536
537        };
538
539}
Note: See TracBrowser for help on using the repository browser.