我希望从我用Cypress测试的当前URL中提取一个URL参数.我基本上可以从this SO post得到答案,然而,当我使用Cypress 的.its() command时,我提取的值对我来说是不可用的. URL中的参数都有句点,我相信这就是我出错的原因. 以下是我正在构建的自定义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)
})
以下是我试图从中提取的URL的相关部分:
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; it completely ignores everything after the period, and thus, never finds my parameter.
早在2018年,我就看到有一个similar error with Cypress's .get()的功能.
我对javascript和Cypress都是新手,所以我希望这只是一件我忽略了的奇怪而简单的事情.在这一点上,我们非常欢迎任何建议或有根据的猜测!
谢谢.