Skip to content

Commit

Permalink
New lambdatest integration
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Aug 23, 2021
1 parent a989cca commit 9d4b4df
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@
<module>serenity-spring</module>
<module>serenity-browsermob-plugin</module>
<module>serenity-browserstack</module>
<module>serenity-lambdatest</module>
<module>serenity-crossbrowsertesting</module>
<module>serenity-maven-plugin</module>
<module>serenity-cli</module>
Expand Down
49 changes: 49 additions & 0 deletions serenity-lambdatest/pom.xml
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>
4 changes: 4 additions & 0 deletions serenity-lambdatest/readme.md
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:
* ``
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);
}
}
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();
}

}
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 "";
}
}
}
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");
}
}
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);
}

}
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);
}
}

0 comments on commit 9d4b4df

Please sign in to comment.