Skip to content

Commit

Permalink
remove sauce bindings from selenium examples (#158)
Browse files Browse the repository at this point in the history
* remove sauce bindings from selenium examples

* fix formatting

* fix base test page
  • Loading branch information
titusfortner authored Oct 18, 2023
1 parent b87ffb4 commit 98aa73d
Show file tree
Hide file tree
Showing 30 changed files with 364 additions and 549 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/selenium-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ jobs:
run: git --no-pager diff --exit-code

build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v1
with:
java-version: 11
- name: Run tests
- name: Run JUnit 5 tests
working-directory: ./selenium-examples
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
run: mvn test -pl selenium-examples -X
run: mvn test
62 changes: 7 additions & 55 deletions selenium-examples/README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,18 @@
# Selenium examples
This folder contains Selenium examples

## Examples
This is the default project for all Selenium and Sauce Labs features

- [Accessibility Test with Sauce Bindings](../selenium-examples/src/test/java/com/saucedemo/accessibility/SauceBindingsExampleTest.java)
- [Accessibility Test with Deque Axe](../selenium-examples/src/test/java/com/saucedemo/accessibility/DequeAxeExampleTest.java)
- [Simple web test, Junit 5](../selenium-examples/src/test/java/com/saucedemo/JUnit5W3CChromeTest.java)
- [Simple Headless test, TestNG](../selenium-examples/src/test/java/com/saucedemo/SampleHeadlessSauceTest.java)
- [Sauce Status Updates w/ JUnit 3](../selenium-examples/src/test/java/com/saucedemo/JUnit3UpdateSauceStatusTest.java)
* Demo package for seeing Sauce Demo site tests
* Sauce Features package for demos of executing special Sauce Labs features
* Selenium Features package for demos of executing Selenium on Sauce Labs

## How to run tests
It is also to demonstrate how to run tests in parallel with Selenium 4 and JUnit 5. See [pom.xml](pom.xml)

### JUnit 5 test
## Execute tests in parallel:
```bash
cd selenium-examples
mvn test -Dtest=JUnit5W3CChromeTest
mvn test -Dsurefire.parallel=20
```

Your output will look like this if done correctly

```java
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.saucelabs:selenium-examples >-------------------
[INFO] Building selenium-examples 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ selenium-examples ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ selenium-examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ selenium-examples ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/nikolayadvolodkin/Documents/source/java/demo-java/selenium-examples/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ selenium-examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ selenium-examples ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.saucedemo.JUnit5W3CChromeTest
Apr 05, 2021 1:48:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
SauceOnDemandSessionID=c36e76bd99964b29824e3c9597558659 job-name=Junit5W3CChromeTest
Test Job Link: https://app.saucelabs.com/tests/c36e76bd99964b29824e3c9597558659
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.44 s - in com.saucedemo.JUnit5W3CChromeTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.728 s
[INFO] Finished at: 2021-04-05T13:48:14-04:00

```

23 changes: 15 additions & 8 deletions selenium-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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>

<groupId>com.com.saucelabs</groupId>
<groupId>com.saucelabs</groupId>
<artifactId>selenium_examples</artifactId>
<version>1.0-SNAPSHOT</version>

Expand All @@ -20,23 +20,30 @@
</licenses>

<properties>
<surefire.parallel>1</surefire.parallel>
<surefire.parallel>10</surefire.parallel>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucebindings-junit5</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.deque.html.axe-core</groupId>
<artifactId>selenium</artifactId>
<version>4.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.titusfortner</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.saucedemo.selenium;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
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.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

public class TestBase {

public WebDriver driver;

@RegisterExtension public TestBase.SauceTestWatcher watcher = new TestBase.SauceTestWatcher();
protected TestInfo testInfo;
protected SessionId id;

public void startChromeSession(TestInfo testInfo) {
startSession(testInfo, new ChromeOptions());
}

public void startFirefoxSession(TestInfo testInfo) {
startSession(testInfo, new FirefoxOptions());
}

public void startSession(TestInfo testInfo, Capabilities options) {
startSession(testInfo, options, new HashMap<>());
}

protected void startSession(
TestInfo testInfo, Capabilities options, Map<String, Object> sauceOptions) {
Objects.requireNonNull(testInfo, "testInfo cannot be null");
Objects.requireNonNull(options, "options cannot be null");
Objects.requireNonNull(sauceOptions, "sauceOptions cannot be null");

this.testInfo = testInfo;

sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
sauceOptions.put("name", testInfo.getDisplayName());
((MutableCapabilities) options).setCapability("sauce:options", sauceOptions);

URL url;
try {
url = new URL("https://ondemand.us-west-1.saucelabs.com/wd/hub");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

driver = new RemoteWebDriver(url, options);
this.id = ((RemoteWebDriver) driver).getSessionId();
}

public class SauceTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
printResults();
try {
((JavascriptExecutor) driver).executeScript("sauce:job-result=passed");
driver.quit();
} catch (Exception e) {
System.out.println("problem with using driver: " + e);
}
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
printResults();

try {
((JavascriptExecutor) driver).executeScript("sauce:job-result=failed");
driver.quit();
} catch (Exception e) {
System.out.println("problem with using driver: " + e);
}
}

public void printResults() {
String sauceReporter =
String.format("SauceOnDemandSessionID=%s job-name=%s", id, testInfo.getDisplayName());
String sauceTestLink = String.format("Test Job Link: https://app.saucelabs.com/tests/%s", id);
System.out.print(sauceReporter + "\n" + sauceTestLink + "\n");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.saucedemo.selenium.login;
package com.saucedemo.selenium.demo;

import com.saucedemo.selenium.SeleniumTestBase;
import com.saucedemo.selenium.TestBase;
import java.time.Duration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -11,11 +11,11 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

/** Login tests with Selenium. */
public class SeleniumLoginTest extends SeleniumTestBase {
public class LoginTest extends TestBase {

@BeforeEach
public void setup(TestInfo testInfo) {
basicSetup(testInfo);
startChromeSession(testInfo);
}

@DisplayName("Swag Labs Login with Selenium")
Expand Down
Loading

0 comments on commit 98aa73d

Please sign in to comment.