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

Revision 3745, 13.8 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                waitForElementById("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                String id = findEmailIdBySuject(subject);
176                clickElement(id);
177                return id.substring(id.lastIndexOf("_") + 1);
178                // waitForElementByXpath("//div[@id='exmail_main_body']/table[1]/tbody[@id='border_tbody']/tr[1]/td[2]/table/tbody/tr/td[1]");
179        }
180
181        public void editEmail() {
182                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr/td[2]/span[2]/span[1]");
183                this.editEmail.click();
184        }
185
186        /**
187         * Encaminha uma mensagem.
188         *
189         * @param id
190         *            Id da mensagem a ser encaminhada
191         * @param to
192         *            Destinatario
193         */
194        public void forwardEmail(String id, String to) {
195                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[3]");
196                this.forwardEmail.click();
197                super.waitForElementById("to_" + id);
198                driver.findElement(By.id("to_" + id)).sendKeys(to);
199                driver.findElement(By.id("send_button_" + id)).click();
200        }
201
202        private void reply(String id) {
203                super.waitForElementById("to_" + id);
204                driver.findElement(By.id("send_button_" + id)).click();
205        }
206
207        /**
208         * Responde uma mensagem.
209         *
210         * @param id
211         *            Id da mensagem a ser respondida
212         */
213        public void replyEmail(String id) {
214                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[5]");
215                this.replyEmail.click();
216                this.reply(id);
217        }
218
219        /**
220         * Responde uma mensagem.
221         *
222         * @param id
223         *            Id da mensagem a ser respondida
224         */
225        public void replyAllEmailWithoutHistory(String id) {
226                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
227                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[3]");
228                this.replyAllEmailWithoutHistory.click();
229                this.reply(id);
230        }
231
232        /**
233         * Responde uma mensagem.
234         *
235         * @param id
236         *            Id da mensagem a ser respondida
237         */
238        public void replyEmailWithoutHistory(String id) {
239                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
240                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[2]");
241                this.replyEmailWithoutHistory.click();
242                this.reply(id);
243        }
244
245        /**
246         * Responde uma mensagem.
247         *
248         * @param id
249         *            Id da mensagem a ser respondida
250         */
251        public void replyAllEmail(String id) {
252                super.findAndClickElement("msg_opt_reply_options_" + id + "_r");
253                super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span[1]");
254                this.replyAllEmail.click();
255                this.reply(id);
256        }
257
258        /**
259         * Abre uma pasta de email especifica.
260         *
261         * @param folder
262         *            Pasta de email a ser aberta.
263         */
264        public void openFolder(String folder) {
265                clickElement(folder);
266                waitForElementById("divScrollMain_0");
267        }
268
269        /**
270         * Abre uma pasta de email especifica.
271         *
272         * @param folder
273         *            Pasta de email a ser aberta.
274         */
275        public void openFolder(WebElement folder) {
276                folder.click();
277                waitForElementById("divScrollMain_0");
278        }
279
280        public void setImportantFilter() {
281                this.importantFilter.click();
282        }
283
284        /**
285         * Limpa a lixeira.
286         */
287        public void cleanupTrash() {
288                this.openFolder(this.trashFolder);
289                this.emptyTrashLink.click();
290        }
291
292        /**
293         * @return Numero de emails na pasta selecionada.
294         */
295        public int getTotalEmails() {
296                return Integer.valueOf(this.totalEmails.getText());
297        }
298
299        public void moveEmail(String folderName) {
300                this.moveEmailLink.click();
301                super.findAndClickElement("lINBOX/" + folderName + "wfolders_tree");
302                this.moveEmailButton.click();
303        }
304
305        public void editFolder() {
306                this.toolsMenu.click();
307                this.waitForElementByXpath("//div[@id='menu_tools']/table/tbody/tr[4]/td");
308                this.editFolder.click();
309        }
310
311        public void createNewFolder(String folderName) {
312                this.editFolder();
313                this.createNewFolder.click();
314                Alert newFolder = driver.switchTo().alert();
315                assert "Insira o nome da nova pasta: ".equals(newFolder.getText());
316                newFolder.sendKeys(folderName);
317                newFolder.accept();
318        }
319
320        public void deleteFolder(String folderName) {
321                this.editFolder();
322                this.driver.findElement(By.id("lINBOX/" + folderName + "folders_tree"))
323                                .click();
324                this.deleteFolder.click();
325                Alert confirm = driver.switchTo().alert();
326                assert ("Você gostaria de excluir a pasta: " + folderName + "?")
327                                .equals(confirm.getText());
328                confirm.accept();
329        }
330
331        /**
332         * Marca email sendo composto como importante.
333         */
334        public void checkImportantFlag() {
335                this.important.click();
336        }
337
338        public void setImportantFlag() {
339                this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]");
340                this.importantFlag.click();
341        }
342
343        public void setNormalFlag() {
344                this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]");
345                this.normalFlag.click();
346        }
347
348        public void setNotReadFlag() {
349                this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[2]");
350                this.notReadFlag.click();
351        }
352
353        /**
354         * Marca solicitacao de confirmacao de leitura do email sendo composto.
355         */
356        public void checkReturnReceipt() {
357                this.returnReceipt.click();
358        }
359
360        /**
361         * Salva email sendo composto (devera ir para a pasta de Rascunhos).
362         *
363         * TODO adicionar teste para verificar se o email realmente esta presente na
364         * pasta Rascunhos??
365         */
366        public void saveEmail() {
367                this.saveEmail.click();
368        }
369
370        /**
371         * Apaga email previamente selecionado.
372         */
373        public void deleteEmail() {
374                this.deleteEmailLink.click();
375        }
376
377        public void selectAllEmails() {
378                this.selectAllEmails.click();
379        }
380
381        /**
382         * Adiciona email no campo de copia oculta.
383         *
384         * @param email
385         *            Email a ser adicionado.
386         */
387        public void setBlindCarbonCopy(String email) {
388                this.blindCarbonCopyLink.click();
389                this.blindCarbonCopy.sendKeys(email);
390        }
391
392        /**
393         * Adiciona email no campo de copia.
394         *
395         * @param email
396         *            Email a ser adicionado.
397         */
398        public void setCarbonCopy(String email) {
399                this.carbonCopyLink.click();
400                this.carbonCopy.sendKeys(email);
401        }
402
403        /**
404         * Anexo um arquivo no email sendo composto.
405         *
406         * @param attachment
407         *            Caminho para o arquivo a ser anexado.
408         */
409        public void addAttachment(String attachment) {
410                this.attachmentLink.click();
411                this.attachment.sendKeys(attachment);
412        }
413
414        public void addAttachment2(String attachment) {
415                this.attachmentLink.click();
416                this.attachment2.sendKeys(attachment);
417        }
418
419        /**
420         * Envia email sendo composto.
421         */
422        public void sendMail() {
423                this.send.click();
424        }
425
426        public void sendMail(String id) {
427                super.findAndClickElement("send_button_" + id);
428        }
429
430        /**
431         * @param to
432         * @param subject
433         */
434        public void composeEmail(String to, String subject) {
435                composeEmail(to, subject, null);
436        }
437
438        /**
439         * @param to
440         */
441        public void composeEmail(String to) {
442                composeEmail(to, null, null);
443        }
444
445        /**
446         * Procura um email pelo assunto.
447         *
448         * @param subject
449         *            Assunto a ser procurado.
450         * @param select
451         *            Se email encontrado, deseja seleciona-lo?
452         * @return "id" do email.
453         */
454        public String findEmailIdBySuject(String subject, boolean select) {
455                String html = driver.getPageSource();
456                String part1 = StringUtils.substringBefore(html, subject);
457                int start = StringUtils.lastIndexOf(part1, "id=\"") + 4;
458                int last = StringUtils.indexOf(part1, "\"", start);
459                String id = StringUtils.substring(part1, start, last);
460
461                if (select) {
462                        // Marca checkbox correspondente ao id da mensagem.
463                        clickElement("check_box" + id.substring(1));
464                }
465                return id;
466        }
467
468        /**
469         * @return Ids de todos os emails da pasta selecionada.
470         */
471        public String[] getEmailIds() {
472                String[] ids = new String[this.getTotalEmails()];
473                for (int i = 1; i <= this.getTotalEmails(); i++) {
474                        String id = driver
475                                        .findElement(
476                                                        By.xpath("//div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr["
477                                                                        + i + "]/td[1]/input[1]")).getAttribute(
478                                                        "id");
479                        ids[i - 1] = id.substring(id.lastIndexOf("_") + 1);
480                }
481                return ids;
482        }
483
484        /**
485         * @param emailId
486         *            Id do email.
487         * @return <code>true</code> se email importante, <code>false</code> caso
488         *         contrario.
489         */
490        public boolean isImportant(String emailId) {
491                return super
492                                .isDisplayed((RenderedWebElement) driver.findElement(By
493                                                .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='"
494                                                                + emailId + "']/td[5]/img")));
495        }
496
497        public String findEmailIdBySuject(String subject) {
498                return findEmailIdBySuject(subject, false);
499        }
500
501        /**
502         * Verifica se mensagem informada corresponde ao exibido pelo Expresso na
503         * div onde sao exibidas mensagens ao usuario.
504         *
505         * @param message
506         *            Mensagem a ser validada.
507         */
508        public void assertMessage(String message) {
509                assert message.equals(getDisplayedElementContent("em_div_write_msg"));
510        }
511}
Note: See TracBrowser for help on using the repository browser.