This is an automated email from the ASF dual-hosted git repository. benw pushed a commit to branch javax in repository https://gitbox.apache.org/repos/asf/tapestry-5.git
commit 88b449320129ce81690b88240ab5ff02d8732252 Author: Ben Weidig <[email protected]> AuthorDate: Sun Apr 5 18:22:27 2026 +0200 SeleniumTestCase make page-load/wait timeout configurable via annotation/props --- .../integration/app1/CoreBehaviorsTests.java | 6 +- .../tapestry5/integration/app1/FormTests.java | 2 +- .../apache/tapestry5/test/SeleniumTestCase.java | 67 +++++++++++++++------- .../tapestry5/test/TapestryTestConfiguration.java | 19 +++++- .../services/web/WebResourcesTest.java | 10 ++-- 5 files changed, 74 insertions(+), 30 deletions(-) diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java index 476f42dea..5d84e302a 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/CoreBehaviorsTests.java @@ -86,12 +86,12 @@ public class CoreBehaviorsTests extends App1TestCase assertTextPresent("[]"); select("//select[@id='blockName']", "fred"); - waitForPageToLoad(PAGE_LOAD_TIMEOUT); + waitForPageToLoad(getPageLoadTimeout()); assertTextPresent("[Block fred.]"); select("//select[@id='blockName']", "barney"); - waitForPageToLoad(PAGE_LOAD_TIMEOUT); + waitForPageToLoad(getPageLoadTimeout()); assertTextPresent("[Block barney.]"); @@ -1063,7 +1063,7 @@ public class CoreBehaviorsTests extends App1TestCase assertTextPresent("Error obtaining injected value for field org.apache.tapestry5.integration.app1.pages.FailedInjectDemo.buffer: No service implements the interface java.lang.StringBuffer."); refresh(); - waitForPageToLoad(PAGE_LOAD_TIMEOUT); + waitForPageToLoad(getPageLoadTimeout()); // Before this bug was fixed, this message would not appear; instead on // complaining about _$resources would appear which was very confusing. diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java index d12b7f584..09e4f3b92 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java @@ -350,7 +350,7 @@ public class FormTests extends App1TestCase private void waitForSelectedToBeRemoved() { - waitForCondition("selenium.browserbot.getCurrentWindow().testSupport.findCSSMatchCount('td.selected') == 0", PAGE_LOAD_TIMEOUT); + waitForCondition("selenium.browserbot.getCurrentWindow().testSupport.findCSSMatchCount('td.selected') == 0", getPageLoadTimeout()); } // TAP5-1408, TAP5-2203 diff --git a/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java b/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java index b96d1c4a4..ebd59c647 100644 --- a/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java +++ b/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java @@ -66,13 +66,6 @@ public abstract class SeleniumTestCase extends Assert implements Selenium { public final static Logger LOGGER = LoggerFactory.getLogger(SeleniumTestCase.class); - public static final long WAIT_TIMEOUT = Long.getLong("selenium.wait.timeout", 15L); - - /** - * 15 seconds - */ - public static final String PAGE_LOAD_TIMEOUT = "15000"; - public static final String TOMCAT = "tomcat"; public static final String JETTY = "jetty"; @@ -85,6 +78,9 @@ public abstract class SeleniumTestCase extends Assert implements Selenium */ public static final String SUBMIT = "//input[@type='submit']"; + private long waitTimeout = 0L; + private String pageLoadTimeout = null; + /** * The underlying {@link Selenium} instance that all the methods of this class delegate to; * this can be useful when attempting to use SeleniumTestCase with a newer version of Selenium which @@ -199,6 +195,10 @@ public abstract class SeleniumTestCase extends Assert implements Selenium annotation = EmptyInnerClass.class.getAnnotation(TapestryTestConfiguration.class); } + this.waitTimeout = getLongParameter(xmlTest, "selenium.wait.timeout", annotation.waitTimeout()); + long pageLoadNum = getLongParameter(xmlTest, "selenium.page-load.timout", annotation.pageLoadTimeout()); + this.pageLoadTimeout = String.valueOf(pageLoadNum * 1_000L); + String webAppFolder = getParameter(xmlTest, TapestryTestConstants.WEB_APP_FOLDER_PARAMETER, annotation.webAppFolder()); String container = getParameter(xmlTest, TapestryTestConstants.SERVLET_CONTAINER_PARAMETER, @@ -215,13 +215,15 @@ public abstract class SeleniumTestCase extends Assert implements Selenium String sep = System.getProperty("line.separator"); LOGGER.info("Starting SeleniumTestCase:" + sep + - " currentDir: " + System.getProperty("user.dir") + sep + - " webAppFolder: " + webAppFolder + sep + - " container: " + container + sep + - " contextPath: " + contextPath + sep + - String.format(" ports: %d / %d", port, sslPort) + sep + - " browserStart: " + browserStartCommand + sep + - " baseURL: " + baseURL); + " currentDir: " + System.getProperty("user.dir") + sep + + " webAppFolder: " + webAppFolder + sep + + " container: " + container + sep + + " contextPath: " + contextPath + sep + + String.format(" ports: %d / %d", port, sslPort) + sep + + " browserStart: " + browserStartCommand + sep + + " baseURL: " + baseURL + sep + + " waitTimeout: " + this.waitTimeout + "s" + sep + + " pageLoadTimeout: " + pageLoadNum + "s"); final Runnable stopWebServer = launchWebServer(container, webAppFolder, contextPath, port, sslPort); @@ -386,7 +388,14 @@ public abstract class SeleniumTestCase extends Assert implements Selenium { String value = getParameter(xmlTest, key, null); - return value != null ? Integer.parseInt(value) : defaultValue; + return value != null && !value.isEmpty() ? Integer.parseInt(value) : defaultValue; + } + + private final long getLongParameter(XmlTest xmlTest, String key, long defaultValue) + { + String value = getParameter(xmlTest, key, null); + + return value != null && !value.isEmpty() ? Long.parseLong(value) : defaultValue; } /** @@ -498,6 +507,24 @@ public abstract class SeleniumTestCase extends Assert implements Selenium return baseURL; } + /** + * Returns the WebDriver wait timeout in seconds. + * + * Either default vaule (15L) or set via system properties (selenium.wait.timeout). + */ + public long getWaitTimeout() { + return this.waitTimeout; + } + + /** + * Returns the Selenium page load timeout in millis as String. + * + * Either default vaule ("15000") or set via system properties (selenium.page-loag.timeout). + */ + public String getPageLoadTimeout() { + return this.pageLoadTimeout; + } + @BeforeMethod public void indicateTestMethodName(Method testMethod) { @@ -1440,7 +1467,7 @@ public abstract class SeleniumTestCase extends Assert implements Selenium protected void waitForCondition(ExpectedCondition condition) { - waitForCondition(condition, WAIT_TIMEOUT); + waitForCondition(condition, getWaitTimeout()); } protected void waitForCondition(ExpectedCondition condition, long timeoutSeconds) @@ -1669,7 +1696,7 @@ public abstract class SeleniumTestCase extends Assert implements Selenium */ protected final void waitForPageToLoad() { - waitForPageToLoad(PAGE_LOAD_TIMEOUT); + waitForPageToLoad(getPageLoadTimeout()); } /** @@ -1833,7 +1860,7 @@ public abstract class SeleniumTestCase extends Assert implements Selenium String condition = String.format("selenium.browserbot.getCurrentWindow().document.getElementById(\"%s\")", elementId); - waitForCondition(condition, PAGE_LOAD_TIMEOUT); + waitForCondition(condition, getPageLoadTimeout()); } /** @@ -1848,7 +1875,7 @@ public abstract class SeleniumTestCase extends Assert implements Selenium String condition = String.format("selenium.browserbot.getCurrentWindow().document.querySelector(\"%s\")", selector); - waitForCondition(condition, PAGE_LOAD_TIMEOUT); + waitForCondition(condition, getPageLoadTimeout()); } /** @@ -1866,7 +1893,7 @@ public abstract class SeleniumTestCase extends Assert implements Selenium { String condition = String.format("selenium.browserbot.getCurrentWindow().testSupport.doesNotExist(\"%s\")", elementId); - waitForCondition(condition, PAGE_LOAD_TIMEOUT); + waitForCondition(condition, getPageLoadTimeout()); } /** diff --git a/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java b/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java index ef9f9d94a..65f902717 100644 --- a/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java +++ b/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java @@ -1,4 +1,4 @@ -// Copyright 2011-2013 The Apache Software Foundation +// Copyright 2011-2013, 2026 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,6 +16,10 @@ package org.apache.tapestry5.test; import java.lang.annotation.*; +import org.openqa.selenium.support.ui.WebDriverWait; + +import com.thoughtworks.selenium.Selenium; + /** * To be used on Selenium-based integration tests that extend {@link SeleniumTestCase} as an alternative to using a * TestNG XML configuration file. Using the XML file, it's intricate to run <em>individual</em> test classes or @@ -62,4 +66,17 @@ public @interface TapestryTestConfiguration * The browser start command to use with Selenium. Defaults to "*firefox". */ String browserStartCommand() default "*firefox"; + + /** + * The duration of seconds {@link WebDriverWait} should use. + * Defaults to 15, overridable via TestNG xml or system properties: {@code selenium.wait.timeout} + */ + long waitTimeout() default 15L; + + /** + * The duration of seconds {@link Selenium#waitForPageToLoad(String)} is using. + * Defaults to 15, overridable via TestNG or system properties: {@code selenium.page-load.timeout} + * The value is automatically converted to String. + */ + long pageLoadTimeout() default 15L; } diff --git a/tapestry-webresources/src/test/java/t5/webresources/services/web/WebResourcesTest.java b/tapestry-webresources/src/test/java/t5/webresources/services/web/WebResourcesTest.java index f96f77120..b260b674c 100644 --- a/tapestry-webresources/src/test/java/t5/webresources/services/web/WebResourcesTest.java +++ b/tapestry-webresources/src/test/java/t5/webresources/services/web/WebResourcesTest.java @@ -1,4 +1,4 @@ -// Copyright 2023 The Apache Software Foundation +// Copyright 2023, 2026 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import org.testng.annotations.Test; /** * Adapted from WebResourcesSpec.groovy.s */ -@TapestryTestConfiguration(webAppFolder = "src/test/webapp") +@TapestryTestConfiguration(webAppFolder = "src/test/webapp", pageLoadTimeout = 60L) public class WebResourcesTest extends SeleniumTestCase { @Test @@ -39,12 +39,12 @@ public class WebResourcesTest extends SeleniumTestCase { click("css=.navbar .dropdown-toggle"); click("link=MultiLess"); waitForInitializedPage(); - waitForCondition("document.getElementById('demo') != null", PAGE_LOAD_TIMEOUT); + waitForCondition("document.getElementById('demo') != null", getPageLoadTimeout()); assertEquals(getEval("window.getComputedStyle(document.getElementById('demo'), null).getPropertyValue('background-color')"), "rgb(179, 179, 255)"); } private void waitForInitializedPage() { - waitForCondition("$('body').attr('data-page-initialized') == 'true' ", PAGE_LOAD_TIMEOUT); + waitForCondition("document.body.getAttribute('data-page-initialized') == 'true' ", + getPageLoadTimeout()); } - }
