@@ -33,6 +33,14 @@ const IGNORED_URLS = [
3333 'https://portal.azure.com' ,
3434]
3535
36+ const rootBaseUrl = Cypress . config ( 'baseUrl' )
37+
38+ const isInternalUrl = ( url : string ) => {
39+ // check against the base url
40+ // and check if the url does not contain a file extension
41+ return url . startsWith ( rootBaseUrl ) && ! url . includes ( '.' )
42+ }
43+
3644const isExternalUrl = ( url : string ) => {
3745 return ! url . includes ( 'localhost' )
3846}
@@ -69,14 +77,11 @@ describe('Broken links test suite', () => {
6977
7078 links
7179 . filter ( ( _i , link ) => {
80+ const href = link . getAttribute ( 'href' )
81+ const src = link . getAttribute ( 'src' )
82+
7283 return ! IGNORED_URLS . some (
73- ( l ) =>
74- //@ts -ignore
75- ( link . getAttribute ( 'href' ) &&
76- link . getAttribute ( 'href' ) ?. includes ( l ) ) ||
77- //@ts -ignore
78- ( link . getAttribute ( 'src' ) &&
79- link . getAttribute ( 'src' ) . includes ( l ) ) ,
84+ ( l ) => href ?. includes ( l ) || src ?. includes ( l ) ,
8085 )
8186 } )
8287 . each ( ( link ) => {
@@ -89,6 +94,25 @@ describe('Broken links test suite', () => {
8994 cy . log ( `link already checked` )
9095 expect ( VISITED_SUCCESSFUL_LINKS [ url ] ) . to . be . true
9196 } else {
97+ // if the link is internal then check the link against the pages fixture (sitemap)
98+ if ( isInternalUrl ( url ) ) {
99+ // clean the url by removing the base url and query params
100+ const rootBaseUrlRegex = new RegExp ( `^${ rootBaseUrl } ` )
101+ let cleanUrl = url . replace ( rootBaseUrlRegex , '' )
102+ const queryIndex = cleanUrl . indexOf ( '?' )
103+ cleanUrl =
104+ queryIndex !== - 1 ? cleanUrl . slice ( 0 , queryIndex ) : cleanUrl
105+
106+ cy . log ( `checking internal link: ${ cleanUrl } ` )
107+ if ( ! pages . includes ( cleanUrl ) ) {
108+ assert . fail ( `${ cleanUrl } is not part of the pages fixture` )
109+ } else {
110+ VISITED_SUCCESSFUL_LINKS [ url ] = true
111+ }
112+
113+ return
114+ }
115+
92116 cy . wait ( 25 )
93117
94118 req ( url ) . then ( ( res : Cypress . Response < any > ) => {
0 commit comments