[英]cy.wrap().its()... doesn't work when the value in .its() contains a period
I am looking to extract a URL parameter from the current URL I'm testing with Cypress.我希望从我正在使用赛普拉斯测试的当前 URL 中提取一个 URL 参数。 I was able to basically get the answer from this SO post , however, my extracted values are not available to me when I use Cypress's .its() command .
我基本上可以从这篇 SO 帖子中得到答案,但是,当我使用赛普拉斯的.its() 命令时,我提取的值对我不可用。 The parameters in the url all have periods in them, and I believe this is the cause for my error.
url中的参数都有句点,我相信这是我出错的原因。 Here is my custom Cypress Command I'm building:
这是我正在构建的自定义 Cypress 命令:
Cypress.Commands.add('getParmsCommand', function(value) {
cy.url().as('url')
cy.then( () => {
cy.log(this.url)
const kvPairArray = this.url.toString().split('?')[1].toString().split('&')
const paramObj = {}
kvPairArray.forEach(param => {
cy.log(param)
//default 'value' to 0 if it doesn't exist
const [ key, value="0" ] = param.split('=')
paramObj[key] = value
})
//forcefully adding a debug element to the key value store for testing
paramObj['beverage'] = 'soda'
cy.wrap(paramObj)
.its('timeline.ws') //doesn't work
// .its(`${Cypress.$.escapeSelector('timeline.ws')}`) doesn't work
// .its('timeline\.ws') doesn't work
// .its('"timeline.ws"') doesn't work
// .its('beverage') this DOES work!
.then(parmVal => {
cy.log(parmVal)
})
Here is the relevant part of the URL that I'm trying to extract from:这是我试图从中提取的 URL 的相关部分:
timeline.ws=3600000&timeline.to&timeline.fm&timeline.ar=false
timeline.ws=3600000&timeline.to&timeline.fm&timeline.ar=false
You can see from the error that Cypress is only looking for the id timeline , NOT timeline.ws ;从错误中可以看出,Cypress 只是在寻找 id timeline ,而不是timeline.ws ; it completely ignores everything after the period, and thus, never finds my parameter.
它完全忽略了这段时间之后的所有内容,因此永远找不到我的参数。
I saw there was a similar error with Cypress's.get() function back in 2018.我在 2018 年看到Cypress 的 .get() function 也有类似的错误。
I am new to both javascript and Cypress, so I hope it's just a weird easy thing I'm overlooking.我是 javascript 和赛普拉斯的新手,所以我希望这只是我忽略的一件奇怪的简单事情。 Any advice or educated guesses are greatly welcome at this point!
在这一点上,非常欢迎任何建议或有根据的猜测!
Thank you.谢谢你。
.its()
is just a shorthand for property extraction. .its()
只是属性提取的简写。 Since it fails with the period, you could instead use bracket notation in a .then()
.由于句点失败,您可以在
.then()
中使用括号表示法。
cy.wrap(paramObj)
.then(paramObj => paramObj['timeline.ws'])
or just要不就
cy.wrap(paramObj['timeline.ws'])
Playing around with the URL constructor玩转 URL构造函数
const urlString = 'http://example.com?timeline.ws=3600000&timeline.to&timeline.fm&timeline.ar=false'
const url = new URL(urlString)
cy.wrap(url.searchParams.get('timeline.ws'))
.should('eq', '3600000')
cy.wrap(url.searchParams.get('timeline.to'))
.should('be.empty')
cy.wrap(url.searchParams.get('timeline.ar'))
.should('eq', 'false')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.