我们如何使用Cypress.io Js自动化框架测试警报和显示的文本?我无法理解Cypress文档中的相关示例,请告知。
describe('Test an alert and the text displaying', function() {
it('Verify alert and its text content', function(){
cy.visit('http://www.seleniumeasy.com/test/javascript-alert-box-demo.html')
cy.get('button').contains('Click me!').click()
cy.on ('window:alert', 'I am an alert box!')
})
})
发布于 2018-08-11 08:19:56
按照Richard Matsen的建议,使用cy.stub()方法得出答案:
describe('Test an alert and the text displaying', function() {
it('Verify alert and its text content', function(){
cy.visit('http://www.seleniumeasy.com/test/javascript-alert-box-demo.html')
const stub = cy.stub()
cy.on ('window:alert', stub)
cy
.get('button').contains('Click me!').click()
.then(() => {
expect(stub.getCall(0)).to.be.calledWith('I am an alert box!')
})
})
})
发布于 2019-02-27 02:31:07
这是一种更简单、更直观的方法:
cy.on('window:alert', (str) => {
expect(str).to.equal(`This is an alert box!`)
})
我发现做这件事的stub()
方法太混乱,不直观,而且容易出错。
发布于 2019-07-16 23:54:11
如果你碰巧使用了这个:alert.js,那么也许我可以帮你省去一些麻烦。尝试类似这样的操作来查找未注册到DOM的元素:
// for example, a button in the modal that needs clicking
// the event that fires the alert
cy.get('<some element>').click()
cy.window().then(win => {
const el = win.Alert.$(`<whatever element you're looking for>`)
cy.wrap(el)
.find('button')
.contains('Confirm')
.click()
})
https://stackoverflow.com/questions/51795306
复制相似问题