Skip to content
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

how close 'ok' window alert #376

Closed
cristopher-rodrigues opened this issue Jan 10, 2017 · 6 comments
Closed

how close 'ok' window alert #376

cristopher-rodrigues opened this issue Jan 10, 2017 · 6 comments

Comments

@cristopher-rodrigues
Copy link

@cristopher-rodrigues cristopher-rodrigues commented Jan 10, 2017

image

@cristopher-rodrigues
Copy link
Author

@cristopher-rodrigues cristopher-rodrigues commented Jan 10, 2017

#33 it's the same?

@brian-mann
Copy link
Member

@brian-mann brian-mann commented Jan 10, 2017

We don't expose any API's to do this.

Internally we actually prevent confirm and alert messages from ever appearing (we auto accept them) - so you should not be seeing these.

However it can pop up if this is coming from an iframe or anything other than your app's main window/frame.

Can you post some code or explain what is calling into this? You may need to update your applications code not to show a confirm or alert box when in test mode. For instance you could check:

if (window.top.Cypress) { 
  //... don't show the confirm... 
}`
@cristopher-rodrigues
Copy link
Author

@cristopher-rodrigues cristopher-rodrigues commented Jan 10, 2017

My code is Legacy 🤕. But that:

if (window.top.Cypress) { 
  //... don't show the confirm... 
}

Work for me!

Very tnks @brian-mann!

wee need close that?

@brian-mann
Copy link
Member

@brian-mann brian-mann commented Jan 10, 2017

Nah leave it open, we need to better explain how to get around this.

@brian-mann
Copy link
Member

@brian-mann brian-mann commented Sep 6, 2017

Likely fixed by: #621

However if alert happens in an <iframe> it'll still pop up. We don't handle that situation.

@brian-mann brian-mann closed this Sep 6, 2017
@cypress-io cypress-io locked as resolved and limited conversation to collaborators Dec 7, 2018
@jennifer-shehane
Copy link
Member

@jennifer-shehane jennifer-shehane commented Jan 16, 2020

I wanted to add an example of testing an alert in an iframe, since the last comment indicates this is not possible. Really, the comment only means that we do not automatically handle alerts.

Below is an example of testing an alert from an iframe.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
<iframe id="iframe" width="300" height="200"
  src="iframe.html">
</iframe>
</body>
</html>

iframe.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Stubbing Iframe Window Alert</title>
</head>
<body>
  <ul>
    <li>
      <h1>My Iframe</h1>
      <button id="alert">Alert</button>
    </li>
  </ul>
  <script>
    var btn = document.getElementById('alert')
    btn.addEventListener('click', function (event) {
      window.alert("Hello world!")
    })
  </script>
</body>
</html>
it('alerts in iframe', () => {
  cy.visit('index.html')
  cy.get('iframe').then(($iframe) => {
    const $body = $iframe.contents().find('body')
    const $win = $iframe[0].contentWindow

    cy.stub($win, 'alert').as('windowAlert')

    cy.wrap($body)
      .find('#alert').click().should(function () {
        expect(this.windowAlert).to.be.calledWith('Hello world!')
      })
  })
})

Screen Shot 2020-01-16 at 11 35 04 AM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants