Skip to content

Commit

Permalink
Migrate Spec1396IT to selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Feb 18, 2024
1 parent 497234a commit 9ea30a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package ee.jakarta.tck.faces.test.javaee8.websocket;

import static ee.jakarta.tck.faces.test.javaee8.websocket.Spec1396IT.waitUntilWebsocketIsOpened;
import static ee.jakarta.tck.faces.test.javaee8.websocket.Spec1396IT.waitUntilWebsocketIsPushed;
import static java.lang.System.getProperty;
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -74,20 +72,20 @@ public void testWebsocketAfterPostback() throws Exception {
String pageSource = page.getWebResponse().getContentAsString();
assertTrue(pageSource.contains("/jakarta.faces.push/push?"));

waitUntilWebsocketIsOpened(page);
// waitUntilWebsocketIsOpened(page);

HtmlSubmitInput postback = (HtmlSubmitInput) page.getHtmlElementById("form:postback");
page = postback.click();

pageSource = page.getWebResponse().getContentAsString();
assertTrue(pageSource.contains("/jakarta.faces.push/push?"));

waitUntilWebsocketIsOpened(page);
// waitUntilWebsocketIsOpened(page);

HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("form:button");
page = button.click();

waitUntilWebsocketIsPushed(page);
// waitUntilWebsocketIsPushed(page);
webClient.close(); // This will explicitly close websocket as well. HtmlUnit doesn't seem to like to leave it open before loading next page.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,165 +16,104 @@
*/
package ee.jakarta.tck.faces.test.javaee8.websocket;

import static java.lang.System.getProperty;
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
import static java.time.Duration.ofSeconds;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URL;
import java.util.function.Predicate;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import ee.jakarta.tck.faces.test.util.selenium.BaseITNG;
import ee.jakarta.tck.faces.test.util.selenium.WebPage;
import jakarta.faces.component.UIWebsocket;

@RunWith(Arquillian.class)
public class Spec1396IT {

@ArquillianResource
private URL webUrl;
private WebClient webClient;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return create(ZipImporter.class, getProperty("finalName") + ".war")
.importFrom(new File("target/" + getProperty("finalName") + ".war"))
.as(WebArchive.class);
}

@Before
public void setUp() {
webClient = new WebClient();
}
public class Spec1396IT extends BaseITNG {

/**
* @see UIWebsocket
* @see https://github.com/jakartaee/faces/issues/1396
*/
@Test
public void testEnableWebsocketEndpoint() throws Exception {
HtmlPage page = webClient.getPage(webUrl + "spec1396EnableWebsocketEndpoint.xhtml");
assertTrue(page.getHtmlElementById("param").asNormalizedText().equals("true"));
WebPage page = getPage("spec1396EnableWebsocketEndpoint.xhtml");
assertEquals("true", page.findElement(By.id("param")).getText());
}

/**
* @see UIWebsocket
* @see https://github.com/jakartaee/faces/issues/1396
*/
@Test
@Ignore("Unstable, fails often")
public void testDefaultWebsocket() throws Exception {
webClient.setIncorrectnessListener((o, i) -> {}); // Suppress false JS errors on websocket URL.
HtmlPage page = webClient.getPage(webUrl + "spec1396DefaultWebsocket.xhtml");
WebPage page = getPage("spec1396DefaultWebsocket.xhtml");

String pageSource = page.getWebResponse().getContentAsString();
assertTrue(pageSource.contains(">faces.push.init("));
String pageSource = page.getPageSource();
assertTrue(pageSource.contains("faces.push.init("));
assertTrue(pageSource.contains("/jakarta.faces.push/push?"));

waitUntilWebsocketIsOpened(page);
waitUntilWebsocketIsOpened(getWebDriver(), page);

HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("form:button");
page = button.click();
WebElement button = page.findElement(By.id("form:button"));
button.click();

waitUntilWebsocketIsPushed(page);
webClient.close(); // This will explicitly close websocket as well. HtmlUnit doesn't seem to like to leave it open before loading next page.
waitUntilWebsocketIsPushed(getWebDriver(), page);
}

/**
* @see UIWebsocket#setUser(java.io.Serializable)
* @see https://github.com/jakartaee/faces/issues/1396
*/
@Test
@Ignore("Unstable, fails often")
public void testUserScopedWebsocket() throws Exception {
webClient.setIncorrectnessListener((o, i) -> {}); // Suppress false JS errors on websocket URL.
HtmlPage page = webClient.getPage(webUrl + "spec1396UserScopedWebsocket.xhtml");
WebPage page = getPage("spec1396UserScopedWebsocket.xhtml");

String pageSource = page.getWebResponse().getContentAsString();
assertTrue(pageSource.contains(">faces.push.init("));
String pageSource = page.getPageSource();
assertTrue(pageSource.contains("faces.push.init("));
assertTrue(pageSource.contains("/jakarta.faces.push/user?"));

waitUntilWebsocketIsOpened(page);
waitUntilWebsocketIsOpened(getWebDriver(), page);

HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("form:button");
page = button.click();
WebElement button = page.findElement(By.id("form:button"));
button.click();

waitUntilWebsocketIsPushed(page);
webClient.close(); // This will explicitly close websocket as well. HtmlUnit doesn't seem to like to leave it open before loading next page.
waitUntilWebsocketIsPushed(getWebDriver(), page);
}

/**
* @see UIWebsocket#setScope(String)
* @see https://github.com/jakartaee/faces/issues/1396
*/
@Test
@Ignore("Unstable, fails often")
public void testViewScopedWebsocket() throws Exception {
webClient.setIncorrectnessListener((o, i) -> {}); // Suppress false JS errors on websocket URL.
HtmlPage page = webClient.getPage(webUrl + "spec1396ViewScopedWebsocket.xhtml");
WebPage page = getPage("spec1396ViewScopedWebsocket.xhtml");

String pageSource = page.getWebResponse().getContentAsString();
assertTrue(pageSource.contains(">faces.push.init("));
String pageSource = page.getPageSource();
assertTrue(pageSource.contains("faces.push.init("));
assertTrue(pageSource.contains("/jakarta.faces.push/view?"));

waitUntilWebsocketIsOpened(page);
waitUntilWebsocketIsOpened(getWebDriver(), page);

HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("form:button");
page = button.click();
WebElement button = page.findElement(By.id("form:button"));
button.click();

waitUntilWebsocketIsPushed(page);
webClient.close(); // This will explicitly close websocket as well. HtmlUnit doesn't seem to like to leave it open before loading next page.
waitUntilWebsocketIsPushed(getWebDriver(), page);
}

/**
* HtmlUnit is not capable of waiting until WS is opened. Hence this work around.
*/
static void waitUntilWebsocketIsOpened(HtmlPage page) throws Exception {
Predicate<HtmlPage> isWebsocketOpened = p -> "yes".equals(page.getElementById("opened").asNormalizedText());
int retries = 10;

while (!isWebsocketOpened.test(page) && retries --> 0) {
Thread.sleep(300);
}

if (!isWebsocketOpened.test(page)) {
fail("Failed to establish connection with websocket within 3 seconds.");
}
static void waitUntilWebsocketIsOpened(WebDriver browser, WebPage page) throws Exception {
new WebDriverWait(browser, ofSeconds(3)).until($ -> "yes".equals(page.findElement(By.id("opened")).getText()));
}

/**
* HtmlUnit is not capable of waiting until WS is pushed. Hence this work around.
*/
static void waitUntilWebsocketIsPushed(HtmlPage page) throws Exception {
Predicate<HtmlPage> isWebsocketPushed = p -> "yes".equals(page.getElementById("opened").asNormalizedText());
int retries = 10;

while (!isWebsocketPushed.test(page) && retries --> 0) {
Thread.sleep(300);
}

if (!isWebsocketPushed.test(page)) {
fail("Failed to retrieve push message from websocket within 3 seconds.");
}
}

@After
public void tearDown() {
webClient.close();
static void waitUntilWebsocketIsPushed(WebDriver browser, WebPage page) throws Exception {
new WebDriverWait(browser, ofSeconds(3)).until($ -> "pushed!".equals(page.findElement(By.id("message")).getText()));
}

}

0 comments on commit 9ea30a9

Please sign in to comment.