3737import java .util .Map ;
3838import java .util .Set ;
3939import java .util .concurrent .atomic .AtomicInteger ;
40+ import java .util .stream .Stream ;
4041import org .apache .commons .lang .exception .ExceptionUtils ;
4142import org .junit .After ;
4243import org .junit .Assert ;
4950
5051public class LoadHtmlTest {
5152
52- protected static final String RESULT_QUERY_METADATA = ("{attributes={charset=UTF-8}, tagName=meta}, "
53- + "{attributes={name=ResourceLoaderDynamicStyles}, tagName=meta}, "
54- + "{attributes={name=generator, content=MediaWiki 1.32.0-wmf.18}, tagName=meta}, "
55- + "{attributes={name=referrer, content=origin}, tagName=meta}, "
56- + "{attributes={name=referrer, content=origin-when-crossorigin}, tagName=meta}, "
57- + "{attributes={name=referrer, content=origin-when-cross-origin}, tagName=meta}, "
58- + "{attributes={property=og:image, content=https://upload.wikimedia.org/wikipedia/en/e/ea/Aap_Kaa_Hak_titles.jpg}, tagName=meta}" );
59-
60- protected static final String RESULT_QUERY_H2 =
61- ("{text=Contents, tagName=h2}, " + "{text=Origins[edit], tagName=h2}, "
62- + "{text=Content[edit], tagName=h2}, "
63- + "{text=Legacy[edit], tagName=h2}, "
64- + "{text=References[edit], tagName=h2}, "
65- + "{text=Navigation menu, tagName=h2}" );
53+ protected static final List <Map <String , Object >> RESULT_QUERY_METADATA = asList (
54+ map ("tagName" , "meta" , "attributes" , map ("charset" , "UTF-8" )),
55+ map ("attributes" , map ("name" , "ResourceLoaderDynamicStyles" ), "tagName" , "meta" ),
56+ map ("attributes" , map ("name" , "generator" , "content" , "MediaWiki 1.32.0-wmf.18" ), "tagName" , "meta" ),
57+ map ("attributes" , map ("name" , "referrer" , "content" , "origin" ), "tagName" , "meta" ),
58+ map ("attributes" , map ("name" , "referrer" , "content" , "origin-when-crossorigin" ), "tagName" , "meta" ),
59+ map ("attributes" , map ("name" , "referrer" , "content" , "origin-when-cross-origin" ), "tagName" , "meta" ),
60+ map (
61+ "attributes" ,
62+ map (
63+ "property" ,
64+ "og:image" ,
65+ "content" ,
66+ "https://upload.wikimedia.org/wikipedia/en/e/ea/Aap_Kaa_Hak_titles.jpg" ),
67+ "tagName" ,
68+ "meta" ));
69+
70+ protected static final List <Map <String , Object >> RESULT_QUERY_H2 = asList (
71+ map ("text" , "Contents" , "tagName" , "h2" ),
72+ map ("text" , "Origins[edit]" , "tagName" , "h2" ),
73+ map ("text" , "Content[edit]" , "tagName" , "h2" ),
74+ map ("text" , "Legacy[edit]" , "tagName" , "h2" ),
75+ map ("text" , "References[edit]" , "tagName" , "h2" ),
76+ map ("text" , "Navigation menu" , "tagName" , "h2" ));
6677
6778 private static final String INVALID_PATH = new File ("src/test/resources/wikipedia1.html" ).getName ();
6879 private static final String VALID_PATH =
@@ -208,11 +219,7 @@ public void testQueryMetadata() {
208219 map ("url" , new File ("src/test/resources/wikipedia.html" ).toURI ().toString (), "query" , query ),
209220 result -> {
210221 Map <String , Object > row = result .next ();
211- assertEquals (
212- map ("metadata" , asList (RESULT_QUERY_METADATA ))
213- .toString ()
214- .trim (),
215- row .get ("value" ).toString ().trim ());
222+ assertEquals (map ("metadata" , RESULT_QUERY_METADATA ), row .get ("value" ));
216223 assertFalse (result .hasNext ());
217224 });
218225 }
@@ -426,9 +433,7 @@ public void testQueryH2() {
426433 map ("url" , new File ("src/test/resources/wikipedia.html" ).toURI ().toString (), "query" , query ),
427434 result -> {
428435 Map <String , Object > row = result .next ();
429- assertEquals (
430- map ("h2" , asList (RESULT_QUERY_H2 )).toString ().trim (),
431- row .get ("value" ).toString ().trim ());
436+ assertEquals (map ("h2" , RESULT_QUERY_H2 ), row .get ("value" ));
432437 assertFalse (result .hasNext ());
433438 });
434439 }
@@ -472,8 +477,9 @@ public void testHref() {
472477 public void testQueryWithFailsSilentlyWithList () {
473478 Map <String , Object > query = map ("a" , "a" , "invalid" , "invalid" , "h6" , "h6" );
474479
475- String expectedH6 =
476- "[{attributes={id=correct}, text=test, tagName=h6}, {attributes={id=childIncorrect}, text=incorrecttest, tagName=h6}]" ;
480+ List <Map <String , Object >> expectedH6 = asList (
481+ map ("attributes" , map ("id" , "correct" ), "text" , "test" , "tagName" , "h6" ),
482+ map ("attributes" , map ("id" , "childIncorrect" ), "text" , "incorrecttest" , "tagName" , "h6" ));
477483
478484 testResult (
479485 db ,
@@ -485,7 +491,7 @@ public void testQueryWithFailsSilentlyWithList() {
485491 // number of <a> tags in html file minus the incorrect tag
486492 assertEquals (107 , ((List ) value .get ("a" )).size ());
487493 assertEquals (Collections .emptyList (), value .get ("invalid" ));
488- assertEquals (expectedH6 , value .get ("h6" ). toString (). trim () );
494+ assertEquals (expectedH6 , value .get ("h6" ));
489495 assertEquals (2 , ((List ) value .get (KEY_ERROR )).size ());
490496 assertFalse (result .hasNext ());
491497 });
@@ -495,8 +501,17 @@ public void testQueryWithFailsSilentlyWithList() {
495501 public void testQueryWithFailsSilentlyWithListAndChildren () {
496502 Map <String , Object > query = map ("a" , "a" , "invalid" , "invalid" , "h6" , "h6" );
497503
498- String expectedH6 =
499- "[{children=[], attributes={id=correct}, text=test, tagName=h6}, {children=[], attributes={id=childIncorrect}, text=incorrect, tagName=h6}]" ;
504+ List <Map <String , Object >> expectedH6 = asList (
505+ map ("children" , asList (), "attributes" , map ("id" , "correct" ), "text" , "test" , "tagName" , "h6" ),
506+ map (
507+ "children" ,
508+ asList (),
509+ "attributes" ,
510+ map ("id" , "childIncorrect" ),
511+ "text" ,
512+ "incorrect" ,
513+ "tagName" ,
514+ "h6" ));
500515
501516 testResult (
502517 db ,
@@ -508,7 +523,7 @@ public void testQueryWithFailsSilentlyWithListAndChildren() {
508523 // number of <a> tags in html file minus the incorrect tag
509524 assertEquals (107 , ((List ) value .get ("a" )).size ());
510525 assertEquals (Collections .emptyList (), value .get ("invalid" ));
511- assertEquals (expectedH6 , value .get ("h6" ). toString (). trim () );
526+ assertEquals (expectedH6 , value .get ("h6" ));
512527 assertEquals (3 , ((List ) value .get (KEY_ERROR )).size ());
513528 assertFalse (result .hasNext ());
514529 });
@@ -638,12 +653,17 @@ public static void skipIfBrowserNotPresentOrCompatible(Runnable runnable) {
638653 try {
639654 runnable .run ();
640655 } catch (RuntimeException e ) {
656+
641657 // The test don't fail if the current chrome/firefox version is incompatible or if the browser is not
642658 // installed
659+ Stream <String > notPresentOrIncompatible = Stream .of (
660+ "cannot find Chrome binary" ,
661+ "Cannot find firefox binary" ,
662+ "Expected browser binary location" ,
663+ "browser start-up failure" ,
664+ "This version of ChromeDriver only supports Chrome version" );
643665 final String msg = e .getMessage ();
644- if (!msg .contains ("cannot find Chrome binary" )
645- && !msg .contains ("Cannot find firefox binary" )
646- && !msg .contains ("This version of ChromeDriver only supports Chrome version" )) {
666+ if (notPresentOrIncompatible .noneMatch (msg ::contains )) {
647667 throw e ;
648668 }
649669 }
0 commit comments