Skip to content

Commit

Permalink
Add test property to set the browser time zone (#2258)
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-tchad authored Feb 13, 2025
1 parent ae27967 commit cffd149
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/org/labkey/test/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -70,6 +72,8 @@ public abstract class TestProperties
}
}

private static ZoneId browserTimeZone = null;

public static void load()
{
/* Force static block to run */
Expand Down Expand Up @@ -162,6 +166,26 @@ public static boolean isDumpBrowserConsole()
return getBooleanProperty("webtest.dump.browser.console", false);
}

public static ZoneId getBrowserTimeZone()
{
if (browserTimeZone == null)
{
String tz = StringUtils.trimToNull(System.getProperty("webtest.browser.tz"));
if (tz != null)
{
String[] split = tz.split("[,\\s]+");
tz = split[LocalDateTime.now().getDayOfMonth() % split.length];
// Verify that time zone is valid
browserTimeZone = ZoneId.of(tz);
}
else
{
browserTimeZone = ZoneId.systemDefault();
}
}
return browserTimeZone;
}

public static double getTimeoutMultiplier()
{
return Math.max(0, getDoubleProperty("webtest.timeout.multiplier", 1.0));
Expand Down
25 changes: 21 additions & 4 deletions src/org/labkey/test/WebDriverWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverService;
Expand All @@ -119,6 +120,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -210,6 +212,17 @@ protected Pair<WebDriver, DriverService> createNewWebDriver(@NotNull Pair<WebDri
WebDriver newWebDriver = null;
DriverService oldDriverService = oldDriverAndService.getRight();
DriverService newDriverService = null;
Map<String, String> browserEnv = new HashMap<>();
ZoneId browserTimeZone = TestProperties.getBrowserTimeZone();
if (browserTimeZone != ZoneId.systemDefault())
{
TestLogger.info("Starting browser with TZ = " + browserTimeZone);
browserEnv.put("TZ", browserTimeZone.toString());
}
else
{
TestLogger.info("Starting browser with TZ = " + browserTimeZone + " (system default)");
}

switch (browserType)
{
Expand All @@ -236,7 +249,8 @@ protected Pair<WebDriver, DriverService> createNewWebDriver(@NotNull Pair<WebDri
}
if (oldWebDriver == null)
{
newWebDriver = new InternetExplorerDriver();
newDriverService = new InternetExplorerDriverService.Builder().withEnvironment(browserEnv).build();
newWebDriver = new InternetExplorerDriver((InternetExplorerDriverService) newDriverService);
}
break;
}
Expand Down Expand Up @@ -276,7 +290,7 @@ protected Pair<WebDriver, DriverService> createNewWebDriver(@NotNull Pair<WebDri
options.addArguments("headless");
}

newDriverService = ChromeDriverService.createDefaultService();
newDriverService = new ChromeDriverService.Builder().withEnvironment(browserEnv).build();
newWebDriver = new ChromeDriver((ChromeDriverService) newDriverService, options);
}
break;
Expand Down Expand Up @@ -373,7 +387,10 @@ protected Pair<WebDriver, DriverService> createNewWebDriver(@NotNull Pair<WebDri
capabilities.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);
FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);

newDriverService = GeckoDriverService.createDefaultService();
GeckoDriverService.Builder driverServiceBuilder = new GeckoDriverService.Builder()
.withEnvironment(browserEnv);

newDriverService = driverServiceBuilder.build();
try
{
newWebDriver = new FirefoxDriver((FirefoxDriverService) newDriverService, firefoxOptions);
Expand All @@ -384,7 +401,7 @@ protected Pair<WebDriver, DriverService> createNewWebDriver(@NotNull Pair<WebDri
{
retry.printStackTrace(System.err);
newDriverService.stop();
newDriverService = GeckoDriverService.createDefaultService();
newDriverService = driverServiceBuilder.build();
sleep(10000);
newWebDriver = new FirefoxDriver((FirefoxDriverService) newDriverService, firefoxOptions);
}
Expand Down
2 changes: 2 additions & 0 deletions test.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ selenium.reuseWebDriver=true
#webtest.webdriver.headless=false
## Customize browser window size
#webtest.window.size=1280x1024
## Set browser time zone
#webtest.browser.tz=America/New_York


#==============================================================================
Expand Down

0 comments on commit cffd149

Please sign in to comment.