Skip to content

Commit ade9b56

Browse files
authored
Merge pull request #1009 from kstekovi/WFLY-20129
[WFLY-20129] Mail QS tests - Use HtmlUnit instead of Selenium
2 parents fa707e4 + d75344d commit ade9b56

2 files changed

Lines changed: 59 additions & 116 deletions

File tree

mail/pom.xml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@
5151
<!-- the versions for BOMs, Packs and Plugins -->
5252
<version.bom.ee>${version.server}</version.bom.ee>
5353
<version.plugin.wildfly>5.1.3.Final</version.plugin.wildfly>
54-
<!-- the versions for other Dependencies and Plugins -->
55-
<version.org.seleniumhq.selenium>4.15.0</version.org.seleniumhq.selenium>
56-
<version.webdrivermanager>5.7.0</version.webdrivermanager>
54+
<!-- the version for HtmlUnit -->
55+
<version.org.htmlunit>4.15.0</version.org.htmlunit>
5756
</properties>
5857

5958
<dependencyManagement>
@@ -100,16 +99,9 @@
10099
<scope>test</scope>
101100
</dependency>
102101
<dependency>
103-
<groupId>org.seleniumhq.selenium</groupId>
104-
<artifactId>selenium-java</artifactId>
105-
<version>${version.org.seleniumhq.selenium}</version>
106-
<scope>test</scope>
107-
</dependency>
108-
109-
<dependency>
110-
<groupId>io.github.bonigarcia</groupId>
111-
<artifactId>webdrivermanager</artifactId>
112-
<version>${version.webdrivermanager}</version>
102+
<groupId>org.htmlunit</groupId>
103+
<artifactId>htmlunit</artifactId>
104+
<version>${version.org.htmlunit}</version>
113105
<scope>test</scope>
114106
</dependency>
115107
</dependencies>
Lines changed: 54 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,96 @@
11
package org.jboss.as.quickstarts.mail;
22

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;
511
import org.junit.Assert;
612
import org.junit.Before;
7-
import org.junit.FixMethodOrder;
813
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+
2215
public class MailTestCaseIT {
2316

2417
private static final String DEFAULT_SERVER_HOST = "http://localhost:8080";
2518

26-
private WebDriver driver;
19+
private String serverHost;
2720

2821
@Before
2922
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");
4124
if (serverHost == null) {
4225
serverHost = System.getProperty("server.host");
4326
}
4427
if (serverHost == null) {
4528
serverHost = DEFAULT_SERVER_HOST;
4629
}
47-
48-
driver.get(serverHost+"/mail");
49-
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(800));
5030
}
5131

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);
5644
}
5745
}
5846

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");
7053

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.");
7358

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"));
8159
submitButton.click();
60+
/* will wait JavaScript to execute up to 30s */
61+
webClient.waitForBackgroundJavaScript(30 * 1000);
8262

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());
8965
}
9066

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");
10071

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");
10574
submitButton.click();
75+
/* will wait JavaScript to execute up to 30s */
76+
webClient.waitForBackgroundJavaScript(30 * 1000);
77+
HtmlTextArea emails = mailHomePage.getHtmlElementById("pop3_emails");
10678

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"));
11779
Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : user01@mail.local"));
11880
Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test"));
11981
Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email."));
12082
}
12183

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");
12886
submitButton.click();
87+
/* will wait JavaScript to execute up to 30s */
88+
webClient.waitForBackgroundJavaScript(30 * 1000);
89+
HtmlTextArea emails = mailHomePage.getHtmlElementById("imap_emails");
12990

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"));
14091
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."));
14495
}
14596
}

0 commit comments

Comments
 (0)