Skip to content

Commit

Permalink
feat: Selenoid compatible the conf file (#3209)
Browse files Browse the repository at this point in the history
* feat: fix bug 3187

* feat: Compatible the conf file

---------

Co-authored-by: kayle <[email protected]>
  • Loading branch information
1109471432 and kayle authored Aug 4, 2023
1 parent db80568 commit 22e4c38
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import net.serenitybdd.core.webdriver.driverproviders.CapabilityValue;
import net.serenitybdd.core.webdriver.enhancers.BeforeAWebdriverScenario;
import net.thucydides.core.environment.SystemEnvironmentVariables;
import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.webdriver.SupportedWebDriver;
import net.thucydides.core.webdriver.capabilities.W3CCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static net.serenitybdd.plugins.selenoid.SerenitySelenoidUtil.SELENOID;
import static net.serenitybdd.plugins.selenoid.SerenitySelenoidUtil.*;

public class BeforeASelenoidScenario implements BeforeAWebdriverScenario {

Expand All @@ -24,25 +28,31 @@ public MutableCapabilities apply(EnvironmentVariables environmentVariables, Supp
if (driver != SupportedWebDriver.REMOTE) {
return capabilities;
}
Map<String, Object> SELENOID_PROPERTIES_MAP = new HashMap<>();
String name = SerenitySelenoidUtil.getName(testOutcome);
SELENOID_PROPERTIES_MAP.put("name", name);
SELENOID_PROPERTIES_MAP.put("videoName", SerenitySelenoidUtil.getVideoName(name));
Map<String, Object> selenoidOptionsMap = SerenitySelenoidUtil.getSelenoidOptionsMap(testOutcome);

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
// Set video link to serenity report
SerenitySelenoidUtil.linkVideoToSerenityReport(testOutcome);


// get properties from serenity.properties
Properties selenoidProperties = environmentVariables.getPropertiesWithPrefix(SELENOID);
for (String propertyName : selenoidProperties.stringPropertyNames()) {
String unPrefixedPropertyName = SerenitySelenoidUtil.unprefixed(propertyName);
Object value = CapabilityValue.fromString(selenoidProperties.getProperty(propertyName));
capability.stream().filter(k -> k.equalsIgnoreCase(unPrefixedPropertyName)).forEach(k -> desiredCapabilities.setCapability(k, value));
SELENOID_PROPERTIES_MAP.put(unPrefixedPropertyName, value);
capability.stream().filter(k -> k.equalsIgnoreCase(unPrefixedPropertyName)).forEach(k -> capabilities.setCapability(k, value));
if (propertyName.startsWith(SELENOID_OPTIONS)) {
selenoidOptionsMap.put(propertyName.replace(SELENOID_OPTIONS, ""), value);
}
}

SerenitySelenoidUtil.linkVideoToSerenityReport(testOutcome, SerenitySelenoidUtil.getVideoName(name));


capabilities.setCapability("selenoid:options", SELENOID_PROPERTIES_MAP);
capabilities.merge(desiredCapabilities);
// get properties from serenity.conf
MutableCapabilities mutableCapabilities = W3CCapabilities.definedIn(SystemEnvironmentVariables.currentEnvironmentVariables()).withPrefix("selenoid").forDriver(driver);
capabilities.merge(mutableCapabilities);
Object o = mutableCapabilities.asMap().get(SELENOID_OPTIONS_CONFIG);
if (o instanceof Map) {
selenoidOptionsMap.putAll((Map) o);
}
capabilities.setCapability(SELENOID_OPTIONS_CONFIG, selenoidOptionsMap);
return capabilities;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,6 +22,11 @@ public class SerenitySelenoidUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(SerenitySelenoidUtil.class);

public static final String SELENOID = "selenoid.";
public static final String SELENOID_OPTIONS = "selenoid.options.";
public static final String SELENOID_OPTIONS_CONFIG = "selenoid:options";
public static final String SELENOID_VIDEO_DATE_PREFIX_FORMAT = "videoDatePrefixFormat";
public static final String SELENOID_VIDEO_LINK_PREFIX = "videoLinkPrefix";
public static final Map<String, Object> SELENOID_PROPERTIES_MAP = new HashMap<>();

public static EnvironmentVariables env = SystemEnvironmentVariables.currentEnvironmentVariables();

Expand All @@ -26,30 +35,36 @@ public static String getName(TestOutcome testOutcome) {

}

public static String getVideoName(String name) {
return String.format("%s%s", name, getVideoNameSuffix());
public static String getVideoName(TestOutcome testOutcome) {
return String.format("%s%s", getName(testOutcome), ".mp4");
}

public static String getVideoDataPrefix() {
String dataFormat = env.getProperty(SELENOID + "videoDataPrefixFormat", "yyyy-MM-dd");
String dataFormat = env.getProperty(SELENOID_OPTIONS + SELENOID_VIDEO_DATE_PREFIX_FORMAT, env.getProperty(String.format("%s\"%s\".%s", SELENOID, SELENOID_OPTIONS_CONFIG, SELENOID_VIDEO_DATE_PREFIX_FORMAT), "yyyy-MM-dd"));
return DateTimeFormatter.ofPattern(dataFormat).format(LocalDateTime.now(ZoneId.systemDefault()));
}

public static String getVideoNameSuffix() {
return env.getProperty(SELENOID + "videoSuffix", ".mp4");
}

protected static void linkVideoToSerenityReport(TestOutcome testOutcome, String link) {
protected static void linkVideoToSerenityReport(TestOutcome testOutcome) {
try {
URL url = new URL(env.getProperty("webdriver.remote.url"));
String videoLinkPrefix = env.getProperty(SELENOID + "videoLinkPrefix", String.format("%s:%s:8080/video", url.getProtocol(), url.getHost()));
testOutcome.setLink(new ExternalLink(String.format("%s/%s", videoLinkPrefix, link), "BrowserStack"));
String videoLinkPrefix = env.getProperty(SELENOID_OPTIONS + SELENOID_VIDEO_LINK_PREFIX, env.getProperty(String.format("%s\"%s\".%s", SELENOID, SELENOID_OPTIONS_CONFIG, SELENOID_VIDEO_LINK_PREFIX), String.format("%s:%s:8080/video", url.getProtocol(), url.getHost())));
testOutcome.setLink(new ExternalLink(String.format("%s/%s", videoLinkPrefix, SELENOID_PROPERTIES_MAP.get("videoName")), "BrowserStack"));
} catch (MalformedURLException e) {
LOGGER.error("Set video link error. {}", e.getMessage());
}

}

protected static Map<String, Object> getSelenoidOptionsMap(TestOutcome testOutcome) {
// Set selenoid videoFileName and VideoName
String name = SerenitySelenoidUtil.getName(testOutcome);
SELENOID_PROPERTIES_MAP.put("name", name);
SELENOID_PROPERTIES_MAP.put("videoName", getVideoName(testOutcome));
Arrays.asList("enableVNC", "enableVideo").forEach(k -> SELENOID_PROPERTIES_MAP.put(k, true));
return SELENOID_PROPERTIES_MAP;
}


protected static String unprefixed(String propertyName) {
return propertyName.replace(SELENOID, "");
}
Expand Down

0 comments on commit 22e4c38

Please sign in to comment.