Skip to content

Commit

Permalink
run in parallel on Sauce Labs
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 29, 2023
1 parent afddfd3 commit e09ca31
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
33 changes: 31 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
</developers>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -51,4 +50,34 @@
<version>1.3.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 12
junit.jupiter.execution.parallel.config.fixed.max-pool-size = 12
</configurationParameters>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
55 changes: 50 additions & 5 deletions src/test/java/com/saucelabs/scalable_tests/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
package com.saucelabs.scalable_tests;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class BaseTest {
WebDriver driver = null;
WebDriver driver;

@RegisterExtension
public MyTestWatcher myTestWatcher = new MyTestWatcher();

@BeforeAll
public static void toggleExecution() {
// This would normally be toggled via CI tool ENV or similar
System.setProperty("SELENIUM_PLATFORM", "SAUCE");
}

@BeforeEach
public void setUp() {
driver = new ChromeDriver();
public void setUp(TestInfo testinfo) throws MalformedURLException {
driver = isLocal() ? runLocal() : runSauce(testinfo);
}

public boolean isLocal() {
return "LOCAL".equals(System.getProperty("SELENIUM_PLATFORM"));
}

private WebDriver runLocal() {
return new ChromeDriver();
}

private WebDriver runSauce(TestInfo testinfo) throws MalformedURLException {
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("latest");
Map<String, Object> sauceOptions = new HashMap<>();
sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
sauceOptions.put("name", testinfo.getDisplayName());
browserOptions.setCapability("sauce:options", sauceOptions);

URL url = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
return new RemoteWebDriver(url, browserOptions);
}

public class MyTestWatcher implements TestWatcher {
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
try {
System.out.println("Test Failed!");
if (isLocal()) {
System.out.println("Test Failed!");
} else {
((JavascriptExecutor) driver).executeScript("sauce:job-result=failed");
}
} catch (Exception ignored) {
} finally {
driver.quit();
Expand All @@ -34,7 +75,11 @@ public void testFailed(ExtensionContext context, Throwable cause) {
@Override
public void testSuccessful(ExtensionContext context) {
try {
System.out.println("Test Passed!");
if (isLocal()) {
System.out.println("Test Passed!");
} else {
((JavascriptExecutor) driver).executeScript("sauce:job-result=passed");
}
} catch (Exception ignored) {
} finally {
driver.quit();
Expand Down

0 comments on commit e09ca31

Please sign in to comment.