1
1
describe ( "links-and-images" , ( ) => {
2
2
it ( "should return 200 for all links and images" , ( ) => {
3
- const testedPages = new Set < string > ( ) ;
4
- const pagesToTest = [ "/" ] ;
3
+ const pagesToTest : string [ ] = [ ] ;
4
+
5
+ // First get all URLs from sitemap
6
+ cy . request ( "https://www.davidhu.io/sitemap-0.xml" ) . then ( ( response ) => {
7
+ const parser = new DOMParser ( ) ;
8
+ const xmlDoc = parser . parseFromString ( response . body , "text/xml" ) ;
9
+ const urls = xmlDoc . getElementsByTagName ( "url" ) ;
10
+
11
+ for ( let i = 0 ; i < urls . length ; i ++ ) {
12
+ const loc = urls [ i ] . getElementsByTagName ( "loc" ) [ 0 ] ;
13
+ if ( loc ) {
14
+ const url = loc . textContent ;
15
+ if ( url ) {
16
+ pagesToTest . push ( url ) ;
17
+ }
18
+ }
19
+ }
20
+
21
+ visitUrlsOnPage ( ) ;
22
+ } ) ;
5
23
6
24
function visitUrlsOnPage ( ) {
7
25
const url = pagesToTest . pop ( ) ;
@@ -10,12 +28,6 @@ describe("links-and-images", () => {
10
28
return ;
11
29
}
12
30
13
- if ( testedPages . has ( url ) ) {
14
- visitUrlsOnPage ( ) ;
15
- return ;
16
- }
17
-
18
- testedPages . add ( url ) ;
19
31
cy . visit ( url ) ;
20
32
cy . log ( ">>>> Checking images for " + url ) ;
21
33
@@ -35,42 +47,33 @@ describe("links-and-images", () => {
35
47
cy . get ( "a" )
36
48
. should ( Cypress . _ . noop )
37
49
. each ( ( link ) => {
38
- const url = ( link . prop ( "href" ) as string ) || "" ;
50
+ const urlString = ( link . prop ( "href" ) as string ) || "" ;
39
51
40
- // these two links do not allow bot visits
41
- if ( url . includes ( "angel.co" ) || url . includes ( "linkedin.com" ) ) {
52
+ const url = new URL ( urlString ) ;
53
+
54
+ // do not allow bot visits
55
+ if ( url . host === "www.linkedin.com" ) {
42
56
return ;
43
57
}
44
58
45
- if ( url . includes ( "mailto:" ) ) {
59
+ if ( url . protocol === "mailto:" ) {
46
60
return ;
47
61
}
48
62
49
63
// don't download any files
50
64
if (
51
- url . endsWith ( ".msi" ) ||
52
- url . endsWith ( ".dmg" ) ||
53
- url . endsWith ( ".AppImage" ) ||
54
- url . endsWith ( ".deb" )
65
+ url . href . endsWith ( ".msi" ) ||
66
+ url . href . endsWith ( ".dmg" ) ||
67
+ url . href . endsWith ( ".AppImage" ) ||
68
+ url . href . endsWith ( ".deb" )
55
69
) {
56
70
return ;
57
71
}
58
72
59
- cy . request ( url ) ;
60
-
61
- if ( url . includes ( ".xml" ) ) {
62
- return ;
63
- }
64
-
65
- cy . log ( '>>>> Adding "' + url + '" to pages to test' ) ;
66
- if ( url . includes ( "davidhu.io/" ) && ! testedPages . has ( url ) ) {
67
- pagesToTest . push ( url ) ;
68
- }
73
+ cy . request ( url . href ) ;
69
74
} ) ;
70
75
} )
71
76
. then ( visitUrlsOnPage ) ;
72
77
}
73
-
74
- visitUrlsOnPage ( ) ;
75
78
} ) ;
76
79
} ) ;
0 commit comments