Skip to content

Commit 5f8cd01

Browse files
authored
[java] add a practice test suite to simulate real world usage (#344)
* [java] add a practice test suite to simulate real world usage * [rename package to exampleservice
1 parent 717c4f5 commit 5f8cd01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3766
-0
lines changed

.github/workflows/integration.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This workflow will build a Java project with Maven
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Integration Tests
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
paths:
10+
- 'java/**'
11+
pull_request:
12+
branches: [ main ]
13+
paths:
14+
- 'java/**'
15+
workflow_dispatch:
16+
17+
jobs:
18+
formatting:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
- uses: axel-op/googlejavaformat-action@v3
23+
with:
24+
args: "--replace"
25+
files: "java/test-project/**/*.java"
26+
skip-commit: true
27+
- name: Print diffs
28+
run: git --no-pager diff --exit-code
29+
integration:
30+
runs-on: ubuntu-latest
31+
strategy:
32+
matrix:
33+
profile: [junit4, junit5, testng]
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Set up JDK 11
37+
uses: actions/setup-java@v4
38+
with:
39+
java-version: 11
40+
distribution: 'temurin'
41+
- name: Remove driver directories
42+
run: |
43+
sudo rm -rf $CHROMEWEBDRIVER $EDGEWEBDRIVER $GECKOWEBDRIVER
44+
- name: Install main bindings snapshot
45+
run: cd java/main && mvn clean package install -DskipTests -Dgpg.skip=true
46+
- name: Install Junit5 bindings snapshot
47+
run: cd java/junit5 && mvn clean package install -DskipTests -Dgpg.skip=true
48+
- name: Install Junit5 bindings snapshot
49+
run: cd java/junit4 && mvn clean package install -DskipTests -Dgpg.skip=true
50+
- name: Install TestNG bindings snapshot
51+
run: cd java/testng && mvn clean package install -DskipTests -Dgpg.skip=true
52+
- name: Test with ${{ matrix.profile }}
53+
env:
54+
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
55+
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
56+
run: cd java/test-project && mvn clean test -P${{ matrix.profile }}

java/test-project/pom.xml

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.example</groupId>
8+
<artifactId>test-project</artifactId>
9+
<version>0.0.1</version>
10+
11+
<properties>
12+
<surefire.parallel>20</surefire.parallel>
13+
<maven.compiler.source>11</maven.compiler.source>
14+
<maven.compiler.target>11</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.titusfortner</groupId>
21+
<artifactId>selenium-logger</artifactId>
22+
<version>2.4.0</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.projectlombok</groupId>
26+
<artifactId>lombok</artifactId>
27+
<version>1.18.30</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.saucelabs</groupId>
32+
<artifactId>saucebindings-junit4</artifactId>
33+
<version>2.0.0-beta.2-SNAPSHOT</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.saucelabs</groupId>
38+
<artifactId>saucebindings-testng</artifactId>
39+
<version>2.0.0-beta.2-SNAPSHOT</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.saucelabs</groupId>
44+
<artifactId>saucebindings-junit5</artifactId>
45+
<version>2.0.0-beta.2-SNAPSHOT</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.saucelabs</groupId>
50+
<artifactId>sauce_bindings</artifactId>
51+
<version>2.0.0-beta.2-SNAPSHOT</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<profiles>
56+
<profile>
57+
<id>junit4</id>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>3.5.1</version>
64+
<dependencies>
65+
<dependency>
66+
<groupId>org.apache.maven.surefire</groupId>
67+
<artifactId>surefire-junit47</artifactId>
68+
<version>3.5.2</version>
69+
</dependency>
70+
</dependencies>
71+
<configuration>
72+
<includes>
73+
<include>**/junit4/**</include>
74+
</includes>
75+
<systemPropertyVariables>
76+
<sauce.enabled>true</sauce.enabled>
77+
</systemPropertyVariables>
78+
<parallel>all</parallel>
79+
<threadCountMethods>${surefire.parallel}</threadCountMethods>
80+
<useUnlimitedThreads>true</useUnlimitedThreads>
81+
<redirectTestOutputToFile>false</redirectTestOutputToFile>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</profile>
87+
<profile>
88+
<id>junit5</id>
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-surefire-plugin</artifactId>
94+
<version>3.5.1</version>
95+
<configuration>
96+
<systemPropertyVariables>
97+
<sauce.enabled>true</sauce.enabled>
98+
</systemPropertyVariables>
99+
<includes>
100+
<include>**/junit5/**</include>
101+
</includes>
102+
<properties>
103+
<configurationParameters>
104+
junit.jupiter.execution.parallel.enabled = true
105+
junit.jupiter.execution.parallel.mode.default = concurrent
106+
junit.jupiter.execution.parallel.config.strategy = fixed
107+
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
108+
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
109+
</configurationParameters>
110+
</properties>
111+
</configuration>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</profile>
116+
<profile>
117+
<id>testng</id>
118+
<build>
119+
<plugins>
120+
<plugin>
121+
<groupId>org.apache.maven.plugins</groupId>
122+
<artifactId>maven-surefire-plugin</artifactId>
123+
<version>3.5.1</version>
124+
<dependencies>
125+
<dependency>
126+
<groupId>org.apache.maven.surefire</groupId>
127+
<artifactId>surefire-testng</artifactId>
128+
<version>3.5.2</version>
129+
</dependency>
130+
</dependencies>
131+
<configuration>
132+
<systemPropertyVariables>
133+
<sauce.enabled>true</sauce.enabled>
134+
</systemPropertyVariables>
135+
<includes>
136+
<include>**/testng/**</include>
137+
</includes>
138+
<parallel>methods</parallel>
139+
<threadCount>50</threadCount>
140+
<redirectTestOutputToFile>false</redirectTestOutputToFile>
141+
</configuration>
142+
</plugin>
143+
</plugins>
144+
</build>
145+
</profile>
146+
</profiles>
147+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.exampleservice.junit4;
2+
3+
import com.exampleservice.pageobjects.NavBar;
4+
import com.exampleservice.pageobjects.user.AccountPage;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.openqa.selenium.WebDriverException;
11+
12+
public class AccountPageTest extends BaseTest {
13+
private AccountPage accountPage;
14+
15+
@Before
16+
public void navigate() {
17+
accountPage = new AccountPage(driver);
18+
accountPage.open();
19+
}
20+
21+
@Category(ContentTests.class)
22+
@Test
23+
public void pageCheck() {
24+
Assertions.assertDoesNotThrow(
25+
() -> accountPage.validateContent("manage your profile, favorites and orders"));
26+
}
27+
28+
@Category(ContentTests.class)
29+
@Test
30+
public void hasNavBar() {
31+
try {
32+
new NavBar(driver).isDisplayed();
33+
} catch (WebDriverException e) {
34+
Assert.fail(e.getMessage());
35+
}
36+
}
37+
38+
@Category(NavigationTests.class)
39+
@Test
40+
public void navigateToFavorites() {
41+
try {
42+
accountPage.navigateToFavorites();
43+
} catch (WebDriverException e) {
44+
Assert.fail(e.getMessage());
45+
}
46+
}
47+
48+
@Category(NavigationTests.class)
49+
@Test
50+
public void navigateToProfile() {
51+
try {
52+
accountPage.navigateToProfile();
53+
} catch (WebDriverException e) {
54+
Assert.fail(e.getMessage());
55+
}
56+
}
57+
58+
@Category(NavigationTests.class)
59+
@Test
60+
public void navigateToInvoices() {
61+
try {
62+
accountPage.navigateToInvoices();
63+
} catch (WebDriverException e) {
64+
Assert.fail(e.getMessage());
65+
}
66+
}
67+
68+
@Category(NavigationTests.class)
69+
@Test
70+
public void navigateToMessages() {
71+
try {
72+
accountPage.navigateToMessages();
73+
} catch (WebDriverException e) {
74+
Assert.fail(e.getMessage());
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.exampleservice.junit4;
2+
3+
import com.saucelabs.saucebindings.SauceSession;
4+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
5+
import java.time.Duration;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.rules.TestWatcher;
9+
import org.junit.runner.Description;
10+
import org.openqa.selenium.WebDriver;
11+
import org.openqa.selenium.chrome.ChromeDriver;
12+
import org.openqa.selenium.chrome.ChromeOptions;
13+
14+
public class BaseTest {
15+
WebDriver driver;
16+
SauceSession session;
17+
18+
@Rule
19+
public SauceBindingsWatcher sauceWatcher =
20+
SauceBindingsWatcher.builder().withCapabilities(getChromeOptions()).build();
21+
22+
@Rule public TestWatcher localWatcher = new LocalTestWatcher();
23+
24+
@Before
25+
public void storeVariables() {
26+
this.session = sauceWatcher.getSession();
27+
this.driver =
28+
SauceSession.isEnabled() ? sauceWatcher.getDriver() : new ChromeDriver(getChromeOptions());
29+
}
30+
31+
private ChromeOptions getChromeOptions() {
32+
ChromeOptions options = new ChromeOptions();
33+
options.setImplicitWaitTimeout(Duration.ofSeconds(2));
34+
return options;
35+
}
36+
37+
public interface ContentTests {}
38+
39+
public interface NavigationTests {}
40+
41+
public interface FeatureTests {}
42+
43+
private class LocalTestWatcher extends TestWatcher {
44+
@Override
45+
protected void failed(Throwable e, Description description) {
46+
if (!SauceSession.isEnabled()) {
47+
driver.quit();
48+
}
49+
}
50+
51+
@Override
52+
protected void succeeded(Description description) {
53+
if (!SauceSession.isEnabled()) {
54+
driver.quit();
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)