|
1 | 1 | package org.jboss.as.quickstarts.mail; |
2 | 2 |
|
3 | | -import io.github.bonigarcia.wdm.WebDriverManager; |
4 | | -import org.junit.After; |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.htmlunit.WebClient; |
| 6 | +import org.htmlunit.html.HtmlElement; |
| 7 | +import org.htmlunit.html.HtmlInput; |
| 8 | +import org.htmlunit.html.HtmlPage; |
| 9 | +import org.htmlunit.html.HtmlSubmitInput; |
| 10 | +import org.htmlunit.html.HtmlTextArea; |
5 | 11 | import org.junit.Assert; |
6 | 12 | import org.junit.Before; |
7 | | -import org.junit.FixMethodOrder; |
8 | 13 | import org.junit.Test; |
9 | | -import org.junit.runners.MethodSorters; |
10 | | -import org.openqa.selenium.By; |
11 | | -import org.openqa.selenium.StaleElementReferenceException; |
12 | | -import org.openqa.selenium.WebDriver; |
13 | | -import org.openqa.selenium.WebElement; |
14 | | -import org.openqa.selenium.chrome.ChromeDriver; |
15 | | -import org.openqa.selenium.chrome.ChromeOptions; |
16 | | -import org.openqa.selenium.support.ui.Wait; |
17 | | -import org.openqa.selenium.support.ui.WebDriverWait; |
18 | | - |
19 | | -import java.time.Duration; |
20 | | - |
21 | | -@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 14 | + |
22 | 15 | public class MailTestCaseIT { |
23 | 16 |
|
24 | 17 | private static final String DEFAULT_SERVER_HOST = "http://localhost:8080"; |
25 | 18 |
|
26 | | - private WebDriver driver; |
| 19 | + private String serverHost; |
27 | 20 |
|
28 | 21 | @Before |
29 | 22 | public void testSetup() { |
30 | | - WebDriverManager.chromedriver().setup(); |
31 | | - |
32 | | - ChromeOptions options = new ChromeOptions(); |
33 | | - options.addArguments("--no-sandbox"); |
34 | | - options.addArguments("--disable-dev-shm-usage"); |
35 | | - options.addArguments("--headless"); |
36 | | - |
37 | | - driver = new ChromeDriver(options); |
38 | | - driver.manage().window().maximize(); |
39 | | - |
40 | | - String serverHost = System.getenv("SERVER_HOST"); |
| 23 | + serverHost = System.getenv("SERVER_HOST"); |
41 | 24 | if (serverHost == null) { |
42 | 25 | serverHost = System.getProperty("server.host"); |
43 | 26 | } |
44 | 27 | if (serverHost == null) { |
45 | 28 | serverHost = DEFAULT_SERVER_HOST; |
46 | 29 | } |
47 | | - |
48 | | - driver.get(serverHost+"/mail"); |
49 | | - driver.manage().timeouts().implicitlyWait(Duration.ofMillis(800)); |
50 | 30 | } |
51 | 31 |
|
52 | | - @After |
53 | | - public void cleanUp() { |
54 | | - if (driver != null) { |
55 | | - driver.close(); |
| 32 | + @Test |
| 33 | + public void testSendAndRetrieveEmail() throws IOException, InterruptedException { |
| 34 | + try (final WebClient webClient = new WebClient()) { |
| 35 | + // Get the home page |
| 36 | + HtmlPage mailHomePage = webClient.getPage(serverHost + "/mail"); |
| 37 | + // Send an email |
| 38 | + sendEmailBySMTP(webClient, mailHomePage); |
| 39 | + // give to mail server extra time to save mail to a mailbox |
| 40 | + Thread.sleep(2000); |
| 41 | + // retrieve an email by POP3 and IMAP |
| 42 | + retrieveEmailByPOP3(webClient, mailHomePage); |
| 43 | + retrieveEmailByIMAP(webClient, mailHomePage); |
56 | 44 | } |
57 | 45 | } |
58 | 46 |
|
59 | | - @Test |
60 | | - public void a_testSMTP() { |
61 | | - Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(20)); |
62 | | - |
63 | | - WebElement from = driver.findElement(By.id("smtp_from")); |
64 | | - WebElement to = driver.findElement(By.id("smtp_to")); |
65 | | - WebElement subject = driver.findElement(By.id("smtp_subject")); |
66 | | - WebElement body = driver.findElement(By.id("smtp_body")); |
67 | | - |
68 | | - from.clear(); |
69 | | - from.sendKeys("user01@mail.local"); |
| 47 | + private void sendEmailBySMTP(WebClient webClient, HtmlPage mailHomePage) throws IOException { |
| 48 | + HtmlInput from = mailHomePage.getHtmlElementById("smtp_from"); |
| 49 | + HtmlInput to = mailHomePage.getHtmlElementById("smtp_to"); |
| 50 | + HtmlInput subject = mailHomePage.getHtmlElementById("smtp_subject"); |
| 51 | + HtmlTextArea body = mailHomePage.getHtmlElementById("smtp_body"); |
| 52 | + HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("smtp_send_btn"); |
70 | 53 |
|
71 | | - to.clear(); |
72 | | - to.sendKeys("user02@mail.local"); |
| 54 | + from.setValue("user01@mail.local"); |
| 55 | + to.setValue("user02@mail.local"); |
| 56 | + subject.setValue("This is a test"); |
| 57 | + body.setText("Hello user02, I've sent an email."); |
73 | 58 |
|
74 | | - subject.clear(); |
75 | | - subject.sendKeys("This is a test"); |
76 | | - |
77 | | - body.clear(); |
78 | | - body.sendKeys("Hello user02, I've sent an email."); |
79 | | - |
80 | | - WebElement submitButton = driver.findElement(By.id("smtp_send_btn")); |
81 | 59 | submitButton.click(); |
| 60 | + /* will wait JavaScript to execute up to 30s */ |
| 61 | + webClient.waitForBackgroundJavaScript(30 * 1000); |
82 | 62 |
|
83 | | - wait.until(d -> driver.findElement(By.xpath("//ul[@id='smtp_messages']/li")).isDisplayed()); |
84 | | - |
85 | | - Assert.assertEquals( |
86 | | - "Unexpected result messages after sending an email via SMTP.", |
87 | | - "Email sent to user02@mail.local", |
88 | | - driver.findElement(By.xpath("//ul[@id='smtp_messages']/li")).getText()); |
| 63 | + HtmlElement message = mailHomePage.getFirstByXPath("//ul[@id='smtp_messages']/li"); |
| 64 | + Assert.assertEquals("Unexpected result messages after sending an email via SMTP.", "Email sent to user02@mail.local", message.asNormalizedText()); |
89 | 65 | } |
90 | 66 |
|
91 | | - @Test |
92 | | - public void b_retrievePOP3() { |
93 | | - Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(20)); |
94 | | - |
95 | | - WebElement user = driver.findElement(By.id("pop3_user")); |
96 | | - WebElement password = driver.findElement(By.id("pop3_password")); |
97 | | - |
98 | | - user.clear(); |
99 | | - user.sendKeys("user02@mail.local"); |
| 67 | + private void retrieveEmailByPOP3(WebClient webClient, HtmlPage mailHomePage) throws IOException { |
| 68 | + HtmlInput user = mailHomePage.getHtmlElementById("pop3_user"); |
| 69 | + HtmlInput password = mailHomePage.getHtmlElementById("pop3_password"); |
| 70 | + HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("pop3_get_emails_btn"); |
100 | 71 |
|
101 | | - password.clear(); |
102 | | - password.sendKeys("1234"); |
103 | | - |
104 | | - WebElement submitButton = driver.findElement(By.id("pop3_get_emails_btn")); |
| 72 | + user.setValue("user02@mail.local"); |
| 73 | + password.setValue("1234"); |
105 | 74 | submitButton.click(); |
| 75 | + /* will wait JavaScript to execute up to 30s */ |
| 76 | + webClient.waitForBackgroundJavaScript(30 * 1000); |
| 77 | + HtmlTextArea emails = mailHomePage.getHtmlElementById("pop3_emails"); |
106 | 78 |
|
107 | | - wait.until(d -> { |
108 | | - try { |
109 | | - WebElement emails = driver.findElement(By.id("pop3_emails")); |
110 | | - return !emails.getText().isEmpty(); |
111 | | - } catch (StaleElementReferenceException sere) { |
112 | | - return false; |
113 | | - } |
114 | | - }); |
115 | | - |
116 | | - WebElement emails = driver.findElement(By.id("pop3_emails")); |
117 | 79 | Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : user01@mail.local")); |
118 | 80 | Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test")); |
119 | 81 | Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email.")); |
120 | 82 | } |
121 | 83 |
|
122 | | - |
123 | | - @Test |
124 | | - public void c_retrieveIMAP() { |
125 | | - Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(20)); |
126 | | - |
127 | | - WebElement submitButton = driver.findElement(By.id("imap_get_emails_btn")); |
| 84 | + private void retrieveEmailByIMAP(WebClient webClient, HtmlPage mailHomePage) throws IOException { |
| 85 | + HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("imap_get_emails_btn"); |
128 | 86 | submitButton.click(); |
| 87 | + /* will wait JavaScript to execute up to 30s */ |
| 88 | + webClient.waitForBackgroundJavaScript(30 * 1000); |
| 89 | + HtmlTextArea emails = mailHomePage.getHtmlElementById("imap_emails"); |
129 | 90 |
|
130 | | - wait.until(d -> { |
131 | | - try { |
132 | | - WebElement emails = driver.findElement(By.id("imap_emails")); |
133 | | - return !emails.getText().isEmpty(); |
134 | | - } catch (StaleElementReferenceException sere) { |
135 | | - return false; |
136 | | - } |
137 | | - }); |
138 | | - |
139 | | - WebElement emails = driver.findElement(By.id("imap_emails")); |
140 | 91 | Assert.assertNotNull("IMAP No messages found.", emails.getText()); |
141 | | - Assert.assertTrue("Expected email not found.", emails.getText().contains("From : user01@mail.local")); |
142 | | - Assert.assertTrue("Expected email not found.", emails.getText().contains("Subject : This is a test")); |
143 | | - Assert.assertTrue("Expected email not found.", emails.getText().contains("Body : Hello user02, I've sent an email.")); |
| 92 | + Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : user01@mail.local")); |
| 93 | + Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test")); |
| 94 | + Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email.")); |
144 | 95 | } |
145 | 96 | } |
0 commit comments