Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-20129] Mail QS tests - Use HtmlUnit instead of Selenium #1009

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
<!-- the versions for BOMs, Packs and Plugins -->
<version.bom.ee>${version.server}</version.bom.ee>
<version.plugin.wildfly>5.1.1.Final</version.plugin.wildfly>
<!-- 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 +99,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
177 changes: 68 additions & 109 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."));
}
}
}
Loading