-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for stubbing typing text into window.prompt and confirming correct alert response #339
Comments
Hi @jennifer-shehane Many thanks. |
The example is linked in the comment above. Will paste the content here: Example <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Stubbing Window Prompt</title>
</head>
<body>
<ul>
<li>
<h1>My Application</h1>
<button id="prompt">Prompt</button>
</li>
</ul>
<script>
var btn = document.getElementById('prompt')
btn.addEventListener('click', function (event) {
let sign = prompt("What's your sign?");
if (sign === "scorpio") {
alert("Wow! I'm a Scorpio too!");
}
})
</script>
</body>
</html> My Cypress test file.
it('window prompt test', function () {
cy.visit('/index.html', {
onBeforeLoad(win) {
cy.stub(win, 'prompt').returns('scorpio')
cy.stub(win, 'alert').as('windowAlert')
}
})
cy.get('#prompt').click().then(function () {
expect(this.windowAlert).to.be.calledWith('Wow! I\'m a Scorpio too!')
})
}) This is the result in the Cypress Test Runner after, notice that you can click within the Command Log to see more details about Spies/Stubs. You will no longer see the 'prompt' window pop up and stop execution of the tests since we're handling the type and confirm in Cypress tests. |
Thanks @jennifer-shehane for getting back on this! Could I have a try at it? |
@hazim Sure! |
Hey @jennifer-shehane thanks for providing the work around. However, in my application when I set password and click on confirm button, the different URL opens in the same tab which ask for QA environment credentials in the alert window. The above code is working for that flow. Could you please suggest something else? |
There is a full example here: cypress-io/cypress#5316 (comment)
Should be added to this example folder https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__window
The text was updated successfully, but these errors were encountered: