Skip to content

Commit 7aaf524

Browse files
committed
[java] get junit4 and junit5 examples to match better
1 parent 4a6dcec commit 7aaf524

File tree

12 files changed

+97
-80
lines changed

12 files changed

+97
-80
lines changed

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/AccessibilityExample.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.deque.html.axecore.results.Results;
44
import com.saucelabs.saucebindings.SauceSession;
55
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
6-
import org.junit.Assert;
76
import org.junit.Before;
87
import org.junit.Rule;
98
import org.junit.Test;
@@ -13,15 +12,15 @@ public class AccessibilityExample {
1312
private SauceSession session;
1413
private WebDriver driver;
1514

16-
// 1. Use the SauceBindingsWatcher rule
15+
// 1. Use the SauceBindingsWatcher rule with defaults
1716
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
1817

19-
// 2. Enable SauceBindingsWatcher rule
18+
// 2. Enable this watcher in the static block
2019
static {
2120
SauceBindingsWatcher.enable();
2221
}
2322

24-
// 3. Get variables created by Watcher
23+
// 3. Get variables created by the watcher
2524
@Before
2625
public void storeVariables() {
2726
this.session = sauceWatcher.getSession();
@@ -30,13 +29,10 @@ public void storeVariables() {
3029

3130
@Test
3231
public void startSession() {
33-
// 4. Use the driver in your tests just like normal
3432
driver.get("https://www.saucedemo.com/");
3533

36-
// 5. Get and assert on accessibility results
34+
// 4. store the accessibility results into an object
3735
Results results = session.getAccessibilityResults();
38-
Assert.assertEquals(3, results.getViolations().size());
39-
40-
// 6. Session is stopped and results are sent to Sauce Labs automatically by the superclass
36+
// if you want, you can assert on the accessibilityResults
4137
}
4238
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/CapabilitiesExample.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CapabilitiesExample {
1515
WebDriver driver;
1616
SauceSession session;
1717

18-
// 1. Create Selenium Capabilities instance in static method
18+
// 1. Create Selenium Capabilities instance in a static method
1919
private static Capabilities createCapabilities() {
2020
SafariOptions browserOptions = new SafariOptions();
2121
browserOptions.setPlatformName("macOS 12");
@@ -26,17 +26,17 @@ private static Capabilities createCapabilities() {
2626
return browserOptions;
2727
}
2828

29-
// 2. Pass these options to the SauceBindingsWatcher rule
29+
// 2. Use the SauceBindingsWatcher rule with these capabilities
3030
@Rule
3131
public SauceBindingsWatcher sauceWatcher =
32-
SauceBindingsWatcher.builder().withCapabilities(createCapabilities()).build();
32+
SauceBindingsWatcher.builder().withCapabilities(createCapabilities()).build();
3333

34-
// 3. Enable SauceBindingsWatcher rule
34+
// 3. Enable this watcher in the static block
3535
static {
3636
SauceBindingsWatcher.enable();
3737
}
3838

39-
// 4. Get variables created by Watcher
39+
// 4. Get variables created by the watcher
4040
@Before
4141
public void storeVariables() {
4242
this.session = sauceWatcher.getSession();
@@ -50,7 +50,5 @@ public void basicOptions() {
5050

5151
// 6. Use the driver instance to do Selenium things
5252
driver.get("https://www.saucedemo.com/");
53-
54-
// 7. Watcher does all teardown activities
5553
}
5654
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/DataCenterExample.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ public class DataCenterExample {
1212
private SauceSession session;
1313
private WebDriver driver;
1414

15-
// 1. Pass in desired Datacenter to the SauceBindingsWatcher rule
15+
// 1. Use the SauceBindingsWatcher rule with specific DataCenter
1616
@Rule
1717
public SauceBindingsWatcher sauceWatcher =
1818
SauceBindingsWatcher.builder().withDataCenter(DataCenter.EU_CENTRAL).build();
1919

20-
// 2. Enable SauceBindingsWatcher rule
20+
// 2. Enable this watcher in the static block
2121
static {
2222
SauceBindingsWatcher.enable();
2323
}
2424

25-
// 3. Get variables created by Watcher
25+
// 3. Get variables created by the watcher
2626
@Before
2727
public void storeVariables() {
2828
this.session = sauceWatcher.getSession();
@@ -36,7 +36,5 @@ public void startSession() {
3636

3737
// 5. Use the driver instance to do Selenium things
3838
driver.get("https://www.saucedemo.com/");
39-
40-
// 6. Watcher does all teardown activities
4139
}
4240
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/QuickStartExample.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class QuickStartExample {
1414
// 1. Use the SauceBindingsWatcher rule
1515
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
1616

17-
// 2. Enable SauceBindingsWatcher rule
17+
// 2. Enable this watcher in the static block
1818
static {
1919
SauceBindingsWatcher.enable();
2020
}
2121

22-
// 3. Get variables created by Watcher
22+
// 3. Get variables created by the watcher
2323
@Before
2424
public void storeVariables() {
2525
this.session = sauceWatcher.getSession();
@@ -33,7 +33,5 @@ public void startSession() {
3333

3434
// 5. Use the driver instance to do Selenium things
3535
driver.get("https://www.saucedemo.com/");
36-
37-
// 6. Watcher does all teardown activities
3836
}
3937
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/RunLocalExample.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import org.openqa.selenium.chrome.ChromeDriver;
1212

1313
public class RunLocalExample {
14-
private SauceSession session;
1514
private WebDriver driver;
15+
private SauceSession session;
1616

17-
// 1. Set multiple rules for when Sauce is and isn't enabled
17+
// 1. Register Watchers
18+
// Register Sauce Bindings watcher with defaults
1819
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
20+
// Register additional test watcher(s) for local execution
1921
@Rule public TestWatcher localWatcher = new LocalTestWatcher();
2022

2123
// Sauce Labs execution is disabled by default,
@@ -26,7 +28,7 @@ public class RunLocalExample {
2628
// SauceBindingsWatcher.enable();
2729
}
2830

29-
// 2. Start driver if running locally
31+
// Only start driver if Sauce Watcher is not enabled
3032
@Before
3133
public void storeVariables() {
3234
this.session = sauceWatcher.getSession();

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/SauceOptionsExample.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,35 @@
1010
import org.junit.Rule;
1111
import org.junit.Test;
1212
import org.openqa.selenium.WebDriver;
13+
import org.openqa.selenium.chrome.ChromeOptions;
1314

1415
public class SauceOptionsExample {
1516
WebDriver driver;
1617
SauceSession session;
1718

1819
// 1. Create SauceOptions instance in static method
1920
public static SauceOptions createSauceOptions() {
20-
return SauceOptions.firefox()
21-
.setBrowserVersion("127.0")
22-
.setPlatformName(SaucePlatform.WINDOWS_10)
23-
.setUnhandledPromptBehavior(UnhandledPromptBehavior.IGNORE)
24-
.setIdleTimeout(Duration.ofSeconds(45))
25-
.setTimeZone("Alaska")
26-
.build();
21+
ChromeOptions chromeOptions = new ChromeOptions();
22+
chromeOptions.addArguments("--hide-scrollbars");
23+
24+
return SauceOptions.chrome(chromeOptions)
25+
.setPlatformName(SaucePlatform.MAC_CATALINA)
26+
.setUnhandledPromptBehavior(UnhandledPromptBehavior.IGNORE)
27+
.setIdleTimeout(Duration.ofSeconds(30))
28+
.build();
2729
}
2830

29-
// 2. Pass these options to the SauceBindingsWatcher rule
31+
// 2. Create SauceBindingsWatcher rule with these options
3032
@Rule
3133
public SauceBindingsWatcher sauceWatcher =
3234
SauceBindingsWatcher.builder().withSauceOptions(createSauceOptions()).build();
3335

34-
// 3. Enable SauceBindingsWatcher rule
36+
// 3. Enable this watcher in the static block
3537
static {
3638
SauceBindingsWatcher.enable();
3739
}
3840

39-
// 4. Get variables created by Watcher
41+
// 4. Get variables created by the watcher
4042
@Before
4143
public void storeVariables() {
4244
this.session = sauceWatcher.getSession();
@@ -50,7 +52,5 @@ public void basicOptions() {
5052

5153
// 6. Use the driver instance to do Selenium things
5254
driver.get("https://www.saucedemo.com/");
53-
54-
// 7. Watcher does all teardown activities
5555
}
5656
}

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/examples/AccessibilityExample.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ public class AccessibilityExample {
1212
WebDriver driver;
1313
SauceSession session;
1414

15-
// Register extension
15+
// 1. Register Sauce Bindings extension with defaults
1616
@RegisterExtension static SauceBindingsExtension sauceExtension = new SauceBindingsExtension();
1717

18-
// Enable extension (this also can be done by running with -Dsauce.enabled=true)
18+
// 2. Enable extension (this also can be done by running with -Dsauce.enabled=true)
1919
static {
2020
sauceExtension.enable();
2121
}
2222

23+
// 3. Get variables created by the Sauce Bindings extension
2324
@BeforeEach
2425
public void setUp(SauceSession session) {
2526
this.session = session;
@@ -28,9 +29,12 @@ public void setUp(SauceSession session) {
2829

2930
@Test
3031
public void accessibilityExample() {
32+
// 4. Use the driver instance to do Selenium things
3133
driver.get("https://www.saucedemo.com/");
32-
// store the accessibility results into an object
34+
35+
// 5. store the accessibility results into an object
3336
Results accessibilityResults = session.getAccessibilityResults();
34-
// if you want, you can assert on the accessibilityResults
37+
38+
// 6. you can assert on the accessibilityResults as desired
3539
}
3640
}

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/examples/CapabilitiesExample.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,40 @@ public class CapabilitiesExample {
1515
WebDriver driver;
1616
SauceSession session;
1717

18-
// Register extension with Selenium capabilities instance
18+
// 1. Create static method with Selenium Capabilities
19+
private static Capabilities getCapabilities() {
20+
SafariOptions browserOptions = new SafariOptions();
21+
browserOptions.setPlatformName("macOS 12");
22+
browserOptions.setBrowserVersion("latest");
23+
Map<String, Object> sauceOptions = new HashMap<>();
24+
sauceOptions.put("idleTimeout", 30);
25+
browserOptions.setCapability("sauce:options", sauceOptions);
26+
return browserOptions;
27+
}
28+
29+
// 2. Register Sauce Bindings extension with these capabilities
1930
@RegisterExtension
2031
static SauceBindingsExtension sauceExtension =
2132
SauceBindingsExtension.builder().withCapabilities(getCapabilities()).build();
2233

23-
// Enable extension (this also can be done by running with -Dsauce.enabled=true)
34+
// 3. Enable extension (this also can be done by running with -Dsauce.enabled=true)
2435
static {
2536
sauceExtension.enable();
2637
}
2738

39+
// 4. Get variables created by the Sauce Bindings extension
2840
@BeforeEach
2941
public void setUp(SauceSession session, WebDriver driver) {
3042
this.session = session;
3143
this.driver = driver;
3244
}
3345

34-
private static Capabilities getCapabilities() {
35-
SafariOptions browserOptions = new SafariOptions();
36-
browserOptions.setPlatformName("macOS 12");
37-
browserOptions.setBrowserVersion("latest");
38-
Map<String, Object> sauceOptions = new HashMap<>();
39-
sauceOptions.put("idleTimeout", 30);
40-
browserOptions.setCapability("sauce:options", sauceOptions);
41-
return browserOptions;
42-
}
43-
4446
@Test
4547
public void capabilitiesExample() {
48+
// 5. Use the session instance to do Sauce Labs things
4649
session.annotate("Navigating to Swag Labs");
50+
51+
// 6. Use the driver instance to do Selenium things
4752
driver.get("https://www.saucedemo.com/");
4853
}
4954
}

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/examples/DataCenterExample.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ public class DataCenterExample {
1212
WebDriver driver;
1313
SauceSession session;
1414

15-
// Register extension with custom data center
15+
// 1. Register Sauce Bindings extension with custom data center
1616
@RegisterExtension
1717
static SauceBindingsExtension sauceExtension =
1818
SauceBindingsExtension.builder().withDataCenter(DataCenter.EU_CENTRAL).build();
1919

20-
// Enable extension (this also can be done by running with -Dsauce.enabled=true)
20+
// 2. Enable extension (this also can be done by running with -Dsauce.enabled=true)
2121
static {
2222
sauceExtension.enable();
2323
}
2424

25+
// 3. Get variables created by the Sauce Bindings extension
2526
@BeforeEach
2627
public void setUp(SauceSession session, WebDriver driver) {
2728
this.session = session;
@@ -30,7 +31,10 @@ public void setUp(SauceSession session, WebDriver driver) {
3031

3132
@Test
3233
public void dataCenterExample() {
34+
// 4. Use the session instance to do Sauce Labs things
3335
session.annotate("Navigating to Swag Labs");
36+
37+
// 5. Use the driver instance to do Selenium things
3438
driver.get("https://www.saucedemo.com/");
3539
}
3640
}

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/examples/QuickStartExample.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ public class QuickStartExample {
1111
WebDriver driver;
1212
SauceSession session;
1313

14-
// Register extension with defaults
14+
// 1. Register Sauce Bindings extension with defaults
1515
@RegisterExtension static SauceBindingsExtension sauceExtension = new SauceBindingsExtension();
1616

17-
// Enable extension (this also can be done by running with -Dsauce.enabled=true)
17+
// 2. Enable extension (this also can be done by running with -Dsauce.enabled=true)
1818
static {
1919
sauceExtension.enable();
2020
}
2121

22+
// 3. Get variables created by the Sauce Bindings extension
2223
@BeforeEach
2324
public void setUp(SauceSession session, WebDriver driver) {
2425
this.session = session;
@@ -27,7 +28,10 @@ public void setUp(SauceSession session, WebDriver driver) {
2728

2829
@Test
2930
public void quickStartExample() {
31+
// 4. Use the session instance to do Sauce Labs things
3032
session.annotate("Navigating to Swag Labs");
33+
34+
// 5. Use the driver instance to do Selenium things
3135
driver.get("https://www.saucedemo.com/");
3236
}
3337
}

0 commit comments

Comments
 (0)