13
13
import org .openqa .selenium .WebDriver ;
14
14
import org .openqa .selenium .MutableCapabilities ;
15
15
import org .openqa .selenium .remote .RemoteWebDriver ;
16
- import org .testng .annotations .AfterMethod ;
17
- import org .testng .annotations .BeforeMethod ;
16
+ import org .testng .annotations .*;
18
17
19
18
public class BrowserStackTestNGTest {
20
19
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
+ }
22
56
23
57
@ BeforeMethod (alwaysRun = true )
24
58
@ org .testng .annotations .Parameters (value = { "config" , "environment" })
25
59
@ SuppressWarnings ("unchecked" )
26
60
public void setUp (String config_file , String environment ) throws Exception {
27
61
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 ));
29
63
JSONObject envs = (JSONObject ) config .get ("environments" );
30
64
31
65
MutableCapabilities capabilities = new MutableCapabilities ();
32
66
33
- Map <String , String > envCapabilities = (Map <String , String >) envs .get (environment );
67
+ Map <String , Object > envCapabilities = (Map <String , Object >) envs .get (environment );
34
68
Iterator it = envCapabilities .entrySet ().iterator ();
35
69
while (it .hasNext ()) {
36
70
Map .Entry pair = (Map .Entry ) it .next ();
37
71
capabilities .setCapability (pair .getKey ().toString (), pair .getValue ());
38
72
}
39
73
40
- Map < String , String > commonCapabilities = (Map <String , String >) config .get ("capabilities" );
74
+ commonCapabilities = (Map <String , Object >) config .get ("capabilities" );
41
75
it = commonCapabilities .entrySet ().iterator ();
42
76
while (it .hasNext ()) {
43
77
Map .Entry pair = (Map .Entry ) it .next ();
@@ -50,19 +84,20 @@ public void setUp(String config_file, String environment) throws Exception {
50
84
}
51
85
}
52
86
53
- String username = System .getenv ("BROWSERSTACK_USERNAME" );
87
+ username = System .getenv ("BROWSERSTACK_USERNAME" );
54
88
if (username == null ) {
55
89
username = (String ) config .get ("user" );
56
90
}
57
91
58
- String accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
92
+ accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
59
93
if (accessKey == null ) {
60
94
accessKey = (String ) config .get ("key" );
61
95
}
62
96
63
97
if (capabilities .getCapability ("bstack:options" ) != null ) {
64
98
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 ()))) {
66
101
l = new Local ();
67
102
Map <String , String > options = new HashMap <String , String >();
68
103
options .put ("key" , accessKey );
@@ -76,10 +111,12 @@ public void setUp(String config_file, String environment) throws Exception {
76
111
}
77
112
78
113
@ AfterMethod (alwaysRun = true )
79
- public void tearDown () throws Exception {
114
+ public void tearDown () {
80
115
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 ();
84
121
}
85
122
}
0 commit comments