Skip to content

Commit

Permalink
Improved Browserstack session name support
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Dec 2, 2023
1 parent a51c4e7 commit 8bae27e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public MutableCapabilities apply(EnvironmentVariables environmentVariables,
// Add the test name to the capabilities
newOptions.put("sessionName", testName);

// Add the build name to the capabilities
newOptions.put("buildName", BuildNameGenerator.forEnvironmentVariables(environmentVariables).getBuildName());


// Add the Browserstack options to the capabilities
capabilities.setCapability(BSTACK_OPTIONS_CAPABILITY, newOptions);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.serenitybdd.plugins.browserstack;

import net.serenitybdd.core.Serenity;
import net.thucydides.model.ThucydidesSystemProperty;
import net.thucydides.model.util.EnvironmentVariables;

import java.io.File;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Get a build name from the environment variables, or generate one if not provided.
* It can be defined externally by setting the BROWSERSTACK_BUILD_NAME environment variable.
* The build name is used to group test results in BrowserStack.
*/
public class BuildNameGenerator {
private final EnvironmentVariables environmentVariables;

BuildNameGenerator(EnvironmentVariables environmentVariables) {
this.environmentVariables = environmentVariables;
}

public static BuildNameGenerator forEnvironmentVariables(EnvironmentVariables environmentVariables) {
return new BuildNameGenerator(environmentVariables);
}

public String getBuildName() {
return this.environmentVariables.optionalProperty("BROWSERSTACK_BUILD_NAME")
.orElseGet(() -> this.environmentVariables.optionalProperty("BROWSERSTACK_BUILD_NAME")
.orElse(defaultBuildName()));
}

private String defaultBuildName() {

String currentDirectoryName = Paths.get("").toAbsolutePath().toFile().getName();
String currentProjectName = ThucydidesSystemProperty.SERENITY_PROJECT_NAME.from(environmentVariables,currentDirectoryName);
String date = Serenity.getTestSuiteStartTime().format(DateTimeFormatter.BASIC_ISO_DATE)
+ "-" + Serenity.getTestSuiteStartTime().format(DateTimeFormatter.ofPattern("HHmmss"));
return currentProjectName + " - " + date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openqa.selenium.firefox.FirefoxProfile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -364,4 +365,10 @@ private static StepEventBus getStepEventBus() {
}
return StepEventBus.getParallelEventBus();
}

private static final LocalDateTime TEST_SUITE_START_TIME = LocalDateTime.now();

public static LocalDateTime getTestSuiteStartTime() {
return TEST_SUITE_START_TIME;
}
}

0 comments on commit 8bae27e

Please sign in to comment.