Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/webapi/dontSeeCurrentPathEquals.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Checks that current URL path does NOT match the expected path.
Query strings and URL fragments are ignored.

```js
I.dontSeeCurrentPathEquals('/form'); // fails for '/form', '/form?user=1', '/form#section'
I.dontSeeCurrentPathEquals('/'); // fails for '/', '/?user=ok', '/#top'
```

@param {string} path value to check.
@returns {void} automatically synchronized promise through #recorder
10 changes: 10 additions & 0 deletions docs/webapi/seeCurrentPathEquals.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Checks that current URL path matches the expected path.
Query strings and URL fragments are ignored.

```js
I.seeCurrentPathEquals('/info'); // passes for '/info', '/info?user=1', '/info#section'
I.seeCurrentPathEquals('/'); // passes for '/', '/?user=ok', '/#top'
```

@param {string} path value to check.
@returns {void} automatically synchronized promise through #recorder
20 changes: 20 additions & 0 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,26 @@ class Playwright extends Helper {
urlEquals(this.options.url).negate(url, await this._getPageUrl())
}

/**
* {{> seeCurrentPathEquals }}
*/
async seeCurrentPathEquals(path) {
const currentUrl = await this._getPageUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
return equals('url path').assert(path, actualPath)
}

/**
* {{> dontSeeCurrentPathEquals }}
*/
async dontSeeCurrentPathEquals(path) {
const currentUrl = await this._getPageUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
return equals('url path').negate(path, actualPath)
}

/**
* {{> see }}
*
Expand Down
20 changes: 20 additions & 0 deletions lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,26 @@ class Puppeteer extends Helper {
urlEquals(this.options.url).negate(url, await this._getPageUrl())
}

/**
* {{> seeCurrentPathEquals }}
*/
async seeCurrentPathEquals(path) {
const currentUrl = await this._getPageUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
return equals('url path').assert(path, actualPath)
}

/**
* {{> dontSeeCurrentPathEquals }}
*/
async dontSeeCurrentPathEquals(path) {
const currentUrl = await this._getPageUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
return equals('url path').negate(path, actualPath)
}

/**
* {{> see }}
*
Expand Down
22 changes: 22 additions & 0 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,28 @@ class WebDriver extends Helper {
return urlEquals(this.options.url).negate(url, decodeUrl(res))
}

/**
* {{> seeCurrentPathEquals }}
*/
async seeCurrentPathEquals(path) {
const currentUrl = await this.browser.getUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
return assert.equal(path, actualPath, `expected url path to be ${path}, but found ${actualPath}`)
}

/**
* {{> dontSeeCurrentPathEquals }}
*/
async dontSeeCurrentPathEquals(path) {
const currentUrl = await this.browser.getUrl()
const baseUrl = this.options.url || 'http://localhost'
const actualPath = new URL(currentUrl, baseUrl).pathname
const errorMessage = `expected url path not to be ${path}, but found ${actualPath}`
const isEqual = path === actualPath
if (isEqual) throw new Error(errorMessage)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a similar function for Playwright and Puppeteer, without custom logic at the end?

}

/**
* Wraps [execute](http://webdriver.io/api/protocol/execute.html) command.
*
Expand Down
39 changes: 39 additions & 0 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ export function tests() {
const url = await I.grabCurrentUrl()
assert.equal(url, `${siteUrl}/info`)
})

it('should check for equality with query strings', async () => {
await I.amOnPage('/info?user=test')
// Query strings matter for exact equality
await I.seeCurrentUrlEquals('/info?user=test')
await I.dontSeeCurrentUrlEquals('/info')
// But substring check works
await I.seeInCurrentUrl('/info')
await I.seeInCurrentUrl('user=test')
})

it('should handle root path with query strings', async () => {
await I.amOnPage('/?user=ok')
// Query strings matter - exact equality requires query string
await I.seeCurrentUrlEquals('/?user=ok')
await I.dontSeeCurrentUrlEquals('/')
// But substring check works for path fragment
await I.seeInCurrentUrl('/')
})

it('should check path equality ignoring query strings', async () => {
await I.amOnPage('/info?user=test')
// Path equality ignores query strings
await I.seeCurrentPathEquals('/info')
await I.dontSeeCurrentPathEquals('/form')
await I.dontSeeCurrentPathEquals('/info?user=test')
})

it('should check root path equality ignoring query strings', async () => {
await I.amOnPage('/?user=ok')
await I.seeCurrentPathEquals('/')
await I.dontSeeCurrentPathEquals('/info')
})

it('should check path equality ignoring hash fragments', async () => {
await I.amOnPage('/info#section')
await I.seeCurrentPathEquals('/info')
await I.dontSeeCurrentPathEquals('/info#section')
})
})

describe('#waitInUrl, #waitUrlEquals', () => {
Expand Down
Loading