1414import com .lambdatest .selenium .lambdatest .capabilities .Selenium3Capabilities ;
1515import com .lambdatest .selenium .lambdatest .capabilities .Selenium4Capabilities ;
1616import com .lambdatest .selenium .lambdatest .capabilities .BrowserOptionsCapabilities ;
17+ import com .lambdatest .selenium .lambdatest .capabilities .CapabilityKeys ;
1718
1819/**
1920 * YAML configuration reader for LambdaTest Selenium SDK.
@@ -123,10 +124,10 @@ public static void injectCapabilities(MutableCapabilities capabilities) {
123124 }
124125
125126 // Ensure lt:options contains credentials
126- if (userCapMap .containsKey ("lt:options" )) {
127- Map <String , Object > ltOptions = (Map <String , Object >) userCapMap .get ("lt:options" );
128- if (sdkCapMap .containsKey ("lt:options" )) {
129- Map <String , Object > sdkLtOptions = (Map <String , Object >) sdkCapMap .get ("lt:options" );
127+ if (userCapMap .containsKey (CapabilityKeys . LT_OPTIONS )) {
128+ Map <String , Object > ltOptions = (Map <String , Object >) userCapMap .get (CapabilityKeys . LT_OPTIONS );
129+ if (sdkCapMap .containsKey (CapabilityKeys . LT_OPTIONS )) {
130+ Map <String , Object > sdkLtOptions = (Map <String , Object >) sdkCapMap .get (CapabilityKeys . LT_OPTIONS );
130131 ltOptions .putAll (sdkLtOptions );
131132 }
132133 }
@@ -147,16 +148,16 @@ private void loadConfig() {
147148
148149 // Try multiple locations for lambdatest.yml
149150 String [] locations = {
150- "lambdatest.yml" , // Root directory
151- "lambdatest.yaml"
151+ CapabilityKeys . CONFIG_FILE_YML , // Root directory
152+ CapabilityKeys . CONFIG_FILE_YAML
152153 };
153154
154155 for (String location : locations ) {
155156 try {
156157 InputStream inputStream = null ;
157158
158159 // First try as file path (for root directory)
159- if (location .equals ("lambdatest.yml" ) || location .equals ("lambdatest.yaml" )) {
160+ if (location .equals (CapabilityKeys . CONFIG_FILE_YML ) || location .equals (CapabilityKeys . CONFIG_FILE_YAML )) {
160161 try {
161162 inputStream = new java .io .FileInputStream (location );
162163 } catch (java .io .FileNotFoundException e ) {
@@ -175,7 +176,7 @@ private void loadConfig() {
175176 inputStream .close ();
176177
177178 // Check if config has "platforms" key (new format)
178- if (rawConfig != null && rawConfig .containsKey ("platforms" )) {
179+ if (rawConfig != null && rawConfig .containsKey (CapabilityKeys . PLATFORMS )) {
179180 config = processPlatformsConfig (rawConfig );
180181 } else {
181182 // Use flat format (backwards compatibility)
@@ -210,7 +211,7 @@ private void loadConfig() {
210211 * Selects platform based on LT_PLATFORM_INDEX environment variable (default: 0)
211212 */
212213 private Map <String , Object > processPlatformsConfig (Map <String , Object > rawConfig ) {
213- Object platformsObj = rawConfig .get ("platforms" );
214+ Object platformsObj = rawConfig .get (CapabilityKeys . PLATFORMS );
214215
215216 if (!(platformsObj instanceof List )) {
216217 // Invalid format, return empty config
@@ -227,9 +228,9 @@ private Map<String, Object> processPlatformsConfig(Map<String, Object> rawConfig
227228 // Get platform index from system property or environment variable (default: 0)
228229 // Priority: System property (-DLT_PLATFORM_INDEX=1) > Environment variable (export LT_PLATFORM_INDEX=1)
229230 int platformIndex = 0 ;
230- String platformIndexStr = System .getProperty ("LT_PLATFORM_INDEX" );
231+ String platformIndexStr = System .getProperty (CapabilityKeys . ENV_LT_PLATFORM_INDEX );
231232 if (platformIndexStr == null || platformIndexStr .trim ().isEmpty ()) {
232- platformIndexStr = System .getenv ("LT_PLATFORM_INDEX" );
233+ platformIndexStr = System .getenv (CapabilityKeys . ENV_LT_PLATFORM_INDEX );
233234 }
234235
235236 if (platformIndexStr != null && !platformIndexStr .trim ().isEmpty ()) {
@@ -259,7 +260,7 @@ private Map<String, Object> processPlatformsConfig(Map<String, Object> rawConfig
259260 // Merge any root-level configurations (non-platforms keys)
260261 for (Map .Entry <String , Object > entry : rawConfig .entrySet ()) {
261262 String key = entry .getKey ();
262- if (!key .equals ("platforms" ) && !platformConfig .containsKey (key )) {
263+ if (!key .equals (CapabilityKeys . PLATFORMS ) && !platformConfig .containsKey (key )) {
263264 platformConfig .put (key , entry .getValue ());
264265 }
265266 }
@@ -281,9 +282,9 @@ public DesiredCapabilities getCapabilities(DesiredCapabilities codeCapabilities)
281282 capabilities .merge (codeCapabilities );
282283
283284 // Ensure lt:options exists if not already set
284- if (codeCapabilities .getCapability ("lt:options" ) == null ) {
285+ if (codeCapabilities .getCapability (CapabilityKeys . LT_OPTIONS ) == null ) {
285286 Map <String , Object > ltOptions = new HashMap <>();
286- capabilities .setCapability ("lt:options" , ltOptions );
287+ capabilities .setCapability (CapabilityKeys . LT_OPTIONS , ltOptions );
287288 }
288289
289290 return capabilities ;
@@ -357,7 +358,7 @@ public DesiredCapabilities getCapabilitiesFromYaml() {
357358 // ============================================================
358359 // 7. Finalize: Set lt:options on capabilities
359360 // ============================================================
360- capabilities .setCapability ("lt:options" , ltOptions );
361+ capabilities .setCapability (CapabilityKeys . LT_OPTIONS , ltOptions );
361362
362363 return capabilities ;
363364 }
@@ -367,26 +368,26 @@ public DesiredCapabilities getCapabilitiesFromYaml() {
367368 */
368369 private void processW3CBrowserCapabilities (DesiredCapabilities capabilities ) {
369370 // browserName (case-sensitive, mandatory)
370- if (config .containsKey ("browserName" )) {
371- capabilities .setCapability ("browserName" , config .get ("browserName" ));
372- } else if (config .containsKey ("browser" )) {
373- capabilities .setCapability ("browserName" , config .get ("browser" ));
371+ if (config .containsKey (CapabilityKeys . BROWSER_NAME )) {
372+ capabilities .setCapability (CapabilityKeys . BROWSER_NAME , config .get (CapabilityKeys . BROWSER_NAME ));
373+ } else if (config .containsKey (CapabilityKeys . BROWSER )) {
374+ capabilities .setCapability (CapabilityKeys . BROWSER_NAME , config .get (CapabilityKeys . BROWSER ));
374375 }
375376
376377 // browserVersion
377- if (config .containsKey ("browserVersion" )) {
378- capabilities .setCapability ("browserVersion" , config .get ("browserVersion" ));
379- } else if (config .containsKey ("version" )) {
380- capabilities .setCapability ("browserVersion" , config .get ("version" ));
378+ if (config .containsKey (CapabilityKeys . BROWSER_VERSION )) {
379+ capabilities .setCapability (CapabilityKeys . BROWSER_VERSION , config .get (CapabilityKeys . BROWSER_VERSION ));
380+ } else if (config .containsKey (CapabilityKeys . VERSION )) {
381+ capabilities .setCapability (CapabilityKeys . BROWSER_VERSION , config .get (CapabilityKeys . VERSION ));
381382 }
382383
383384 // platformName
384- if (config .containsKey ("platformName" )) {
385- capabilities .setCapability ("platformName" , config .get ("platformName" ));
386- } else if (config .containsKey ("platform" )) {
387- capabilities .setCapability ("platformName" , config .get ("platform" ));
388- } else if (config .containsKey ("OS" )) {
389- capabilities .setCapability ("platformName" , config .get ("OS" ));
385+ if (config .containsKey (CapabilityKeys . PLATFORM_NAME )) {
386+ capabilities .setCapability (CapabilityKeys . PLATFORM_NAME , config .get (CapabilityKeys . PLATFORM_NAME ));
387+ } else if (config .containsKey (CapabilityKeys . PLATFORM )) {
388+ capabilities .setCapability (CapabilityKeys . PLATFORM_NAME , config .get (CapabilityKeys . PLATFORM ));
389+ } else if (config .containsKey (CapabilityKeys . OS )) {
390+ capabilities .setCapability (CapabilityKeys . PLATFORM_NAME , config .get (CapabilityKeys . OS ));
390391 }
391392 }
392393
@@ -397,11 +398,11 @@ private void processCredentials(Map<String, Object> ltOptions) {
397398 try {
398399 String username = getUsername ();
399400 String accessKey = getAccessKey ();
400- ltOptions .put ("user" , username );
401- ltOptions .put ("accessKey" , accessKey );
401+ ltOptions .put (CapabilityKeys . USER , username );
402+ ltOptions .put (CapabilityKeys . ACCESS_KEY , accessKey );
402403 } catch (Exception e ) {
403404 // Credentials will be required when creating WebDriver
404- throw new RuntimeException ("LambdaTest credentials not found. Please set LT_USERNAME and LT_ACCESS_KEY environment variables or add 'username ' and 'accesskey ' to lambdatest.yml" );
405+ throw new RuntimeException ("LambdaTest credentials not found. Please set " + CapabilityKeys . ENV_LT_USERNAME + " and " + CapabilityKeys . ENV_LT_ACCESS_KEY + " environment variables or add '" + CapabilityKeys . USERNAME + " ' and '" + CapabilityKeys . ACCESSKEY + " ' to " + CapabilityKeys . CONFIG_FILE_YML );
405406 }
406407 }
407408
@@ -410,10 +411,10 @@ private void processCredentials(Map<String, Object> ltOptions) {
410411 * version should be set on DesiredCapabilities AND as browserVersion for W3C.
411412 */
412413 private void processVersionCapability (DesiredCapabilities capabilities ) {
413- if (config .containsKey ("version" ) && !config .containsKey ("browserVersion" )) {
414- Object versionValue = config .get ("version" );
415- capabilities .setCapability ("version" , versionValue ); // Selenium 3
416- capabilities .setCapability ("browserVersion" , versionValue ); // W3C
414+ if (config .containsKey (CapabilityKeys . VERSION ) && !config .containsKey (CapabilityKeys . BROWSER_VERSION )) {
415+ Object versionValue = config .get (CapabilityKeys . VERSION );
416+ capabilities .setCapability (CapabilityKeys . VERSION , versionValue ); // Selenium 3
417+ capabilities .setCapability (CapabilityKeys . BROWSER_VERSION , versionValue ); // W3C
417418 }
418419 }
419420
@@ -422,18 +423,18 @@ private void processVersionCapability(DesiredCapabilities capabilities) {
422423 */
423424 private void processSpecialCases (DesiredCapabilities capabilities , Map <String , Object > ltOptions ) {
424425 // lambda:userFiles - set directly on capabilities (not in lt:options)
425- if (config .containsKey ("lambda:userFiles" )) {
426- capabilities .setCapability ("lambda:userFiles" , config .get ("lambda:userFiles" ));
427- } else if (config .containsKey ("userFiles" )) {
428- capabilities .setCapability ("lambda:userFiles" , config .get ("userFiles" ));
426+ if (config .containsKey (CapabilityKeys . LAMBDA_USER_FILES )) {
427+ capabilities .setCapability (CapabilityKeys . LAMBDA_USER_FILES , config .get (CapabilityKeys . LAMBDA_USER_FILES ));
428+ } else if (config .containsKey (CapabilityKeys . USER_FILES )) {
429+ capabilities .setCapability (CapabilityKeys . LAMBDA_USER_FILES , config .get (CapabilityKeys . USER_FILES ));
429430 }
430431
431432 // project -> projectName mapping for Selenium 3, and project -> lt:options for Selenium 4
432- if (config .containsKey ("project" )) {
433- Object projectValue = config .get ("project" );
434- ltOptions .put ("project" , projectValue ); // Selenium 4
435- if (!config .containsKey ("projectName" )) {
436- capabilities .setCapability ("projectName" , projectValue ); // Selenium 3
433+ if (config .containsKey (CapabilityKeys . PROJECT )) {
434+ Object projectValue = config .get (CapabilityKeys . PROJECT );
435+ ltOptions .put (CapabilityKeys . PROJECT , projectValue ); // Selenium 4
436+ if (!config .containsKey (CapabilityKeys . PROJECT_NAME )) {
437+ capabilities .setCapability (CapabilityKeys . PROJECT_NAME , projectValue ); // Selenium 3
437438 }
438439 }
439440 }
@@ -453,43 +454,43 @@ public String getHubUrl() {
453454 String username = getUsername ();
454455 String accessKey = getAccessKey ();
455456
456- return "https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub" ;
457+ return CapabilityKeys . HUB_URL_PREFIX + username + CapabilityKeys . HUB_URL_SEPARATOR + accessKey + CapabilityKeys . HUB_URL_SUFFIX ;
457458 }
458459
459460 /**
460461 * Get username with priority: Environment variables > YAML file > Error
461462 */
462463 public String getUsername () {
463464 // Priority 1: Environment variables
464- String username = System .getenv ("LT_USERNAME" );
465+ String username = System .getenv (CapabilityKeys . ENV_LT_USERNAME );
465466 if (username != null && !username .trim ().isEmpty ()) {
466467 return username ;
467468 }
468469
469470 // Priority 2: YAML file
470- if (config != null && config .containsKey ("username" )) {
471- return config .get ("username" ).toString ();
471+ if (config != null && config .containsKey (CapabilityKeys . USERNAME )) {
472+ return config .get (CapabilityKeys . USERNAME ).toString ();
472473 }
473474
474- throw new RuntimeException ("LambdaTest username not found. Please set LT_USERNAME environment variable or add 'username ' to lambdatest.yml" );
475+ throw new RuntimeException ("LambdaTest username not found. Please set " + CapabilityKeys . ENV_LT_USERNAME + " environment variable or add '" + CapabilityKeys . USERNAME + " ' to " + CapabilityKeys . CONFIG_FILE_YML );
475476 }
476477
477478 /**
478479 * Get access key with priority: Environment variables > YAML file > Error
479480 */
480481 public String getAccessKey () {
481482 // Priority 1: Environment variables
482- String accessKey = System .getenv ("LT_ACCESS_KEY" );
483+ String accessKey = System .getenv (CapabilityKeys . ENV_LT_ACCESS_KEY );
483484 if (accessKey != null && !accessKey .trim ().isEmpty ()) {
484485 return accessKey ;
485486 }
486487
487488 // Priority 2: YAML file
488- if (config != null && config .containsKey ("accesskey" )) {
489- return config .get ("accesskey" ).toString ();
489+ if (config != null && config .containsKey (CapabilityKeys . ACCESSKEY )) {
490+ return config .get (CapabilityKeys . ACCESSKEY ).toString ();
490491 }
491492
492- throw new RuntimeException ("LambdaTest access key not found. Please set LT_ACCESS_KEY environment variable or add 'accesskey ' to lambdatest.yml" );
493+ throw new RuntimeException ("LambdaTest access key not found. Please set " + CapabilityKeys . ENV_LT_ACCESS_KEY + " environment variable or add '" + CapabilityKeys . ACCESSKEY + " ' to " + CapabilityKeys . CONFIG_FILE_YML );
493494 }
494495
495496 /**
@@ -524,8 +525,8 @@ private void startTunnelAutomatically() {
524525
525526 // Get optional tunnel name from config
526527 String tunnelName = null ;
527- if (config .containsKey ("tunnelName" )) {
528- tunnelName = config .get ("tunnelName" ).toString ();
528+ if (config .containsKey (CapabilityKeys . TUNNEL_NAME )) {
529+ tunnelName = config .get (CapabilityKeys . TUNNEL_NAME ).toString ();
529530 }
530531
531532 // Start the tunnel
0 commit comments