1
- package com .browserstack . run_local_test ;
1
+ package com .browserstack ;
2
2
3
3
import com .browserstack .local .Local ;
4
4
15
15
import org .json .simple .parser .JSONParser ;
16
16
17
17
import io .appium .java_client .android .AndroidDriver ;
18
- import io .appium .java_client .android .AndroidElement ;
18
+ import io .appium .java_client .android .options . UiAutomator2Options ;
19
19
20
20
import org .junit .runners .Parameterized ;
21
- import org .openqa .selenium .remote .DesiredCapabilities ;
21
+
22
+ import org .openqa .selenium .WebElement ;
22
23
23
24
import org .junit .After ;
24
25
import org .junit .Before ;
28
29
29
30
@ RunWith (Parameterized .class )
30
31
public class BrowserStackJUnitTest {
31
- public AndroidDriver < AndroidElement > driver ;
32
+ public AndroidDriver driver ;
32
33
private Local local ;
33
34
34
35
private static JSONObject config ;
@@ -40,62 +41,69 @@ public class BrowserStackJUnitTest {
40
41
public static Iterable <? extends Object > data () throws Exception {
41
42
List <Integer > taskIDs = new ArrayList <Integer >();
42
43
43
- JSONParser parser = new JSONParser ();
44
- config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/com/browserstack/run_local_test/local.conf.json" ));
45
- int envs = ((JSONArray ) config .get ("environments" )).size ();
44
+ if (System .getProperty ("config" ) != null ) {
45
+ JSONParser parser = new JSONParser ();
46
+ config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/com/browserstack/" + System .getProperty ("config" )));
47
+ int envs = ((JSONArray ) config .get ("environments" )).size ();
46
48
47
- for (int i = 0 ; i < envs ; i ++) {
48
- taskIDs .add (i );
49
+ for (int i = 0 ; i < envs ; i ++) {
50
+ taskIDs .add (i );
51
+ }
49
52
}
50
-
51
53
return taskIDs ;
52
54
}
53
55
54
56
@ Before
55
57
public void setUp () throws Exception {
56
58
JSONArray envs = (JSONArray ) config .get ("environments" );
57
59
58
- DesiredCapabilities capabilities = new DesiredCapabilities ();
60
+ UiAutomator2Options options = new UiAutomator2Options ();
59
61
60
62
Map <String , String > envCapabilities = (Map <String , String >) envs .get (taskID );
61
63
Iterator it = envCapabilities .entrySet ().iterator ();
62
64
while (it .hasNext ()) {
63
65
Map .Entry pair = (Map .Entry ) it .next ();
64
- capabilities .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
66
+ options .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
65
67
}
66
68
67
69
Map <String , String > commonCapabilities = (Map <String , String >) config .get ("capabilities" );
68
70
it = commonCapabilities .entrySet ().iterator ();
69
71
while (it .hasNext ()) {
70
72
Map .Entry pair = (Map .Entry ) it .next ();
71
- if (capabilities .getCapability (pair .getKey ().toString ()) == null ) {
72
- capabilities .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
73
+ if (options .getCapability (pair .getKey ().toString ()) == null ) {
74
+ options .setCapability (pair .getKey ().toString (), pair .getValue ());
75
+ }else if (pair .getKey ().toString ().equalsIgnoreCase ("bstack:options" )){
76
+ HashMap bstackOptionsMap = (HashMap ) pair .getValue ();
77
+ bstackOptionsMap .putAll ((HashMap ) options .getCapability ("bstack:options" ));
78
+ options .setCapability (pair .getKey ().toString (), bstackOptionsMap );
73
79
}
74
80
}
75
81
82
+ JSONObject browserstackOptions = (JSONObject ) options .getCapability ("bstack:options" );
83
+
76
84
String username = System .getenv ("BROWSERSTACK_USERNAME" );
77
85
if (username == null ) {
78
- username = (String ) config .get ("username " );
86
+ username = (String ) browserstackOptions .get ("userName " );
79
87
}
80
88
81
89
String accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
82
90
if (accessKey == null ) {
83
- accessKey = (String ) config .get ("access_key " );
91
+ accessKey = (String ) browserstackOptions .get ("accessKey " );
84
92
}
85
93
86
94
String app = System .getenv ("BROWSERSTACK_APP_ID" );
87
95
if (app != null && !app .isEmpty ()) {
88
- capabilities .setCapability ("app" , app );
96
+ options .setCapability ("app" , app );
89
97
}
90
98
91
- if (capabilities . getCapability ( "browserstack. local" ) != null && capabilities . getCapability ( "browserstack. local" ) == "true" ) {
99
+ if (browserstackOptions . get ( " local" ) != null && browserstackOptions . get ( " local"). toString ( ) == "true" ) {
92
100
local = new Local ();
93
- Map <String , String > options = new HashMap <String , String >();
94
- options .put ("key" , accessKey );
95
- local .start (options );
101
+ Map <String , String > LocalOptions = new HashMap <String , String >();
102
+ LocalOptions .put ("key" , accessKey );
103
+ local .start (LocalOptions );
96
104
}
97
105
98
- driver = new AndroidDriver (new URL ("http://" + username + ":" + accessKey + "@" + config .get ("server" ) + "/wd/hub" ), capabilities );
106
+ driver = new AndroidDriver (new URL ("http://" + config .get ("server" )+ "/wd/hub" ), options );
99
107
}
100
108
101
109
@ After
0 commit comments