Skip to content

Commit bcb5202

Browse files
author
DavertMik
committed
implemented path normalization
1 parent e011581 commit bcb5202

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

lib/helper/Playwright.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
clearString,
2424
requireWithFallback,
2525
normalizeSpacesInString,
26+
normalizePath,
2627
relativeDir,
2728
} from '../utils.js'
2829
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
@@ -2412,7 +2413,7 @@ class Playwright extends Helper {
24122413
const currentUrl = await this._getPageUrl()
24132414
const baseUrl = this.options.url || 'http://localhost'
24142415
const actualPath = new URL(currentUrl, baseUrl).pathname
2415-
return equals('url path').assert(path, actualPath)
2416+
return equals('url path').assert(normalizePath(path), normalizePath(actualPath))
24162417
}
24172418

24182419
/**
@@ -2422,7 +2423,7 @@ class Playwright extends Helper {
24222423
const currentUrl = await this._getPageUrl()
24232424
const baseUrl = this.options.url || 'http://localhost'
24242425
const actualPath = new URL(currentUrl, baseUrl).pathname
2425-
return equals('url path').negate(path, actualPath)
2426+
return equals('url path').negate(normalizePath(path), normalizePath(actualPath))
24262427
}
24272428

24282429
/**

lib/helper/Puppeteer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
isModifierKey,
2727
requireWithFallback,
2828
normalizeSpacesInString,
29+
normalizePath,
2930
} from '../utils.js'
3031
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
3132
import ElementNotFound from './errors/ElementNotFound.js'
@@ -1691,7 +1692,7 @@ class Puppeteer extends Helper {
16911692
const currentUrl = await this._getPageUrl()
16921693
const baseUrl = this.options.url || 'http://localhost'
16931694
const actualPath = new URL(currentUrl, baseUrl).pathname
1694-
return equals('url path').assert(path, actualPath)
1695+
return equals('url path').assert(normalizePath(path), normalizePath(actualPath))
16951696
}
16961697

16971698
/**
@@ -1701,7 +1702,7 @@ class Puppeteer extends Helper {
17011702
const currentUrl = await this._getPageUrl()
17021703
const baseUrl = this.options.url || 'http://localhost'
17031704
const actualPath = new URL(currentUrl, baseUrl).pathname
1704-
return equals('url path').negate(path, actualPath)
1705+
return equals('url path').negate(normalizePath(path), normalizePath(actualPath))
17051706
}
17061707

17071708
/**

lib/helper/WebDriver.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ import output from '../output.js'
1313
const { debug } = output
1414
import { empty } from '../assert/empty.js'
1515
import { truth } from '../assert/truth.js'
16-
import { xpathLocator, fileExists, decodeUrl, chunkArray, convertCssPropertiesToCamelCase, screenshotOutputFolder, getNormalizedKeyAttributeValue, modifierKeys } from '../utils.js'
16+
import {
17+
xpathLocator,
18+
fileExists,
19+
decodeUrl,
20+
chunkArray,
21+
convertCssPropertiesToCamelCase,
22+
screenshotOutputFolder,
23+
getNormalizedKeyAttributeValue,
24+
modifierKeys,
25+
normalizePath,
26+
} from '../utils.js'
1727
import { isColorProperty, convertColorToRGBA } from '../colorUtils.js'
1828
import ElementNotFound from './errors/ElementNotFound.js'
1929
import ConnectionRefused from './errors/ConnectionRefused.js'
@@ -1851,7 +1861,7 @@ class WebDriver extends Helper {
18511861
const currentUrl = await this.browser.getUrl()
18521862
const baseUrl = this.options.url || 'http://localhost'
18531863
const actualPath = new URL(currentUrl, baseUrl).pathname
1854-
return equals('url path').assert(path, actualPath)
1864+
return equals('url path').assert(normalizePath(path), normalizePath(actualPath))
18551865
}
18561866

18571867
/**
@@ -1861,7 +1871,7 @@ class WebDriver extends Helper {
18611871
const currentUrl = await this.browser.getUrl()
18621872
const baseUrl = this.options.url || 'http://localhost'
18631873
const actualPath = new URL(currentUrl, baseUrl).pathname
1864-
return equals('url path').negate(path, actualPath)
1874+
return equals('url path').negate(normalizePath(path), normalizePath(actualPath))
18651875
}
18661876

18671877
/**

lib/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ export const decodeUrl = function (url) {
150150
return decodeURIComponent(decodeURIComponent(decodeURIComponent(url)))
151151
}
152152

153+
export const normalizePath = function (path) {
154+
if (path === '' || path === '/') return '/'
155+
return path
156+
.replace(/\/+/g, '/')
157+
.replace(/\/$/, '') || '/'
158+
}
159+
153160
export const xpathLocator = {
154161
/**
155162
* @param {string} string

test/helper/webapi.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ export function tests() {
114114
await I.seeCurrentPathEquals('/info')
115115
await I.dontSeeCurrentPathEquals('/info#section')
116116
})
117+
118+
it('should normalize trailing slashes in path comparison', async () => {
119+
await I.amOnPage('/info/')
120+
await I.seeCurrentPathEquals('/info')
121+
await I.seeCurrentPathEquals('/info/')
122+
123+
await I.amOnPage('/users/')
124+
await I.seeCurrentPathEquals('/users')
125+
await I.seeCurrentPathEquals('/users/')
126+
})
127+
128+
it('should normalize multiple consecutive slashes in path', async () => {
129+
await I.amOnPage('/users//1')
130+
await I.seeCurrentPathEquals('/users/1')
131+
await I.seeCurrentPathEquals('/users//1')
132+
133+
await I.amOnPage('/info///test')
134+
await I.seeCurrentPathEquals('/info/test')
135+
})
136+
137+
it('should handle root path correctly', async () => {
138+
await I.amOnPage('/')
139+
await I.seeCurrentPathEquals('/')
140+
await I.seeCurrentPathEquals('')
141+
await I.dontSeeCurrentPathEquals('/info')
142+
})
143+
144+
it('should normalize both expected and actual paths', async () => {
145+
await I.amOnPage('/users/')
146+
await I.seeCurrentPathEquals('/users/')
147+
await I.seeCurrentPathEquals('/users')
148+
149+
await I.amOnPage('/users//1/')
150+
await I.seeCurrentPathEquals('/users/1')
151+
await I.seeCurrentPathEquals('/users/1/')
152+
})
117153
})
118154

119155
describe('#waitInUrl, #waitUrlEquals', () => {

0 commit comments

Comments
 (0)