1
- package com .browserstack . run_local_test ;
1
+ package com .browserstack ;
2
2
3
3
import com .browserstack .local .Local ;
4
4
import io .appium .java_client .android .AndroidDriver ;
5
- import io .appium .java_client .android .AndroidElement ;
5
+ import io .appium .java_client .android .options . UiAutomator2Options ;
6
6
import org .json .simple .JSONArray ;
7
7
import org .json .simple .JSONObject ;
8
8
import org .json .simple .parser .JSONParser ;
12
12
13
13
import org .junit .jupiter .api .parallel .Execution ;
14
14
import org .junit .jupiter .api .parallel .ExecutionMode ;
15
- import org .openqa .selenium .remote .DesiredCapabilities ;
16
15
17
16
import java .io .FileReader ;
18
17
import java .io .IOException ;
24
23
@ Execution (ExecutionMode .CONCURRENT )
25
24
public class BrowserStackJUnitTest {
26
25
27
- public AndroidDriver < AndroidElement > driver ;
26
+ public AndroidDriver driver ;
28
27
private Local local ;
29
28
public String username ;
30
29
public String accessKey ;
31
- public DesiredCapabilities capabilities ;
30
+ public UiAutomator2Options options ;
32
31
33
32
public static JSONObject config ;
34
33
35
34
private static Stream <Integer > devices () throws IOException , ParseException {
36
35
List <Integer > taskIDs = new ArrayList <Integer >();
37
36
38
- JSONParser parser = new JSONParser ();
39
- config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/com/browserstack/run_local_test/local.conf.json" ));
40
- int envs = ((JSONArray ) config .get ("environments" )).size ();
37
+ if (System .getProperty ("config" ) != null ) {
38
+ JSONParser parser = new JSONParser ();
39
+ config = (JSONObject ) parser .parse (new FileReader ("src/test/resources/com/browserstack/" + System .getProperty ("config" )));
40
+ int envs = ((JSONArray ) config .get ("environments" )).size ();
41
41
42
- for (int i = 0 ; i < envs ; i ++) {
43
- taskIDs .add (i );
42
+ for (int i = 0 ; i < envs ; i ++) {
43
+ taskIDs .add (i );
44
+ }
44
45
}
45
46
46
47
return taskIDs .stream ();
@@ -53,45 +54,51 @@ public void createConnection(int taskId) throws MalformedURLException {
53
54
Iterator it = envCapabilities .entrySet ().iterator ();
54
55
while (it .hasNext ()) {
55
56
Map .Entry pair = (Map .Entry )it .next ();
56
- capabilities .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
57
+ options .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
57
58
}
58
59
59
- driver = new AndroidDriver (new URL ("http://" + username + ":" + accessKey + "@" + config .get ("server" )+"/wd/hub" ), capabilities );
60
+ driver = new AndroidDriver (new URL ("http://" + config .get ("server" )+"/wd/hub" ), options );
60
61
}
61
62
62
63
@ BeforeEach
63
64
public void setup () throws Exception {
64
- capabilities = new DesiredCapabilities ();
65
+ options = new UiAutomator2Options ();
65
66
66
67
Map <String , String > commonCapabilities = (Map <String , String >) config .get ("capabilities" );
67
68
Iterator it = commonCapabilities .entrySet ().iterator ();
68
69
while (it .hasNext ()) {
69
70
Map .Entry pair = (Map .Entry )it .next ();
70
- if (capabilities .getCapability (pair .getKey ().toString ()) == null ){
71
- capabilities .setCapability (pair .getKey ().toString (), pair .getValue ().toString ());
71
+ if (options .getCapability (pair .getKey ().toString ()) == null ){
72
+ options .setCapability (pair .getKey ().toString (), pair .getValue ());
73
+ }else if (pair .getKey ().toString ().equalsIgnoreCase ("bstack:options" )){
74
+ HashMap bstackOptionsMap = (HashMap ) pair .getValue ();
75
+ bstackOptionsMap .putAll ((HashMap ) options .getCapability ("bstack:options" ));
76
+ options .setCapability (pair .getKey ().toString (), bstackOptionsMap );
72
77
}
73
78
}
74
79
80
+ JSONObject browserstackOptions = (JSONObject ) options .getCapability ("bstack:options" );
81
+
75
82
username = System .getenv ("BROWSERSTACK_USERNAME" );
76
83
if (username == null ) {
77
- username = (String ) config .get ("username " );
84
+ username = (String ) browserstackOptions .get ("userName " );
78
85
}
79
86
80
87
accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
81
88
if (accessKey == null ) {
82
- accessKey = (String ) config .get ("access_key " );
89
+ accessKey = (String ) browserstackOptions .get ("accessKey " );
83
90
}
84
91
85
92
String app = System .getenv ("BROWSERSTACK_APP_ID" );
86
93
if (app != null && !app .isEmpty ()) {
87
- capabilities .setCapability ("app" , app );
94
+ options .setCapability ("app" , app );
88
95
}
89
96
90
- if (capabilities . getCapability ( "browserstack. local" ) != null && capabilities . getCapability ( "browserstack. local" ) == "true" ){
97
+ if (browserstackOptions . get ( " local" ) != null && browserstackOptions . get ( " local"). toString ( ) == "true" ){
91
98
local = new Local ();
92
- Map <String , String > options = new HashMap <String , String >();
93
- options .put ("key" , accessKey );
94
- local .start (options );
99
+ Map <String , String > LocalOptions = new HashMap <String , String >();
100
+ LocalOptions .put ("key" , accessKey );
101
+ local .start (LocalOptions );
95
102
}
96
103
}
97
104
0 commit comments