5
5
import com .saucelabs .saucebindings .junit5 .SauceBindingsExtension ;
6
6
import com .saucelabs .saucebindings .options .SauceOptions ;
7
7
import java .time .Duration ;
8
+ import java .util .List ;
8
9
import org .junit .jupiter .api .AfterAll ;
10
+ import org .junit .jupiter .api .Assertions ;
9
11
import org .junit .jupiter .api .BeforeAll ;
10
12
import org .junit .jupiter .api .BeforeEach ;
11
13
import org .junit .jupiter .api .Test ;
@@ -20,40 +22,42 @@ public class ToggleLocalExample {
20
22
WebDriver driver ;
21
23
SauceSession session ;
22
24
25
+ // Allow registering multiple test watchers
23
26
@ RegisterExtension
24
27
static SauceBindingsExtension sauceExtension = new SauceBindingsExtension (getSauceOptions ());
25
28
26
29
@ RegisterExtension TestWatcher testWatcher = new LocalTestWatcher ();
27
30
28
- // To run test on Sauce Labs, change this to "false"
31
+ // Run tests with this property set to "false" to execute on Sauce Labs
29
32
@ BeforeAll
30
33
public static void disableSauce () {
31
34
System .setProperty ("sauce.disabled" , "true" );
32
35
}
33
36
34
- @ AfterAll
35
- public static void resetSauce () {
36
- System .clearProperty ("sauce.disabled" );
37
- }
38
-
39
37
@ BeforeEach
40
38
public void setup () {
41
- // TODO: Allow getting session even when disabled
42
- if (isSauceEnabled ()) {
43
- session = sauceExtension .getSession ();
44
- driver = sauceExtension .getDriver ();
45
- } else {
39
+ session = sauceExtension .getSession ();
40
+ if (SauceSession .isDisabled ()) {
46
41
driver = new ChromeDriver (getCapabilities ());
42
+ } else {
43
+ driver = sauceExtension .getDriver ();
47
44
}
48
45
}
49
46
50
47
@ Test
51
48
public void localExample () {
52
- // TODO: Allow this method to be ignored if Sauce is disabled
53
- if (isSauceEnabled ()) {
54
- session .annotate ("Navigating to Swag Labs" );
55
- }
49
+ // This code executes whether running locally or on Sauce
56
50
driver .get ("https://www.saucedemo.com/" );
51
+
52
+ // This code executes if Sauce enabled, and is ignored when disabled
53
+ Assertions .assertDoesNotThrow (
54
+ () -> {
55
+ session .annotate ("This gets ignored" );
56
+ session .addTags (List .of ("ignored" ));
57
+ session .stopNetwork ();
58
+ session .enableLogging ();
59
+ session .getAccessibilityResults ();
60
+ });
57
61
}
58
62
59
63
private static SauceOptions getSauceOptions () {
@@ -66,33 +70,30 @@ private static SauceOptions getSauceOptions() {
66
70
}
67
71
68
72
private static ChromeOptions getCapabilities () {
69
- ChromeOptions chromeOptions = new ChromeOptions ();
70
- chromeOptions .addArguments ("--hide-scrollbars" );
71
-
72
- return chromeOptions ;
73
- }
74
-
75
- // TODO: Implement this as a method in SauceSession directly
76
- private boolean isSauceEnabled () {
77
- String value = System .getenv ("SAUCE_DISABLED" );
78
- return Boolean .parseBoolean (value ) || !Boolean .getBoolean ("sauce.disabled" );
73
+ return new ChromeOptions ();
79
74
}
80
75
76
+ // Do not quit the driver if running on Sauce Labs
81
77
public class LocalTestWatcher implements TestWatcher {
82
78
@ Override
83
79
public void testSuccessful (ExtensionContext context ) {
84
- if (! isSauceEnabled ()) {
85
- System . out . println ( "Test Succeeded" );
80
+ System . out . println ( "Test Succeeded" );
81
+ if ( SauceSession . isDisabled ()) {
86
82
driver .quit ();
87
83
}
88
84
}
89
85
90
86
@ Override
91
87
public void testFailed (ExtensionContext context , Throwable cause ) {
92
- if (! isSauceEnabled ()) {
93
- System . out . println ( "Test Failed" );
88
+ System . out . println ( "Test Failed: " + cause . getMessage ());
89
+ if ( SauceSession . isDisabled ()) {
94
90
driver .quit ();
95
91
}
96
92
}
97
93
}
94
+
95
+ @ AfterAll
96
+ public static void resetSauce () {
97
+ System .clearProperty ("sauce.disabled" );
98
+ }
98
99
}
0 commit comments