1 | package br.gov.serpro.expresso.cte.pages.email; |
---|
2 | |
---|
3 | import org.apache.commons.lang.StringUtils; |
---|
4 | import org.openqa.selenium.Alert; |
---|
5 | import org.openqa.selenium.By; |
---|
6 | import org.openqa.selenium.JavascriptExecutor; |
---|
7 | import org.openqa.selenium.RenderedWebElement; |
---|
8 | import org.openqa.selenium.WebDriver; |
---|
9 | import org.openqa.selenium.WebElement; |
---|
10 | import org.openqa.selenium.support.FindBy; |
---|
11 | |
---|
12 | import 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 | */ |
---|
20 | public 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 | /** |
---|
123 | * @param driver |
---|
124 | */ |
---|
125 | public MailPage(WebDriver driver) { |
---|
126 | super(driver); |
---|
127 | } |
---|
128 | |
---|
129 | public void createNewEmail() { |
---|
130 | this.newEmail.click(); |
---|
131 | waitForElementById("to_1"); |
---|
132 | } |
---|
133 | |
---|
134 | public void setSubject(String subject) { |
---|
135 | this.subject.sendKeys(subject != null ? subject : ""); |
---|
136 | } |
---|
137 | |
---|
138 | public void setBody(String body) { |
---|
139 | // Foi o unico jeito que consegui pegar o richtexteditor... |
---|
140 | if (body != null) { |
---|
141 | ((JavascriptExecutor) driver) |
---|
142 | .executeScript("document.getElementById('body_1').contentWindow.document.body.innerHTML='" |
---|
143 | + body + "';"); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * @param to |
---|
149 | * @param subject |
---|
150 | * @param body |
---|
151 | */ |
---|
152 | public void composeEmail(String to, String subject, String body) { |
---|
153 | this.createNewEmail(); |
---|
154 | this.to.sendKeys(to); |
---|
155 | this.setSubject(subject); |
---|
156 | this.setBody(body); |
---|
157 | } |
---|
158 | |
---|
159 | public String readEmail(String subject) { |
---|
160 | String id = findEmailIdBySuject(subject); |
---|
161 | clickElement(id); |
---|
162 | return id.substring(id.lastIndexOf("_") + 1); |
---|
163 | // waitForElementByXpath("//div[@id='exmail_main_body']/table[1]/tbody[@id='border_tbody']/tr[1]/td[2]/table/tbody/tr/td[1]"); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Encaminha uma mensagem. |
---|
168 | * |
---|
169 | * @param id |
---|
170 | * Id da mensagem a ser encaminhada |
---|
171 | * @param to |
---|
172 | * Destinatario |
---|
173 | */ |
---|
174 | public void forwardEmail(String id, String to) { |
---|
175 | super.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[3]/span[3]"); |
---|
176 | this.forwardEmail.click(); |
---|
177 | super.waitForElementById("to_" + id); |
---|
178 | driver.findElement(By.id("to_" + id)).sendKeys(to); |
---|
179 | driver.findElement(By.id("send_button_" + id)).click(); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Abre uma pasta de email especifica. |
---|
184 | * |
---|
185 | * @param folder |
---|
186 | * Pasta de email a ser aberta. |
---|
187 | */ |
---|
188 | public void openFolder(String folder) { |
---|
189 | clickElement(folder); |
---|
190 | waitForElementById("divScrollMain_0"); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Abre uma pasta de email especifica. |
---|
195 | * |
---|
196 | * @param folder |
---|
197 | * Pasta de email a ser aberta. |
---|
198 | */ |
---|
199 | public void openFolder(WebElement folder) { |
---|
200 | folder.click(); |
---|
201 | waitForElementById("divScrollMain_0"); |
---|
202 | } |
---|
203 | |
---|
204 | public void setImportantFilter() { |
---|
205 | this.importantFilter.click(); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Limpa a lixeira. |
---|
210 | */ |
---|
211 | public void cleanupTrash() { |
---|
212 | this.openFolder(this.trashFolder); |
---|
213 | this.emptyTrashLink.click(); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * @return Numero de emails na pasta selecionada. |
---|
218 | */ |
---|
219 | public int getTotalEmails() { |
---|
220 | return Integer.valueOf(this.totalEmails.getText()); |
---|
221 | } |
---|
222 | |
---|
223 | public void moveEmail(String folderName) { |
---|
224 | this.moveEmailLink.click(); |
---|
225 | super.findAndClickElement("lINBOX/" + folderName + "wfolders_tree"); |
---|
226 | this.moveEmailButton.click(); |
---|
227 | } |
---|
228 | |
---|
229 | public void editFolder() { |
---|
230 | this.toolsMenu.click(); |
---|
231 | this.waitForElementByXpath("//div[@id='menu_tools']/table/tbody/tr[4]/td"); |
---|
232 | this.editFolder.click(); |
---|
233 | } |
---|
234 | |
---|
235 | public void createNewFolder(String folderName) { |
---|
236 | this.editFolder(); |
---|
237 | this.createNewFolder.click(); |
---|
238 | Alert newFolder = driver.switchTo().alert(); |
---|
239 | assert "Insira o nome da nova pasta: ".equals(newFolder.getText()); |
---|
240 | newFolder.sendKeys(folderName); |
---|
241 | newFolder.accept(); |
---|
242 | } |
---|
243 | |
---|
244 | public void deleteFolder(String folderName) { |
---|
245 | this.editFolder(); |
---|
246 | this.driver.findElement(By.id("lINBOX/" + folderName + "folders_tree")) |
---|
247 | .click(); |
---|
248 | this.deleteFolder.click(); |
---|
249 | Alert confirm = driver.switchTo().alert(); |
---|
250 | assert ("Você gostaria de excluir a pasta: " + folderName + "?") |
---|
251 | .equals(confirm.getText()); |
---|
252 | confirm.accept(); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Marca email sendo composto como importante. |
---|
257 | */ |
---|
258 | public void checkImportantFlag() { |
---|
259 | this.important.click(); |
---|
260 | } |
---|
261 | |
---|
262 | public void setImportantFlag() { |
---|
263 | this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"); |
---|
264 | this.importantFlag.click(); |
---|
265 | } |
---|
266 | |
---|
267 | public void setNormalFlag() { |
---|
268 | this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[3]"); |
---|
269 | this.normalFlag.click(); |
---|
270 | } |
---|
271 | |
---|
272 | public void setNotReadFlag() { |
---|
273 | this.waitForElementByXpath("//div[@id='exmail_main_body']/div[2]/table/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/span[2]"); |
---|
274 | this.notReadFlag.click(); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * Marca solicitacao de confirmacao de leitura do email sendo composto. |
---|
279 | */ |
---|
280 | public void checkReturnReceipt() { |
---|
281 | this.returnReceipt.click(); |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Salva email sendo composto (devera ir para a pasta de Rascunhos). |
---|
286 | * |
---|
287 | * TODO adicionar teste para verificar se o email realmente esta presente na |
---|
288 | * pasta Rascunhos?? |
---|
289 | */ |
---|
290 | public void saveEmail() { |
---|
291 | this.saveEmail.click(); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * Apaga email previamente selecionado. |
---|
296 | */ |
---|
297 | public void deleteEmail() { |
---|
298 | this.deleteEmailLink.click(); |
---|
299 | } |
---|
300 | |
---|
301 | public void selectAllEmails() { |
---|
302 | this.selectAllEmails.click(); |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Adiciona email no campo de copia oculta. |
---|
307 | * |
---|
308 | * @param email |
---|
309 | * Email a ser adicionado. |
---|
310 | */ |
---|
311 | public void setBlindCarbonCopy(String email) { |
---|
312 | this.blindCarbonCopyLink.click(); |
---|
313 | this.blindCarbonCopy.sendKeys(email); |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * Adiciona email no campo de copia. |
---|
318 | * |
---|
319 | * @param email |
---|
320 | * Email a ser adicionado. |
---|
321 | */ |
---|
322 | public void setCarbonCopy(String email) { |
---|
323 | this.carbonCopyLink.click(); |
---|
324 | this.carbonCopy.sendKeys(email); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * Anexo um arquivo no email sendo composto. |
---|
329 | * |
---|
330 | * @param attachment |
---|
331 | * Caminho para o arquivo a ser anexado. |
---|
332 | */ |
---|
333 | public void addAttachment(String attachment) { |
---|
334 | this.attachmentLink.click(); |
---|
335 | this.attachment.sendKeys(attachment); |
---|
336 | } |
---|
337 | |
---|
338 | public void addAttachment2(String attachment) { |
---|
339 | this.attachmentLink.click(); |
---|
340 | this.attachment2.sendKeys(attachment); |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | * Envia email sendo composto. |
---|
345 | */ |
---|
346 | public void sendMail() { |
---|
347 | this.send.click(); |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * @param to |
---|
352 | * @param subject |
---|
353 | */ |
---|
354 | public void composeEmail(String to, String subject) { |
---|
355 | composeEmail(to, subject, null); |
---|
356 | } |
---|
357 | |
---|
358 | /** |
---|
359 | * @param to |
---|
360 | */ |
---|
361 | public void composeEmail(String to) { |
---|
362 | composeEmail(to, null, null); |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Procura um email pelo assunto. |
---|
367 | * |
---|
368 | * @param subject |
---|
369 | * Assunto a ser procurado. |
---|
370 | * @param select |
---|
371 | * Se email encontrado, deseja seleciona-lo? |
---|
372 | * @return "id" do email. |
---|
373 | */ |
---|
374 | public String findEmailIdBySuject(String subject, boolean select) { |
---|
375 | String html = driver.getPageSource(); |
---|
376 | String part1 = StringUtils.substringBefore(html, subject); |
---|
377 | int start = StringUtils.lastIndexOf(part1, "id=\"") + 4; |
---|
378 | int last = StringUtils.indexOf(part1, "\"", start); |
---|
379 | String id = StringUtils.substring(part1, start, last); |
---|
380 | |
---|
381 | if (select) { |
---|
382 | // Marca checkbox correspondente ao id da mensagem. |
---|
383 | clickElement("check_box" + id.substring(1)); |
---|
384 | } |
---|
385 | return id; |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * @return Ids de todos os emails da pasta selecionada. |
---|
390 | */ |
---|
391 | public String[] getEmailIds() { |
---|
392 | String[] ids = new String[this.getTotalEmails()]; |
---|
393 | for (int i = 1; i <= this.getTotalEmails(); i++) { |
---|
394 | String id = driver |
---|
395 | .findElement( |
---|
396 | By.xpath("//div[@id='divScrollMain_0']/table[@id='table_box']/tbody[@id='tbody_box']/tr[" |
---|
397 | + i + "]/td[1]/input[1]")).getAttribute( |
---|
398 | "id"); |
---|
399 | ids[i - 1] = id.substring(id.lastIndexOf("_") + 1); |
---|
400 | } |
---|
401 | return ids; |
---|
402 | } |
---|
403 | |
---|
404 | /** |
---|
405 | * @param emailId |
---|
406 | * Id do email. |
---|
407 | * @return <code>true</code> se email importante, <code>false</code> caso |
---|
408 | * contrario. |
---|
409 | */ |
---|
410 | public boolean isImportant(String emailId) { |
---|
411 | return super |
---|
412 | .isDisplayed((RenderedWebElement) driver.findElement(By |
---|
413 | .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='" |
---|
414 | + emailId + "']/td[5]/img"))); |
---|
415 | } |
---|
416 | |
---|
417 | public String findEmailIdBySuject(String subject) { |
---|
418 | return findEmailIdBySuject(subject, false); |
---|
419 | } |
---|
420 | |
---|
421 | /** |
---|
422 | * Verifica se mensagem informada corresponde ao exibido pelo Expresso na |
---|
423 | * div onde sao exibidas mensagens ao usuario. |
---|
424 | * |
---|
425 | * @param message |
---|
426 | * Mensagem a ser validada. |
---|
427 | */ |
---|
428 | public void assertMessage(String message) { |
---|
429 | assert message.equals(getDisplayedElementContent("em_div_write_msg")); |
---|
430 | } |
---|
431 | } |
---|