Skip to content

Commit

Permalink
[WFLY-20129] Mail QS tests - Use HtmlUnit instead of Selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
kstekovi committed Jan 23, 2025
1 parent 3ffabcb commit 47c44ff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 119 deletions.
14 changes: 5 additions & 9 deletions mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<!-- the versions for other Dependencies and Plugins -->
<version.org.seleniumhq.selenium>4.15.0</version.org.seleniumhq.selenium>
<version.webdrivermanager>5.7.0</version.webdrivermanager>
<!-- the version for HtmlUnit -->
<version.net.sourceforge.htmlunit>2.70.0</version.net.sourceforge.htmlunit>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -100,18 +102,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${version.org.seleniumhq.selenium}</version>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${version.net.sourceforge.htmlunit}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${version.webdrivermanager}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
179 changes: 69 additions & 110 deletions mail/src/test/java/org/jboss/as/quickstarts/mail/MailTestCaseIT.java
Original file line number Diff line number Diff line change
@@ -1,143 +1,102 @@
package org.jboss.as.quickstarts.mail;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.After;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MailTestCaseIT {

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

private WebDriver driver;
private String serverHost;

@Before
public void testSetup() {
WebDriverManager.chromedriver().setup();

ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--headless");

driver = new ChromeDriver(options);
driver.manage().window().maximize();

String serverHost = System.getenv("SERVER_HOST");
serverHost = System.getenv("SERVER_HOST");
if (serverHost == null) {
serverHost = System.getProperty("server.host");
}
if (serverHost == null) {
serverHost = DEFAULT_SERVER_HOST;
}

driver.get(serverHost+"/mail");
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
}

@After
public void cleanUp() {
if (driver != null) {
driver.close();
}
}

@Test
public void a_testSMTP() {
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(15));

WebElement from = driver.findElement(By.id("smtp_from"));
WebElement to = driver.findElement(By.id("smtp_to"));
WebElement subject = driver.findElement(By.id("smtp_subject"));
WebElement body = driver.findElement(By.id("smtp_body"));

from.clear();
from.sendKeys("[email protected]");

to.clear();
to.sendKeys("[email protected]");

subject.clear();
subject.sendKeys("This is a test");

body.clear();
body.sendKeys("Hello user02, I've sent an email.");

WebElement submitButton = driver.findElement(By.id("smtp_send_btn"));
submitButton.click();

WebElement message = driver.findElement(By.xpath("//ul[@id='smtp_messages']/li"));
wait.until(d -> message.isDisplayed());

Assert.assertEquals("Unexpected result messages after sending an email via SMTP.", "Email sent to [email protected]", message.getText());
public void a_testSMTP() throws IOException {
try (final WebClient webClient = new WebClient()) {
// Get the first page
HtmlPage mailHomePage = webClient.getPage(serverHost + "/mail");

HtmlInput from = mailHomePage.getHtmlElementById("smtp_from");
HtmlInput to = mailHomePage.getHtmlElementById("smtp_to");
HtmlInput subject = mailHomePage.getHtmlElementById("smtp_subject");
HtmlTextArea body = mailHomePage.getHtmlElementById("smtp_body");
HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("smtp_send_btn");

from.setValue("[email protected]");
to.setValue("[email protected]");
subject.setValue("This is a test");
body.setText("Hello user02, I've sent an email.");

submitButton.click();
/* will wait JavaScript to execute up to 30s */
webClient.waitForBackgroundJavaScript(30 * 1000);

HtmlElement message = mailHomePage.getFirstByXPath("//ul[@id=\"smtp_messages\"]");
Assert.assertEquals("Unexpected result messages after sending an email via SMTP.", "Email sent to [email protected]", message.asNormalizedText());
}
}

@Test
public void b_retrievePOP3() {
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(15));

WebElement user = driver.findElement(By.id("pop3_user"));
WebElement password = driver.findElement(By.id("pop3_password"));

user.clear();
user.sendKeys("[email protected]");

password.clear();
password.sendKeys("1234");

WebElement submitButton = driver.findElement(By.id("pop3_get_emails_btn"));
submitButton.click();

wait.until(d -> {
try {
WebElement emails = driver.findElement(By.id("pop3_emails"));
return !emails.getText().isEmpty();
} catch (StaleElementReferenceException sere) {
return false;
}
});

WebElement emails = driver.findElement(By.id("pop3_emails"));
Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : [email protected]"));
Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test"));
Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email."));
public void b_retrievePOP3() throws IOException {
try (final WebClient webClient = new WebClient()) {
// Get the first page
HtmlPage mailHomePage = webClient.getPage(serverHost + "/mail");

HtmlInput user = mailHomePage.getHtmlElementById("pop3_user");
HtmlInput password = mailHomePage.getHtmlElementById("pop3_password");
HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("pop3_get_emails_btn");

user.setValue("[email protected]");
password.setValue("1234");
submitButton.click();
/* will wait JavaScript to execute up to 30s */
webClient.waitForBackgroundJavaScript(30 * 1000);
HtmlTextArea emails = mailHomePage.getHtmlElementById("pop3_emails");

Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : [email protected]"));
Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test"));
Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email."));
}
}


@Test
public void c_retrieveIMAP() {
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(15));

WebElement submitButton = driver.findElement(By.id("imap_get_emails_btn"));
submitButton.click();

wait.until(d -> {
try {
WebElement emails = driver.findElement(By.id("imap_emails"));
return !emails.getText().isEmpty();
} catch (StaleElementReferenceException sere) {
return false;
}
});

WebElement emails = driver.findElement(By.id("imap_emails"));
Assert.assertNotNull("IMAP No messages found.", emails.getText());
Assert.assertTrue("Expected email not found.", emails.getText().contains("From : [email protected]"));
Assert.assertTrue("Expected email not found.", emails.getText().contains("Subject : This is a test"));
Assert.assertTrue("Expected email not found.", emails.getText().contains("Body : Hello user02, I've sent an email."));
public void c_retrieveIMAP() throws IOException {
try (final WebClient webClient = new WebClient()) {
// Get the first page
HtmlPage mailHomePage = webClient.getPage(serverHost + "/mail");

HtmlSubmitInput submitButton = mailHomePage.getHtmlElementById("imap_get_emails_btn");
submitButton.click();
/* will wait JavaScript to execute up to 30s */
webClient.waitForBackgroundJavaScript(30 * 1000);
HtmlTextArea emails = mailHomePage.getHtmlElementById("imap_emails");

Assert.assertNotNull("IMAP No messages found.", emails.getText());
Assert.assertTrue("Expected From not found: " + emails.getText(), emails.getText().contains("From : [email protected]"));
Assert.assertTrue("Expected Subject not found: " + emails.getText(), emails.getText().contains("Subject : This is a test"));
Assert.assertTrue("Expected Body not found : " + emails.getText(), emails.getText().contains("Body : Hello user02, I've sent an email."));
}
}
}
}

0 comments on commit 47c44ff

Please sign in to comment.