forked from serenity-bdd/serenity-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>serenity-bdd</artifactId> | ||
<groupId>net.serenity-bdd</groupId> | ||
<version>2.5.11-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>serenity-lambdatest</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Serenity LambdaTest Integration</name> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Automatic-Module-Name>com.serenitybdd.serenitybrowserstack</Automatic-Module-Name> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>serenity-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<!-- TEST DEPENDENCIES --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Serenity BDD LambdaTest Integration | ||
|
||
To run your tests on LambdaTest, you will need to set the following system properties: | ||
* `` |
36 changes: 36 additions & 0 deletions
36
serenity-lambdatest/src/main/java/net/serenitybdd/lambdatest/AfterALambdaTestScenario.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.serenitybdd.lambdatest; | ||
|
||
import net.serenitybdd.core.webdriver.RemoteDriver; | ||
import net.serenitybdd.core.webdriver.enhancers.AfterAWebdriverScenario; | ||
import net.thucydides.core.model.ExternalLink; | ||
import net.thucydides.core.model.TestOutcome; | ||
import net.thucydides.core.util.EnvironmentVariables; | ||
import org.openqa.selenium.JavascriptExecutor; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.remote.SessionId; | ||
|
||
import static net.thucydides.core.model.TestResult.FAILURE; | ||
|
||
public class AfterALambdaTestScenario implements AfterAWebdriverScenario { | ||
|
||
@Override | ||
public void apply(EnvironmentVariables environmentVariables, TestOutcome testOutcome, WebDriver driver) { | ||
if (!LambdaTestConfiguration.isActiveFor(environmentVariables)) { | ||
return; | ||
} | ||
// Update the test outcome if the test failed | ||
if (testOutcome.getResult().isAtLeast(FAILURE)) { | ||
((JavascriptExecutor) driver).executeScript("lambda-status=failed"); | ||
} | ||
|
||
// Set the link to the video | ||
SessionId sessionId = RemoteDriver.of(driver).getSessionId(); | ||
String publicUrl = LambdaTestVideoLink.forEnvironment(environmentVariables).videoUrlForSession(sessionId.toString()); | ||
testOutcome.setLink(new ExternalLink(publicUrl, "LambdaTest")); | ||
|
||
} | ||
|
||
public boolean isActivated(EnvironmentVariables environmentVariables) { | ||
return LambdaTestConfiguration.isActiveFor(environmentVariables); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
serenity-lambdatest/src/main/java/net/serenitybdd/lambdatest/BeforeALambdaTestScenario.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.serenitybdd.lambdatest; | ||
|
||
import net.serenitybdd.core.environment.EnvironmentSpecificConfiguration; | ||
import net.serenitybdd.core.webdriver.enhancers.AfterAWebdriverScenario; | ||
import net.serenitybdd.core.webdriver.enhancers.BeforeAWebdriverScenario; | ||
import net.thucydides.core.model.TestOutcome; | ||
import net.thucydides.core.util.EnvironmentVariables; | ||
import net.thucydides.core.webdriver.SupportedWebDriver; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
|
||
public class BeforeALambdaTestScenario implements BeforeAWebdriverScenario { | ||
|
||
@Override | ||
public DesiredCapabilities apply(EnvironmentVariables environmentVariables, | ||
SupportedWebDriver driver, | ||
TestOutcome testOutcome, | ||
DesiredCapabilities capabilities) { | ||
if (!LambdaTestConfiguration.isActiveFor(environmentVariables)) { | ||
return capabilities; | ||
} | ||
capabilities.setCapability("build", testOutcome.getStoryTitle()); | ||
capabilities.setCapability("name", testOutcome.getCompleteName()); | ||
return capabilities; | ||
} | ||
|
||
public boolean isActivated(EnvironmentVariables environmentVariables) { | ||
return !EnvironmentSpecificConfiguration.from(environmentVariables) | ||
.getPropertiesWithPrefix("browserstack").isEmpty(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
serenity-lambdatest/src/main/java/net/serenitybdd/lambdatest/LambdaTestAuthToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.serenitybdd.lambdatest; | ||
|
||
import net.serenitybdd.core.environment.EnvironmentSpecificConfiguration; | ||
import net.thucydides.core.util.EnvironmentVariables; | ||
|
||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
class LambdaTestAuthToken { | ||
static String usingCredentialsFrom(EnvironmentVariables environmentVariables) { | ||
String username = EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty("lambdatest.user").orElse(""); | ||
String accessKey = EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty("lambdatest.key").orElse(""); | ||
try { | ||
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); | ||
String usernameAndAccessKey = username + ":" + accessKey; | ||
messageDigest.update(usernameAndAccessKey.getBytes(),0,usernameAndAccessKey.length()); | ||
return new BigInteger(1,messageDigest.digest()).toString(16); | ||
} catch (NoSuchAlgorithmException e) { | ||
return ""; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
serenity-lambdatest/src/main/java/net/serenitybdd/lambdatest/LambdaTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.serenitybdd.lambdatest; | ||
|
||
import net.serenitybdd.core.environment.EnvironmentSpecificConfiguration; | ||
import net.thucydides.core.util.EnvironmentVariables; | ||
|
||
class LambdaTestConfiguration { | ||
static boolean isActiveFor(EnvironmentVariables environmentVariables) { | ||
return EnvironmentSpecificConfiguration.from(environmentVariables) | ||
.getOptionalProperty("webdriver.remote.url").orElse("") | ||
.contains("lambdatest"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
serenity-lambdatest/src/main/java/net/serenitybdd/lambdatest/LambdaTestVideoLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.serenitybdd.lambdatest; | ||
|
||
import net.thucydides.core.util.EnvironmentVariables; | ||
|
||
public class LambdaTestVideoLink { | ||
private final String PUBLIC_URL = "https://automation.lambdatest.com/public/video?testID=%s&auth=%s"; | ||
|
||
private final EnvironmentVariables environmentVariables; | ||
|
||
private LambdaTestVideoLink(EnvironmentVariables environmentVariables) { | ||
this.environmentVariables = environmentVariables; | ||
} | ||
|
||
public static LambdaTestVideoLink forEnvironment(EnvironmentVariables environmentVariables) { | ||
return new LambdaTestVideoLink(environmentVariables); | ||
} | ||
|
||
public String videoUrlForSession(String sessionId) { | ||
String authToken = LambdaTestAuthToken.usingCredentialsFrom(environmentVariables); | ||
return String.format(PUBLIC_URL, sessionId, authToken); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...lambdatest/src/test/java/net/serenitybdd/lambdatest/WhenAddingLambdaTestCapabilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
package net.serenitybdd.lambdatest; | ||
|
||
import net.serenitybdd.core.webdriver.OverrideDriverCapabilities; | ||
import net.thucydides.core.model.Story; | ||
import net.thucydides.core.model.TestOutcome; | ||
import net.thucydides.core.util.EnvironmentVariables; | ||
import net.thucydides.core.util.MockEnvironmentVariables; | ||
import net.thucydides.core.webdriver.SupportedWebDriver; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class WhenAddingLambdaTestCapabilities { | ||
|
||
EnvironmentVariables environmentVariables; | ||
|
||
private static final TestOutcome SAMPLE_TEST_OUTCOME = TestOutcome.forTestInStory("sample_test", Story.called("Sample story")); | ||
|
||
BeforeALambdaTestScenario beforeAScenario; | ||
DesiredCapabilities capabilities; | ||
|
||
@BeforeEach | ||
public void prepareSession() { | ||
OverrideDriverCapabilities.clear(); | ||
beforeAScenario = new BeforeALambdaTestScenario(); | ||
capabilities = new DesiredCapabilities(); | ||
environmentVariables = new MockEnvironmentVariables(); | ||
} | ||
|
||
|
||
@Test | ||
public void theBrowserNameShouldBeAddedDirectlyToTheCapability() { | ||
|
||
environmentVariables.setProperty("webdriver.remote.url", "https://hub-cloud.lambdatest.com/wd/hub"); | ||
|
||
beforeAScenario.apply(environmentVariables, SupportedWebDriver.REMOTE, SAMPLE_TEST_OUTCOME, capabilities); | ||
|
||
assertThat(capabilities.getCapability("name")).isEqualTo("Sample story:sample_test"); | ||
} | ||
|
||
@Test | ||
public void theRemoteUrlMustContainLambdaTest() { | ||
|
||
environmentVariables.setProperty("webdriver.remote.url", "https://hub-cloud.saucelabs.com/wd/hub"); | ||
|
||
beforeAScenario.apply(environmentVariables, SupportedWebDriver.REMOTE, SAMPLE_TEST_OUTCOME, capabilities); | ||
|
||
assertThat(capabilities.getCapability("name")).isNull(); | ||
} | ||
|
||
@Test | ||
public void theAuthTokenUsesTheUsernameAndAPIKey() { | ||
|
||
environmentVariables.setProperty("lambdatest.user", "my.username"); | ||
environmentVariables.setProperty("lambdatest.key", "XXXXXXXXXXXX"); | ||
String expectedDigest = DigestUtils.md5Hex("my.username:XXXXXXXXXXXX".getBytes()); | ||
|
||
String authToken = LambdaTestAuthToken.usingCredentialsFrom(environmentVariables); | ||
|
||
assertThat(authToken).isEqualTo(expectedDigest); | ||
} | ||
|
||
@Test | ||
public void theVideoLinkCombinesTheSessionIdAndTheAuthToken() { | ||
|
||
environmentVariables.setProperty("lambdatest.user", "my.username"); | ||
environmentVariables.setProperty("lambdatest.key", "XXXXXXXXXXXX"); | ||
|
||
String sessionId = "HJKXM-RHZL1-SVPWY-AB8X6"; | ||
String digest = DigestUtils.md5Hex("my.username:XXXXXXXXXXXX".getBytes()); | ||
|
||
String expectedUrl = "https://automation.lambdatest.com/public/video?testID=HJKXM-RHZL1-SVPWY-AB8X6&auth=" + digest; | ||
|
||
assertThat(LambdaTestVideoLink.forEnvironment(environmentVariables).videoUrlForSession(sessionId)) | ||
.isEqualTo(expectedUrl); | ||
} | ||
} |