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

Allow for "global logging" to be possible from Cypress events #1949

Closed
egucciar opened this issue Jun 14, 2018 · 12 comments
Closed

Allow for "global logging" to be possible from Cypress events #1949

egucciar opened this issue Jun 14, 2018 · 12 comments

Comments

@egucciar
Copy link
Contributor

egucciar commented Jun 14, 2018

Current behavior:

Cyrrently, attempting to make a cy.log call either onBeforeLoad or on fail is not working. I need to output some metadata to test runner on each run so i can reconcile the test run with our Sentry integration in case there is a backend or JS error which is not being captured in the test runner.

These codes do not work

Cypress.on('window:before:load', win => {
  win.SentryReleaseVersion = (new Date()).toISOString() + randomString(5);
  console.log('Release Version', win.SentryReleaseVersion)
  cy.log('Release Version', win.SentryReleaseVersion);
});

Cypress.on('fail', () => {
  cy.window().then(win => {
    console.log('Release Version', win.SentryReleaseVersion)
    cy.log('Release Version', win.SentryReleaseVersion);
    Cypress.log({
      name: 'Test',
      message: win.SentryReleaseVersion
    })
  })
})

Desired behavior:

I would like a clear path to accomplish the above use case without writing a beforeEach for every suite in the app.

Versions

3.0.1

@brian-mann
Copy link
Member

Neither of those functions support writing async code, which cy.* commands are. However you can use the synchronous versions of both to do what you want to do.

Can you go into more detail about:

I would like a clear path to accomplish the above use case without writing a beforeEach for every suite in the app.

You can write a simple root level beforeHook in your supportFile which will automatically run for all specs. You don't need to touch anything in your spec files for that to work. The support file is automatically imported before each spec.

Per your other questions: you can use Cypress.log(...) and you can use cy.state('window') to get synchronous access to the AUT's window.

@egucciar
Copy link
Contributor Author

@brian-mann where can i find the docs for the root level before? I have not seen the capability yet.

Let me try cy.state and Cypress.log in the fail event and see if it works.

@egucciar
Copy link
Contributor Author

egucciar commented Jun 14, 2018

@brian-mann trying this code passes the test & then has an indefinite loading graphic on what im trying to do :(

image

Cypress.on('fail', () => {
  const window = cy.state('window');
  Cypress.log({
    name: 'Version:',
    message: window.sentryReleaseVersion
  })
})

@egucciar
Copy link
Contributor Author

egucciar commented Jun 14, 2018

@brian-mann ok i understand - you can use beforeEach and afterEach outside of a describe block. TIL.

This is working for me:

afterEach(() => {
  const window = cy.state('window');
  cy.log('Version:', window.SentryReleaseVersion)
})

@egucciar
Copy link
Contributor Author

Thanks for the help.

The only other thing I can see is my afterEach will run even if the test passed. Is there a way for me to get the status of the test too?

@jennifer-shehane
Copy link
Member

Doc on global hooks in support file: https://on.cypress.io/writing-and-organizing-tests#Support-file

@brian-mann
Copy link
Member

@egucciar this is a feature as part of mocha so we don't really go into a lot of detail since it's not us adding anything here.

I think this is an FAQ question too: @jennifer-shehane / @ValerieThoma if it's not, it should be, possibly a recipe as well.

https://mochajs.org/#root-level-hooks

@egucciar
Copy link
Contributor Author

@ValerieThoma @jennifer-shehane @brian-mann

wil the afterEach run before the error screenshot occurs? the reasoning behind all of this is i want to output a uniquely identifying string to the Cypress console when a test fails. That way, if the test fails due to an exception or error thrown, i can cross-match it to Sentry.

@egucciar
Copy link
Contributor Author

@brian-mann alternatively if you can log out to the standard console during a CLI run, then we can just output the version rather than logging it to the test runner itself.

I think integration with Sentry would greatly improve the benefit we get from our awesome cypress test suite, so I hope I can find a solution

@jennifer-shehane
Copy link
Member

I've added Sentry to the list of integrations we're considering in our integrations issue: #981

@egucciar
Copy link
Contributor Author

egucciar commented Jun 20, 2018

Neat! For now I'm beforeEaching the version instead and can reference the video for more details. Sentry and other integrations out of the box would be awesome. We personally use Segment to streamline our use of Integration platforms , and are happy with the result , but the ability to view the logs next to the test cases would be super awesome

@jennifer-shehane
Copy link
Member

Since this issue hasn't had activity in a while, we'll close the issue until we can confirm this is still happening. Please comment if there is new information to provide concerning the original issue and we'd be happy to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants