1313import org .openqa .selenium .WebDriver ;
1414import org .openqa .selenium .MutableCapabilities ;
1515import org .openqa .selenium .remote .RemoteWebDriver ;
16- import org .testng .annotations .AfterMethod ;
17- import org .testng .annotations .BeforeMethod ;
16+ import org .testng .annotations .*;
1817
1918public class BrowserStackTestNGTest {
2019 public WebDriver driver ;
21- private Local l ;
20+ private static Local l ;
21+ private static JSONObject config ;
22+ private static Map <String , Object > commonCapabilities ;
23+ private static String username ;
24+ private static String accessKey ;
25+
26+ @ BeforeSuite (alwaysRun =true )
27+ @ Parameters (value = { "config" })
28+ public void beforeSuite (String config_file ) throws Exception {
29+ JSONParser parser = new JSONParser ();
30+ config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/conf/" + config_file ));
31+ commonCapabilities = (Map <String , Object >) config .get ("capabilities" );
32+ HashMap bstackOptionsMap = (HashMap ) commonCapabilities .get ("bstack:options" );
33+
34+
35+ username = System .getenv ("BROWSERSTACK_USERNAME" );
36+ if (username == null ) {
37+ username = (String ) config .get ("user" );
38+ }
39+
40+ accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
41+ if (accessKey == null ) {
42+ accessKey = (String ) config .get ("key" );
43+ }
44+ try {
45+ if ((bstackOptionsMap .get ("local" ) != null &&
46+ bstackOptionsMap .get ("local" ).toString ().equalsIgnoreCase ("true" ) && (l == null ))) {
47+ l = new Local ();
48+ Map <String , String > options = new HashMap <String , String >();
49+ options .put ("key" , accessKey );
50+ l .start (options );
51+ }
52+ } catch (Exception e ) {
53+ System .out .println ("Error while start local - " + e );
54+ }
55+ }
2256
2357 @ BeforeMethod (alwaysRun = true )
2458 @ org .testng .annotations .Parameters (value = { "config" , "environment" })
2559 @ SuppressWarnings ("unchecked" )
2660 public void setUp (String config_file , String environment ) throws Exception {
2761 JSONParser parser = new JSONParser ();
28- JSONObject config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/conf/" + config_file ));
62+ config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/conf/" + config_file ));
2963 JSONObject envs = (JSONObject ) config .get ("environments" );
3064
3165 MutableCapabilities capabilities = new MutableCapabilities ();
3266
33- Map <String , String > envCapabilities = (Map <String , String >) envs .get (environment );
67+ Map <String , Object > envCapabilities = (Map <String , Object >) envs .get (environment );
3468 Iterator it = envCapabilities .entrySet ().iterator ();
3569 while (it .hasNext ()) {
3670 Map .Entry pair = (Map .Entry ) it .next ();
3771 capabilities .setCapability (pair .getKey ().toString (), pair .getValue ());
3872 }
3973
40- Map < String , String > commonCapabilities = (Map <String , String >) config .get ("capabilities" );
74+ commonCapabilities = (Map <String , Object >) config .get ("capabilities" );
4175 it = commonCapabilities .entrySet ().iterator ();
4276 while (it .hasNext ()) {
4377 Map .Entry pair = (Map .Entry ) it .next ();
@@ -50,19 +84,20 @@ public void setUp(String config_file, String environment) throws Exception {
5084 }
5185 }
5286
53- String username = System .getenv ("BROWSERSTACK_USERNAME" );
87+ username = System .getenv ("BROWSERSTACK_USERNAME" );
5488 if (username == null ) {
5589 username = (String ) config .get ("user" );
5690 }
5791
58- String accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
92+ accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
5993 if (accessKey == null ) {
6094 accessKey = (String ) config .get ("key" );
6195 }
6296
6397 if (capabilities .getCapability ("bstack:options" ) != null ) {
6498 HashMap bstackOptionsMap = (HashMap ) capabilities .getCapability ("bstack:options" );
65- if ((bstackOptionsMap .get ("local" ) != null && bstackOptionsMap .get ("local" ).toString ().equalsIgnoreCase ("true" )) || (capabilities .getCapability ("browserstack.local" ) != null && capabilities .getCapability ("browserstack.local" ).toString ().equalsIgnoreCase ("true" ))) {
99+ if ((bstackOptionsMap .get ("local" ) != null &&
100+ bstackOptionsMap .get ("local" ).toString ().equalsIgnoreCase ("true" ) && (l == null || !l .isRunning ()))) {
66101 l = new Local ();
67102 Map <String , String > options = new HashMap <String , String >();
68103 options .put ("key" , accessKey );
@@ -76,10 +111,12 @@ public void setUp(String config_file, String environment) throws Exception {
76111 }
77112
78113 @ AfterMethod (alwaysRun = true )
79- public void tearDown () throws Exception {
114+ public void tearDown () {
80115 driver .quit ();
81- if (l != null ) {
82- l .stop ();
83- }
116+ }
117+
118+ @ AfterSuite (alwaysRun = true )
119+ public void afterSuite () throws Exception {
120+ if (l != null ) l .stop ();
84121 }
85122}
0 commit comments