source: devel/testlink/automation2.0/src/test/java/br/gov/serpro/expresso/cte/pages/email/MailPage.java @ 3766

Revision 3766, 14.5 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Novos casos de teste implementados usando WebDriver?

Line 
1package br.gov.serpro.expresso.cte.pages.email;
2
3import org.apache.commons.lang.StringUtils;
4import org.openqa.selenium.Alert;
5import org.openqa.selenium.By;
6import org.openqa.selenium.JavascriptExecutor;
7import org.openqa.selenium.RenderedWebElement;
8import org.openqa.selenium.WebDriver;
9import org.openqa.selenium.WebElement;
10import org.openqa.selenium.support.FindBy;
11
12import br.gov.serpro.expresso.cte.pages.common.Page;
13
14/**
15 * @author L.F.Estivalet (Serpro)
16 *
17 *         Created on Dec 16, 2010 at 9:43:21 AM
18 *
19 */
20public class MailPage extends Page {
21
22        @FindBy(xpath = "//table[@id='folders_tbl']/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span")
23        private WebElement newEmail;
24
25        @FindBy(id = "to_1")
26        private WebElement to;
27
28        @FindBy(id = "subject_1")
29        private WebElement subject;
30
31        @FindBy(id = "send_button_1")
32        private WebElement send;
33
34        @FindBy(id = "important_message_1")
35        /** Flag de importante no momento de compor um email. */
36        private WebElement important;
37
38        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]")
39        /** Flag de importante de um email aberto. */
40        private WebElement importantFlag;
41
42        @FindBy(id = "span_flag_FLAGGED")
43        /** Filtra emails importantes. */
44        private WebElement importantFilter;
45
46        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]")
47        private WebElement normalFlag;
48
49        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[2]")
50        private WebElement notReadFlag;
51
52        @FindBy(id = "return_receipt_1")
53        private WebElement returnReceipt;
54
55        @FindBy(id = "a_cco_link_1")
56        private WebElement blindCarbonCopyLink;
57
58        @FindBy(id = "cco_1")
59        private WebElement blindCarbonCopy;
60
61        @FindBy(id = "a_cc_link_1")
62        private WebElement carbonCopyLink;
63
64        @FindBy(id = "cc_1")
65        private WebElement carbonCopy;
66
67        @FindBy(id = "save_message_options_1")
68        private WebElement saveEmail;
69
70        @FindBy(linkText = "Anexos: adicionar+")
71        private WebElement attachmentLink;
72
73        @FindBy(id = "inputFile_1_1")
74        private WebElement attachment;
75
76        @FindBy(id = "inputFile_1_2")
77        private WebElement attachment2;
78
79        @FindBy(xpath = "//div[@id='footer_menu']/table[@id='footer_box']/tbody/tr[@id='table_message']/td[@id='span_options']/span[1]/span")
80        private WebElement deleteEmailLink;
81
82        @FindBy(id = "lINBOX/Trashtree_folders")
83        private WebElement trashFolder;
84
85        @FindBy(id = "empty_trash")
86        private WebElement emptyTrashLink;
87
88        @FindBy(id = "divScrollMain_0")
89        private WebElement emailsListDiv;
90
91        @FindBy(id = "tot_m")
92        private WebElement totalEmails;
93
94        @FindBy(xpath = "//div[@id='exmail_main_body']/table[1]/tbody[@id='border_tbody']/tr[1]/td[2]/table/tbody/tr/td[1]")
95        private WebElement emailTab;
96
97        @FindBy(id = "chk_box_select_all_messages")
98        private WebElement selectAllEmails;
99
100        @FindBy(xpath = "//td[@id='link_tools']/div/span")
101        private WebElement toolsMenu;
102
103        @FindBy(xpath = "//div[@id='menu_tools']/table/tbody/tr[4]/td")
104        private WebElement editFolder;
105
106        @FindBy(xpath = "//input[@value='Nova Pasta']")
107        private WebElement createNewFolder;
108
109        @FindBy(xpath = "//input[@value='Excluir pasta']")
110        private WebElement deleteFolder;
111
112        @FindBy(xpath = "//span[@onclick='wfolders.makeWindow(\"\",\"move_to\")']")
113        /** Opcao de mover do rodape. */
114        private WebElement moveEmailLink;
115
116        @FindBy(id = "dJSWin_wfolders_bok")
117        private WebElement moveEmailButton;
118
119        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[3]")
120        private WebElement forwardEmail;
121
122        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[5]")
123        private WebElement replyEmail;
124
125        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[1]")
126        private WebElement replyAllEmail;
127
128        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[2]")
129        private WebElement replyEmailWithoutHistory;
130
131        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[3]")
132        private WebElement replyAllEmailWithoutHistory;
133
134        @FindBy(xpath = "//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr/td[2]/span[2]/span[1]")
135        private WebElement editEmail;
136
137        /**
138         * @param driver
139         */
140        public MailPage(WebDriver driver) {
141                super(driver);
142        }
143
144        public void createNewEmail() {
145                this.newEmail.click();
146                waitForElement(By.id("to_1"));
147        }
148
149        public void setSubject(String subject) {
150                this.subject.sendKeys(subject != null ? subject : "");
151        }
152
153        public void setBody(String body) {
154                // Foi o unico jeito que consegui pegar o richtexteditor...
155                if (body != null) {
156                        ((JavascriptExecutor) driver)
157                                        .executeScript("document.getElementById('body_1').contentWindow.document.body.innerHTML='"
158                                                        + body + "';");
159                }
160        }
161
162        /**
163         * @param to
164         * @param subject
165         * @param body
166         */
167        public void composeEmail(String to, String subject, String body) {
168                this.createNewEmail();
169                this.to.sendKeys(to);
170                this.setSubject(subject);
171                this.setBody(body);
172        }
173
174        public String readEmail(String subject) {
175                // waitForElementByXpath("//div[@id='exmail_main_body']/table[1]/tbody[@id='border_tbody']/tr[1]/td[2]/table/tbody/tr/td[1]");
176                String id = findEmailIdBySuject(subject);
177                id = id.substring(id.lastIndexOf("_") + 1);
178                clickElement(By.id("td_who_" + id));
179                super.waitForElement(By.id("div_message_scroll_" + id + "_r"));
180                return id;
181        }
182
183        public void editEmail() {
184                super.waitForElement(By
185                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr/td[2]/span[2]/span[1]"));
186                this.editEmail.click();
187        }
188
189        /**
190         * Encaminha uma mensagem.
191         *
192         * @param id
193         *            Id da mensagem a ser encaminhada
194         * @param to
195         *            Destinatario
196         */
197        public void forwardEmail(String id, String to) {
198                super.waitForElement(By
199                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[3]"));
200                this.forwardEmail.click();
201                super.waitForElement(By.id("to_" + id));
202                driver.findElement(By.id("to_" + id)).sendKeys(to);
203                driver.findElement(By.id("send_button_" + id)).click();
204        }
205
206        private void reply(String id) {
207                super.waitForElement(By.id("to_" + id));
208                driver.findElement(By.id("send_button_" + id)).click();
209        }
210
211        /**
212         * Responde uma mensagem.
213         *
214         * @param id
215         *            Id da mensagem a ser respondida
216         */
217        public void replyEmail(String id) {
218                super.waitForElement(By
219                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[5]"));
220                this.replyEmail.click();
221                this.reply(id);
222        }
223
224        /**
225         * Responde uma mensagem.
226         *
227         * @param id
228         *            Id da mensagem a ser respondida
229         */
230        public void replyAllEmailWithoutHistory(String id) {
231                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
232                super.waitForElement(By
233                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[3]"));
234                this.replyAllEmailWithoutHistory.click();
235                this.reply(id);
236        }
237
238        /**
239         * Responde uma mensagem.
240         *
241         * @param id
242         *            Id da mensagem a ser respondida
243         */
244        public void replyEmailWithoutHistory(String id) {
245                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
246                super.waitForElement(By
247                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[2]"));
248                this.replyEmailWithoutHistory.click();
249                this.reply(id);
250        }
251
252        /**
253         * Responde uma mensagem.
254         *
255         * @param id
256         *            Id da mensagem a ser respondida
257         */
258        public void replyAllEmail(String id) {
259                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
260                super.waitForElement(By
261                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[1]"));
262                this.replyAllEmail.click();
263                this.reply(id);
264        }
265
266        /**
267         * Abre uma pasta de email especifica.
268         *
269         * @param folder
270         *            Pasta de email a ser aberta.
271         */
272        public void openFolder(String folder) {
273                clickElement(By.id(folder));
274                waitForElement(By.id("divScrollMain_0"));
275                waitForElement(By
276                                .xpath("//div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[1]/td[1]/input[1]"));
277        }
278
279        /**
280         * Abre uma pasta de email especifica.
281         *
282         * @param folder
283         *            Pasta de email a ser aberta.
284         */
285        public void openFolder(WebElement folder) {
286                folder.click();
287                waitForElement(By.id("divScrollMain_0"));
288        }
289
290        public void setImportantFilter() {
291                this.importantFilter.click();
292        }
293
294        /**
295         * Limpa a lixeira.
296         */
297        public void cleanupTrash() {
298                this.openFolder(this.trashFolder);
299                this.emptyTrashLink.click();
300        }
301
302        /**
303         * @return Numero de emails na pasta selecionada.
304         */
305        public int getTotalEmails() {
306                return Integer.valueOf(this.totalEmails.getText());
307        }
308
309        public void moveEmail(String folderName) {
310                this.moveEmailLink.click();
311                super.findAndClickElement("lINBOX/" + folderName + "wfolders_tree");
312                this.moveEmailButton.click();
313        }
314
315        public void editFolder() {
316                this.toolsMenu.click();
317                this.waitForElement(By
318                                .xpath("//div[@id='menu_tools']/table/tbody/tr[4]/td"));
319                this.editFolder.click();
320        }
321
322        public void createNewFolder(String folderName) {
323                this.editFolder();
324                this.createNewFolder.click();
325                Alert newFolder = driver.switchTo().alert();
326                assert "Insira o nome da nova pasta: ".equals(newFolder.getText());
327                newFolder.sendKeys(folderName);
328                newFolder.accept();
329        }
330
331        public void deleteFolder(String folderName) {
332                this.editFolder();
333                this.driver.findElement(By.id("lINBOX/" + folderName + "folders_tree"))
334                                .click();
335                this.deleteFolder.click();
336                Alert confirm = driver.switchTo().alert();
337                assert ("Você gostaria de excluir a pasta: " + folderName + "?")
338                                .equals(confirm.getText());
339                confirm.accept();
340        }
341
342        /**
343         * Marca email sendo composto como importante.
344         */
345        public void checkImportantFlag() {
346                this.important.click();
347        }
348
349        public void setImportantFlag() {
350                this.waitForElement(By
351                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"));
352                this.importantFlag.click();
353        }
354
355        public void setNormalFlag() {
356                this.waitForElement(By
357                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"));
358                this.normalFlag.click();
359        }
360
361        public void setNotReadFlag() {
362                this.waitForElement(By
363                                .xpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[2]"));
364                this.notReadFlag.click();
365        }
366
367        /**
368         * Marca solicitacao de confirmacao de leitura do email sendo composto.
369         */
370        public void checkReturnReceipt() {
371                this.returnReceipt.click();
372        }
373
374        /**
375         * Salva email sendo composto (devera ir para a pasta de Rascunhos).
376         *
377         * TODO adicionar teste para verificar se o email realmente esta presente na
378         * pasta Rascunhos??
379         */
380        public void saveEmail() {
381                this.saveEmail.click();
382        }
383
384        /**
385         * Apaga email previamente selecionado.
386         */
387        public void deleteEmail() {
388                this.deleteEmailLink.click();
389        }
390
391        public void selectAllEmails() {
392                this.selectAllEmails.click();
393        }
394
395        /**
396         * Adiciona email no campo de copia oculta.
397         *
398         * @param email
399         *            Email a ser adicionado.
400         */
401        public void setBlindCarbonCopy(String email) {
402                this.blindCarbonCopyLink.click();
403                this.blindCarbonCopy.sendKeys(email);
404        }
405
406        /**
407         * Adiciona email no campo de copia.
408         *
409         * @param email
410         *            Email a ser adicionado.
411         */
412        public void setCarbonCopy(String email) {
413                this.carbonCopyLink.click();
414                this.carbonCopy.sendKeys(email);
415        }
416
417        /**
418         * Anexo um arquivo no email sendo composto.
419         *
420         * @param attachment
421         *            Caminho para o arquivo a ser anexado.
422         */
423        public void addAttachment(String attachment) {
424                this.attachmentLink.click();
425                this.attachment.sendKeys(attachment);
426        }
427
428        public void addAttachment2(String attachment) {
429                this.attachmentLink.click();
430                this.attachment2.sendKeys(attachment);
431        }
432
433        /**
434         * Envia email sendo composto.
435         */
436        public void sendMail() {
437                this.send.click();
438        }
439
440        /**
441         * Fecha aba do email sendo composto e confirma que email nao esta salvo.
442         */
443        public void closeMail() {
444                this.closeMail(true);
445        }
446
447        public void closeMail(boolean checkAlert) {
448                super.clickElement(By.xpath("//td[@id='border_id_1']/div/div[2]/img"));
449                if (checkAlert) {
450                        Alert closeAlert = driver.switchTo().alert();
451                        closeAlert.accept();
452                }
453        }
454
455        public void sendMail(String id) {
456                super.findAndClickElement("send_button_" + id);
457        }
458
459        /**
460         * @param to
461         * @param subject
462         */
463        public void composeEmail(String to, String subject) {
464                composeEmail(to, subject, null);
465        }
466
467        /**
468         * @param to
469         */
470        public void composeEmail(String to) {
471                composeEmail(to, null, null);
472        }
473
474        /**
475         * Procura um email pelo assunto.
476         *
477         * @param subject
478         *            Assunto a ser procurado.
479         * @param select
480         *            Se email encontrado, deseja seleciona-lo?
481         * @return "id" do email.
482         */
483        public String findEmailIdBySuject(String subject, boolean select) {
484                String html = driver.getPageSource();
485                String part1 = StringUtils.substringBefore(html, subject);
486                int start = StringUtils.lastIndexOf(part1, "id=\"") + 4;
487                int last = StringUtils.indexOf(part1, "\"", start);
488                String id = StringUtils.substring(part1, start, last);
489
490                if (select) {
491                        // Marca checkbox correspondente ao id da mensagem.
492                        clickElement(By.id("check_box" + id.substring(1)));
493                }
494                return id;
495        }
496
497        /**
498         * @return Ids de todos os emails da pasta selecionada.
499         */
500        public String[] getEmailIds() {
501                String[] ids = new String[this.getTotalEmails()];
502                for (int i = 1; i <= this.getTotalEmails(); i++) {
503                        String id = driver
504                                        .findElement(
505                                                        By.xpath("//div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr["
506                                                                        + i + "]/td[1]/input[1]")).getAttribute(
507                                                        "id");
508                        ids[i - 1] = id.substring(id.lastIndexOf("_") + 1);
509                }
510                return ids;
511        }
512
513        /**
514         * @param emailId
515         *            Id do email.
516         * @return <code>true</code> se email importante, <code>false</code> caso
517         *         contrario.
518         */
519        public boolean isImportant(String emailId) {
520                return super
521                                .isDisplayed((RenderedWebElement) driver.findElement(By
522                                                .xpath("//div[@id='exmail_main_body']/div[@id='content_id_0']/div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[@id='"
523                                                                + emailId + "']/td[5]/img")));
524        }
525
526        public String findEmailIdBySuject(String subject) {
527                return findEmailIdBySuject(subject, false);
528        }
529
530        /**
531         * Verifica se mensagem informada corresponde ao exibido pelo Expresso na
532         * div onde sao exibidas mensagens ao usuario.
533         *
534         * @param message
535         *            Mensagem a ser validada.
536         */
537        public void assertMessage(String message) {
538                assert message.equals(getDisplayedElementContent("em_div_write_msg"));
539        }
540}
Note: See TracBrowser for help on using the repository browser.