1- package com .browserstack . run_local_test ;
1+ package com .browserstack ;
22
33import com .browserstack .local .Local ;
44import io .appium .java_client .android .AndroidDriver ;
5- import io .appium .java_client .android .AndroidElement ;
5+ import io .appium .java_client .android .options . UiAutomator2Options ;
66import org .json .simple .JSONArray ;
77import org .json .simple .JSONObject ;
88import org .json .simple .parser .JSONParser ;
1212
1313import org .junit .jupiter .api .parallel .Execution ;
1414import org .junit .jupiter .api .parallel .ExecutionMode ;
15- import org .openqa .selenium .remote .DesiredCapabilities ;
1615
1716import java .io .FileReader ;
1817import java .io .IOException ;
2423@ Execution (ExecutionMode .CONCURRENT )
2524public class BrowserStackJUnitTest {
2625
27- public AndroidDriver < AndroidElement > driver ;
26+ public AndroidDriver driver ;
2827 private Local local ;
2928 public String username ;
3029 public String accessKey ;
31- public DesiredCapabilities capabilities ;
30+ public UiAutomator2Options options ;
3231
3332 public static JSONObject config ;
3433
3534 private static Stream <Integer > devices () throws IOException , ParseException {
3635 List <Integer > taskIDs = new ArrayList <Integer >();
3736
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 ();
4141
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+ }
4445 }
4546
4647 return taskIDs .stream ();
@@ -53,45 +54,51 @@ public void createConnection(int taskId) throws MalformedURLException {
5354 Iterator it = envCapabilities .entrySet ().iterator ();
5455 while (it .hasNext ()) {
5556 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 ());
5758 }
5859
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 );
6061 }
6162
6263 @ BeforeEach
6364 public void setup () throws Exception {
64- capabilities = new DesiredCapabilities ();
65+ options = new UiAutomator2Options ();
6566
6667 Map <String , String > commonCapabilities = (Map <String , String >) config .get ("capabilities" );
6768 Iterator it = commonCapabilities .entrySet ().iterator ();
6869 while (it .hasNext ()) {
6970 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 );
7277 }
7378 }
7479
80+ JSONObject browserstackOptions = (JSONObject ) options .getCapability ("bstack:options" );
81+
7582 username = System .getenv ("BROWSERSTACK_USERNAME" );
7683 if (username == null ) {
77- username = (String ) config .get ("username " );
84+ username = (String ) browserstackOptions .get ("userName " );
7885 }
7986
8087 accessKey = System .getenv ("BROWSERSTACK_ACCESS_KEY" );
8188 if (accessKey == null ) {
82- accessKey = (String ) config .get ("access_key " );
89+ accessKey = (String ) browserstackOptions .get ("accessKey " );
8390 }
8491
8592 String app = System .getenv ("BROWSERSTACK_APP_ID" );
8693 if (app != null && !app .isEmpty ()) {
87- capabilities .setCapability ("app" , app );
94+ options .setCapability ("app" , app );
8895 }
8996
90- if (capabilities . getCapability ( "browserstack. local" ) != null && capabilities . getCapability ( "browserstack. local" ) == "true" ){
97+ if (browserstackOptions . get ( " local" ) != null && browserstackOptions . get ( " local"). toString ( ) == "true" ){
9198 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 );
95102 }
96103 }
97104
0 commit comments