我正在撰寫 CY 測驗,并試圖自己解決幾個小時但沒有成功。你能幫我一下嗎:)
每次我運行測驗時,我都會得到新的 URL,例如
https://website.com/en/info/is/here/
我只需要保存
/en/info/is/here/(所以沒有域名)
我需要稍后將它與另一個href進行比較。
你能告訴我怎么做或者至少是方向嗎?非常感謝!
uj5u.com熱心網友回復:
cy.location ()命令為您提供命名部分,因此示例pathname中的部分是您需要的部分
cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
cy.location().should((loc) => {
..
cy.wrap(loc.pathname).as('url1')
...
})
如果您也有搜索或哈希
cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
cy.location().should((loc) => {
..
cy.wrap(loc.pathname loc.search loc.hash).as('url1')
...
})
uj5u.com熱心網友回復:
您可以.split()在 URL 字串上使用。
保存它的位置取決于它的使用位置。
在一項測驗中:
let pathname
cy.url().then((url) => url.split('/').slice(3)).as('pathname1')
...
cy.get('@pathname1').then(pathname1 => {
expect(pathname1).to.eq(pathname2)
})
測驗之間:
let pathname1
it('gets first pathname', () => {
cy.url().then((url) => pathname1 = url.split('/').slice(3))
})
it('uses first pathname', () => {
expect(pathname1).to.eq(pathname2)
})
uj5u.com熱心網友回復:
您可以使用以下內容:
let firstUrl = null;
let secondUrl = null;
cy.url().then(url => {
firstUrl = url;
});
/* sometimes later */
cy.url().then(url => {
secondUrl = url;
});
/* sometimes later */
expect(firstUrl).to.equal(secondUrl)
如果您只想比較這些 URL 的某些部分,我建議您使用正則運算式。
uj5u.com熱心網友回復:
您可以使用 javascriptsplit來執行此操作:
let partUrl
cy.url().then((url) => {
partUrl = url.split('com')[1] //saves /en/info/is/here/
})
uj5u.com熱心網友回復:
使用URL介面決議您的字串(也由 cy.location 使用)
const urlString = 'https://website.com/en/info/is/here/'
const url = new URL(urlString)
const pathname = url.pathname // yields "/en/info/is/here/"
uj5u.com熱心網友回復:
您可以使用.replace()Cypress baseUrl 值,然后將該值存盤在 Cypress 環境變數中。
cy.url().then((url) => {
Cypress.env('someUrl', url.replace(Cypress.config('baseUrl'), '');
}).then(() => {
cy.log(Cypress.env('someUrl'));
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/516731.html
標籤:测试网址自动化小路柏
上一篇:如何計算兩個未排序陣列中的重復項
