Javascript 是否可以在Cypress中提示的文本框中键入并单击ok按钮
是否可以使用Cypress命令并单击Javascript 是否可以在Cypress中提示的文本框中键入并单击ok按钮,javascript,cypress,Javascript,Cypress,是否可以使用Cypress命令并单击okinside alert,在警报中显示的文本框中键入内容。下面的测试显示警报消息,但没有执行任何操作 describe('Type some text inside the textbox in an alert', function() { it.only('Type some text inside the textbox test', function() { cy.visit('http://sometesturl.com')
ok
inside alert,在警报中显示的文本框中键入内容。下面的测试显示警报消息,但没有执行任何操作
describe('Type some text inside the textbox in an alert', function() {
it.only('Type some text inside the textbox test', function() {
cy.visit('http://sometesturl.com')
const stub = cy.stub()
cy.on ('window:alert', stub)
cy
.get('#pidforinput').click()
.then(($stub) => {
const myWin = $stub.find('input');
cy.wrap(myWin ).type("This is a text box");
})
})
})
//下面是我的js代码,在里面显示警报和文本框
function promptMessage() {
var favColor = prompt("What is your favorite color?", "");
if (favColor != null){
alert("Your favorite color is " + favColor);
}
else {
alert("You did not specify your favorite color");
}
}
所以现在根据Cypress的文档,您可以使用存根来更改这样的提示行为
cy.visit('http://localhost:3000', {
onBeforeLoad(win) {
cy.stub(win, 'prompt').returns('my custom message')
}
})
cy.window().its('prompt').should('be.called')
cy.get('any_selector').should('have.value', 'my custom message')
检查进一步的信息我得到以下CypressError:超时重试:应该找到元素:'.name',但从未找到它。这里的.name
只是一个示例,您可以在任何地方从提示符返回消息。您可以在那里使用任何选择器。我已经在那里编辑了我的答案。所以,我们不能在警报中的“文本框”字段中键入返回的文本,然后单击警报中的“确定”按钮吗?因此,我们正在修改使用提示的行为。因此,基本上,无论您想在文本框中键入什么,都可以将其替换为my custom message
。在处理窗口事件的Cypress检查中,它将显示相同的行为。确认中的文本已使用cy.stub(win,'prompt')传递。返回('my custom message')
thiscy.get('any_selector')。应该('have.value','my custom message')
这用于检查是否传递了正确的文本,您可以跳过此部分。