我需要检查按钮上URL的特定参数。
我就是这样做的:
cy.get('.send-button-url')
.should('have.attr', 'href')
.and('eq', 'mailto:contact@example.com?subject=&body=')
.then(url => {
const arr = url.split('.com?')[1].split('&')
const paramObj = {}
arr.forEach(param => {
const [key, value] = param.split('=')
paramObj[key] = value
})
cy.get('input[placeholder*="Subject"]')
.invoke('val')
.then(sometext => cy.log(sometext)) // this one shows `log ` empty. That's good, because the input field is empty at the beginning. cy.wrap(paramObj).then(obj => {
expect(obj.subject).to.eq(
cy
.get('input[placeholder*="Subject"]')
.invoke('val')
.then(sometext => sometext),
)
})
})它给了我:
AssertionError期望“”等于{ Object (specWindow,chainerId,.)}
主语开头是空的,应该比空的要空。
如果我跑了
cy
.get('input[placeholder*="Subject"]')
.invoke('val')
.then(sometext => cy.log(sometext),
)它显示带有空文本的log。而且很好。
我做错了什么以及如何修复它?
发布于 2022-11-06 02:58:57
使用subject解析href可能更容易一些,因为href是内置在浏览器中的。
cy.get('.send-button-url')
.should('have.attr', 'href')
.and('eq', 'mailto:contact@example.com?subject=&body=')
.then(href => {
const url = new URL(href)
const subject = url.searchParams.get('subject')
cy.get('input[placeholder*="Subject"]')
.invoke('val')
.should('eq', subject)
})这适用于这个带有空白主题的HTML
<a class="send-button-url" href="mailto:contact@example.com?subject=&body=" ></a>
<input placeholder="Enter the Subject" value="">用非空白主题“柏树”进行测试
<a class="send-button-url" href="mailto:contact@example.com?subject=Cypress&body=" ></a>
<input placeholder="Enter The Subject" value="Cypress">然后,您需要更改.and()断言,或将其参数化。
const subject = 'Cypress'
const expectedHref = `mailto:contact@example.com?subject=${subject}&body=`
cy.get('input').type(subject)
cy.get('.send-button-url')
.should('have.attr', 'href')
.and('eq', expectedHref)
.then(href => {
const url = new URL(href)
const subject = url.searchParams.get('subject')
cy.get('input[placeholder*="Subject"]')
.invoke('val')
.should('eq', subject)
})https://stackoverflow.com/questions/74327881
复制相似问题