From 8e3e26e0a21e393d6a14855e27e74e4684523786 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Mon, 6 May 2024 11:58:27 +0000 Subject: [PATCH] [java][grid]: Set test name to video file name in dynamic grid (#13907) * [java][grid]: Set test name to video file name in dynamic grid Signed-off-by: Viet Nguyen Duc * Revert unexpected changes Signed-off-by: Viet Nguyen Duc --------- Signed-off-by: Viet Nguyen Duc --- .../node/docker/DockerSessionFactory.java | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java index dd0ed03d6fde0..d7aa7eec721fe 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java @@ -301,23 +301,34 @@ private Container createBrowserContainer(int port, Capabilities sessionCapabilit } private Map getBrowserContainerEnvVars(Capabilities sessionRequestCapabilities) { - Optional screenResolution = - ofNullable(getScreenResolution(sessionRequestCapabilities)); Map envVars = new HashMap<>(); - if (screenResolution.isPresent()) { - envVars.put("SE_SCREEN_WIDTH", String.valueOf(screenResolution.get().getWidth())); - envVars.put("SE_SCREEN_HEIGHT", String.valueOf(screenResolution.get().getHeight())); - } - Optional timeZone = ofNullable(getTimeZone(sessionRequestCapabilities)); - timeZone.ifPresent(zone -> envVars.put("TZ", zone.getID())); // Passing env vars set to the child container + setEnvVarsToContainer(envVars); + // Capabilities set to env vars with higher precedence + setCapsToEnvVars(sessionRequestCapabilities, envVars); + return envVars; + } + + private void setEnvVarsToContainer(Map envVars) { Map seEnvVars = System.getenv(); seEnvVars.entrySet().stream() .filter( entry -> entry.getKey().startsWith("SE_") || entry.getKey().equalsIgnoreCase("LANGUAGE")) .forEach(entry -> envVars.put(entry.getKey(), entry.getValue())); - return envVars; + } + + private void setCapsToEnvVars( + Capabilities sessionRequestCapabilities, Map envVars) { + Optional screenResolution = + ofNullable(getScreenResolution(sessionRequestCapabilities)); + screenResolution.ifPresent( + dimension -> { + envVars.put("SE_SCREEN_WIDTH", String.valueOf(dimension.getWidth())); + envVars.put("SE_SCREEN_HEIGHT", String.valueOf(dimension.getHeight())); + }); + Optional timeZone = ofNullable(getTimeZone(sessionRequestCapabilities)); + timeZone.ifPresent(zone -> envVars.put("TZ", zone.getID())); } private Container startVideoContainer( @@ -357,17 +368,31 @@ private Container startVideoContainer( private Map getVideoContainerEnvVars( Capabilities sessionRequestCapabilities, String containerIp) { Map envVars = new HashMap<>(); + // Passing env vars set to the child container + setEnvVarsToContainer(envVars); + // Capabilities set to env vars with higher precedence + setCapsToEnvVars(sessionRequestCapabilities, envVars); envVars.put("DISPLAY_CONTAINER_NAME", containerIp); - Optional screenResolution = - ofNullable(getScreenResolution(sessionRequestCapabilities)); - screenResolution.ifPresent( - dimension -> { - envVars.put("SE_SCREEN_WIDTH", String.valueOf(dimension.getWidth())); - envVars.put("SE_SCREEN_HEIGHT", String.valueOf(dimension.getHeight())); - }); + Optional testName = ofNullable(getTestName(sessionRequestCapabilities)); + testName.ifPresent(name -> envVars.put("SE_VIDEO_FILE_NAME", String.format("%s.mp4", name))); return envVars; } + private String getTestName(Capabilities sessionRequestCapabilities) { + Optional testName = ofNullable(sessionRequestCapabilities.getCapability("se:name")); + if (testName.isPresent()) { + String name = testName.get().toString(); + if (!name.isEmpty()) { + name = name.replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_-]", ""); + if (name.length() > 251) { + name = name.substring(0, 251); + } + return name; + } + } + return null; + } + private TimeZone getTimeZone(Capabilities sessionRequestCapabilities) { Optional timeZone = ofNullable(sessionRequestCapabilities.getCapability("se:timeZone")); if (timeZone.isPresent()) {