2121
2222package eu .tsystems .mms .tic .testframework .utils ;
2323
24- import java .util .ArrayList ;
24+ import com .google .gson .Gson ;
25+ import com .google .gson .GsonBuilder ;
26+ import eu .tsystems .mms .tic .testframework .logging .Loggable ;
27+ import org .apache .commons .lang3 .StringUtils ;
28+ import org .openqa .selenium .Capabilities ;
29+ import org .openqa .selenium .remote .DesiredCapabilities ;
30+
2531import java .util .Collections ;
2632import java .util .List ;
2733import java .util .Map ;
28- import java .util .TreeMap ;
29- import org .openqa .selenium .Capabilities ;
30- import org .openqa .selenium .chrome .ChromeOptions ;
31- import org .openqa .selenium .remote .DesiredCapabilities ;
32- import org .apache .commons .lang3 .StringUtils ;
3334
3435/**
3536 * This is a simple helper to modify log messages of {@link Capabilities} to short long values or do other opertations
3940 *
4041 * @author Eric Kubenka
4142 */
42- public class DefaultCapabilityUtils {
43+ public class DefaultCapabilityUtils implements Loggable {
4344
4445 /**
45- * Clean the given {@link Capabilities} and return a {@link Map}
46+ * Clean the given {@link Capabilities} from very long values and return a {@link Map}
4647 *
4748 * @param capabilities {@link Capabilities}
4849 * @return Map
@@ -52,59 +53,60 @@ public Map<String, Object> clean(Capabilities capabilities) {
5253 }
5354
5455 public Map <String , Object > clean (Map <String , Object > capabilityMap ) {
55-
56- // 1. make map modifiable.
57- Map <String , Object > mutableCapabilityMap = new TreeMap <>(capabilityMap );
56+ // 1. clone and make map modifiable.
57+ // For deep cloning it is needed convert it to JSON and back because Firefox options also contain some immutable map objects
58+ Gson gson = new GsonBuilder ().create ();
59+ String json = gson .toJson (capabilityMap );
60+ Map <String , Object > clonedMap = (Map <String , Object >) gson .fromJson (json , Map .class );
5861
5962 // 2. do all the operations
60- mutableCapabilityMap = shortChromeExtensionStrings ( mutableCapabilityMap );
63+ shortMapValues ( clonedMap );
6164
6265 // 3. make the map unmodifiable again.
63- return Collections .unmodifiableMap (mutableCapabilityMap );
66+ return Collections .unmodifiableMap (clonedMap );
6467 }
6568
6669 /**
67- * Extensions strings are very long, so therefore we will cut them off
68- *
69- * @param capabilityMap {@link Map}
70- * @return Map
70+ * Some caps like Extensions strings are very long, so therefore we will cut them off
7171 */
72- private Map <String , Object > shortChromeExtensionStrings (Map <String , Object > capabilityMap ) {
72+ private void shortMapValues (Map <String , Object > map2Short ) {
73+ // Exception list of keys which should not shorten
74+ List <String > exceptionList = List .of (
75+ "path" // Absolute path for Firefox extension files
76+ );
7377
74- final Object chromeOptionsObject = capabilityMap .get (ChromeOptions .CAPABILITY );
75- final Object extensionsObject = capabilityMap .get ("extensions" );
76-
77- if (chromeOptionsObject != null ) {
78- final Map chromeOptions = (Map ) chromeOptionsObject ;
79- if (chromeOptions .containsKey ("extensions" )) {
80- chromeOptions .put ("extensions" , shortAllStringsInLists (chromeOptions .get ("extensions" )));
78+ try {
79+ for (Map .Entry <String , Object > entry : map2Short .entrySet ()) {
80+ Object value = entry .getValue ();
81+ String key = entry .getKey ();
82+ if (value instanceof Map ) {
83+ Map <String , Object > subMap = (Map <String , Object >) value ;
84+ shortMapValues (subMap );
85+ } else {
86+ String stringValue = String .valueOf (value );
87+ if (stringValue .length () > 40 && !exceptionList .contains (key )) {
88+ entry .setValue (stringValue .substring (0 , 40 ) + "..." );
89+ }
90+ }
8191 }
92+ } catch (Exception e ) {
93+ log ().debug ("Cannot clean map: " , e );
8294 }
8395
84- if (extensionsObject != null ) {
85- capabilityMap .put ("extensions" , shortAllStringsInLists (extensionsObject ));
86- }
87-
88- return capabilityMap ;
89- }
90-
91- private List <String > shortAllStringsInLists (final Object stringListObject ) {
92- final List <String > extList = new ArrayList <>();
93- ((List <String >) stringListObject ).forEach (e -> extList .add (e .substring (0 , 10 ) + "..." ));
94- return extList ;
9596 }
9697
9798 /**
9899 * Sets a capability value if the existing value doesn't match the same type,
99100 * is an empty string or doesn't exist.
101+ *
100102 * @param capabilities
101103 * @param capabilityName
102104 * @param capability
103105 * @param <T>
104106 */
105107 public <T > void putIfAbsent (DesiredCapabilities capabilities , String capabilityName , T capability ) {
106108 Object existingCapability = capabilities .getCapability (capabilityName );
107- if (!capability .getClass ().isInstance (existingCapability ) || (existingCapability instanceof String && StringUtils .isBlank ((String )existingCapability ))) {
109+ if (!capability .getClass ().isInstance (existingCapability ) || (existingCapability instanceof String && StringUtils .isBlank ((String ) existingCapability ))) {
108110 capabilities .setCapability (capabilityName , capability );
109111 }
110112 }
0 commit comments