11using Aquality . Selenium . Core . Configurations ;
22using Aquality . Selenium . Core . Elements . Interfaces ;
33using Aquality . Selenium . Core . Localization ;
4- using SkiaSharp ;
54using System ;
65using System . Collections . Generic ;
76using System . Globalization ;
@@ -39,15 +38,7 @@ public virtual float Compare(string dumpName = null)
3938 {
4039 var directory = GetDumpDirectory ( dumpName ) ;
4140 LocalizedLogger . Info ( "loc.form.dump.compare" , directory . Name ) ;
42- if ( ! directory . Exists )
43- {
44- throw new InvalidOperationException ( $ "Dump directory [{ directory . FullName } ] does not exist.") ;
45- }
46- var imageFiles = directory . GetFiles ( $ "*{ ImageFormat . Extension } ") ;
47- if ( imageFiles . Length == 0 )
48- {
49- throw new InvalidOperationException ( $ "Dump directory [{ directory . FullName } ] does not contain any [*{ ImageFormat . Extension } ] files.") ;
50- }
41+ var imageFiles = GetImageFiles ( directory ) ;
5142 var existingElements = FilterElementsForVisualization ( ) . ToDictionary ( el => el . Key , el => el . Value ) ;
5243 var countOfUnproceededElements = existingElements . Count ;
5344 var countOfProceededElements = 0 ;
@@ -64,12 +55,21 @@ public virtual float Compare(string dumpName = null)
6455 }
6556 else
6657 {
67- comparisonResult += existingElements [ key ] . Visual . GetDifference ( SKImage . FromEncodedData ( imageFile . FullName ) ) ;
58+ comparisonResult += existingElements [ key ] . Visual . GetDifference ( imageFile . ReadImage ( ) ) ;
6859 countOfUnproceededElements -- ;
6960 countOfProceededElements ++ ;
7061 existingElements . Remove ( key ) ;
7162 }
7263 }
64+ LogUnproceededElements ( countOfUnproceededElements , existingElements , absentOnFormElementNames ) ;
65+ // adding of countOfUnproceededElements means 100% difference for each element absent in dump or on page
66+ var result = ( comparisonResult + countOfUnproceededElements ) / ( countOfProceededElements + countOfUnproceededElements ) ;
67+ LocalizedLogger . Info ( "loc.form.dump.compare.result" , result . ToString ( "P" , CultureInfo . InvariantCulture ) ) ;
68+ return result ;
69+ }
70+
71+ private void LogUnproceededElements ( int countOfUnproceededElements , IDictionary < string , T > existingElements , IList < string > absentOnFormElementNames )
72+ {
7373 if ( countOfUnproceededElements > 0 )
7474 {
7575 if ( existingElements . Any ( ) )
@@ -82,10 +82,20 @@ public virtual float Compare(string dumpName = null)
8282 }
8383 LocalizedLogger . Warn ( "loc.form.dump.unprocessedelements" , countOfUnproceededElements ) ;
8484 }
85- // adding of countOfUnproceededElements means 100% difference for each element absent in dump or on page
86- var result = ( comparisonResult + countOfUnproceededElements ) / ( countOfProceededElements + countOfUnproceededElements ) ;
87- LocalizedLogger . Info ( "loc.form.dump.compare.result" , result . ToString ( "P" , CultureInfo . InvariantCulture ) ) ;
88- return result ;
85+ }
86+
87+ private FileInfo [ ] GetImageFiles ( DirectoryInfo directory )
88+ {
89+ if ( ! directory . Exists )
90+ {
91+ throw new InvalidOperationException ( $ "Dump directory [{ directory . FullName } ] does not exist.") ;
92+ }
93+ var imageFiles = directory . GetFiles ( $ "*{ ImageFormat . Extension } ") ;
94+ if ( imageFiles . Length == 0 )
95+ {
96+ throw new InvalidOperationException ( $ "Dump directory [{ directory . FullName } ] does not contain any [*{ ImageFormat . Extension } ] files.") ;
97+ }
98+ return imageFiles ;
8999 }
90100
91101 public virtual void Save ( string dumpName = null )
@@ -170,10 +180,11 @@ protected virtual DirectoryInfo GetDumpDirectory(string dumpName = null)
170180 if ( fullDumpPath . Length + maxNameLengthOfDumpElements > MaxFullFileNameLength )
171181 {
172182 validDumpNameString = validDumpNameString . Substring ( 0 , MaxFullFileNameLength - Path . GetFullPath ( DumpsDirectory ) . Length - maxNameLengthOfDumpElements ) ;
173- LocalizedLogger . Warn ( "loc.form.dump.exceededdumpname" , validDumpNameString ) ;
183+ fullDumpPath = Path . Combine ( DumpsDirectory , validDumpNameString ) ;
184+ LocalizedLogger . Warn ( "loc.form.dump.exceededdumpname" , fullDumpPath ) ;
174185 }
175186
176- return new DirectoryInfo ( Path . Combine ( DumpsDirectory , validDumpNameString ) ) ;
187+ return new DirectoryInfo ( fullDumpPath ) ;
177188 }
178189 }
179190}
0 commit comments