【问题标题】:Unable to select from auto complete Cypress无法从自动完成赛普拉斯中选择
【发布时间】:2021-12-08 05:13:24
【问题描述】:
the auto-select for auto complete field
it('Test to grab the autocomplete values', ()=> {
cy.visit('https://jqueryui.com/autocomplete/');
cy.get('#tags').type('c')
cy.get('#ui-id-2').first().click()
})
})
它显示它被点击但它没有被选中
【问题讨论】:
标签:
javascript
automated-tests
cypress
【解决方案1】:
我可以看到有一个 iframe,要处理 iframe,你必须使用代码:
cy.get('iframe.demoframe').its('0.contentDocument').its('body')
所以你的代码应该是这样的:
cy.visit('https://jqueryui.com/autocomplete/')
cy.get('iframe.demo-frame')
.its('0.contentDocument')
.its('body')
.find('#tags')
.type('c')
cy.get('iframe.demo-frame')
.its('0.contentDocument')
.its('body')
.find('li')
.first()
.click()
现在,如果您想进一步压缩您的脚本,您可以使用赛普拉斯自定义命令。去cypress/support/command.js写:
Cypress.Commands.add('getIframe', (iframe) => {
return cy
.get(iframe)
.its('0.contentDocument.body')
.should('be.visible')
.then(cy.wrap)
})
你的测试将是:
cy.visit('https://jqueryui.com/autocomplete/')
cy.getIframe('iframe.demo-frame').find('#tags').type('c')
cy.getIframe('iframe.demo-frame').find('li').first().click()
【解决方案2】:
你的代码基本没问题,但是需要在iframe内执行
cy.visit('https://jqueryui.com/autocomplete/')
cy.get('iframe.demo-frame')
.its('0.contentDocument.body')
.within(() => {
cy.get('#tags').type('c')
cy.get('#ui-id-2').first().click()
cy.get('#tags').should('have.value', 'ActionScript')
})