44import static org .junit .Assert .assertTrue ;
55
66import com .browserstack .local .Local ;
7- import com .thoughtworks .gauge .AfterSpec ;
8- import com .thoughtworks .gauge .BeforeSpec ;
9- import com .thoughtworks .gauge .BeforeSuite ;
10- import com .thoughtworks .gauge .Step ;
7+ import com .thoughtworks .gauge .*;
118import org .openqa .selenium .MutableCapabilities ;
129import org .openqa .selenium .WebDriver ;
1310import org .openqa .selenium .remote .RemoteWebDriver ;
@@ -24,19 +21,29 @@ public class SearchSpec {
2421 private static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub.browserstack.com/wd/hub" ;
2522 private WebDriver driver ;
2623 private Local local ;
27-
24+ @ BeforeSuite
25+ public void beforeSuite () throws Exception {
26+ if (!(System .getenv ("LOCAL" ).isEmpty ()) && System .getenv ("LOCAL" ).equalsIgnoreCase ("true" )) {
27+ local = new Local ();
28+ Map <String , String > options = new HashMap <String , String >();
29+ options .put ("key" , AUTOMATE_KEY );
30+ local .start (options );
31+ }
32+ }
2833 @ BeforeSpec
2934 public void setUp () throws Exception {
3035 try {
3136 MutableCapabilities caps = new MutableCapabilities ();
3237 HashMap <String , Object > browserstackOptions = new HashMap <String , Object >();
3338
3439 if (!(System .getenv ("LOCAL" ).isEmpty ()) && System .getenv ("LOCAL" ).equalsIgnoreCase ("true" )) {
35- local = new Local ();
40+ if (local == null || !local .isRunning ()){
41+ local = new Local ();
42+ Map <String , String > options = new HashMap <String , String >();
43+ options .put ("key" , AUTOMATE_KEY );
44+ local .start (options );
45+ }
3646 browserstackOptions .put ("local" , "true" );
37- Map <String , String > options = new HashMap <String , String >();
38- options .put ("key" , AUTOMATE_KEY );
39- local .start (options );
4047 }
4148
4249 // Capabilities from environment
@@ -90,9 +97,12 @@ public void page_should_contain(String expectedTitle) {
9097 }
9198
9299 @ AfterSpec
93- public void tearDown () throws Exception {
100+ public void tearDown () {
94101 driver .quit ();
95- if (local != null ) {
102+ }
103+ @ AfterSuite
104+ public void afterSuite () throws Exception {
105+ if (local != null && local .isRunning ()){
96106 local .stop ();
97107 }
98108 }
0 commit comments