admin管理员组文章数量:1356352
Prior to switching to using a hash router, I had been implementing the cy.url
mand frequently to assure that links were navigating to the right URL addresses throughout the application. Now that we are using hash routing cy.url
no longer yields a string, instead it is yielding a function. Any ideas how to work around this or reasons this is happening?
I was getting errors through out the cypress test runner like:
AssertionError: object tested must be an array, an object, or a string, but undefined given
so I logged the typeof result console.log(typeof(cy.url))
and got function printed to the console.
cy.get(dataCyButtonAttribute)
.should('be.visible')
.click()
console.log(typeof(cy.url))
cy.url().then(url => {
const categoryId = url.split(`${linkType}/`)[1]
const category = url.split('admin/')[1]
expect(category).to.contain(linkType)
expect(categoryId).to.equal('new')
})
}
Prior to switching to using a hash router, I had been implementing the cy.url
mand frequently to assure that links were navigating to the right URL addresses throughout the application. Now that we are using hash routing cy.url
no longer yields a string, instead it is yielding a function. Any ideas how to work around this or reasons this is happening?
I was getting errors through out the cypress test runner like:
AssertionError: object tested must be an array, an object, or a string, but undefined given
so I logged the typeof result console.log(typeof(cy.url))
and got function printed to the console.
cy.get(dataCyButtonAttribute)
.should('be.visible')
.click()
console.log(typeof(cy.url))
cy.url().then(url => {
const categoryId = url.split(`${linkType}/`)[1]
const category = url.split('admin/')[1]
expect(category).to.contain(linkType)
expect(categoryId).to.equal('new')
})
}
Share
Improve this question
edited May 6, 2022 at 19:51
bad_coder
13k20 gold badges56 silver badges88 bronze badges
asked Jun 21, 2019 at 20:44
Justin OswaldJustin Oswald
1791 gold badge5 silver badges13 bronze badges
1
-
Can you share an example GitHub repository that shows this behavior? I tried to reproduce this using your example, but
cy.url
always yields a string for me. – Zach Bloomquist Commented Jun 24, 2019 at 19:48
2 Answers
Reset to default 7This should yield a string:
const returnedUrl = null
cy.url().then(url => {
returnedUrl = url;
});
Cypress mands are asynchronous and must be followed by .then()
in order to yield useful return values.
You can refer to this Github issue for more info: https://github./cypress-io/cypress/issues/2150
I have encountered the same issue. And my solution as below.
cy.url().then(($base_url) => {
let id = $base_url.substr($base_url.lastIndexOf('/'),$base_url.length)
cy.log("The id is " + id);
})
It works for me.
本文标签: javascriptcyurl not returning a string as expectedStack Overflow
版权声明:本文标题:javascript - cy.url not returning a string as expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744054082a2582953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论