Open CSchulz opened 4 years ago
I simplified this example down
index.html
<!DOCTYPE html>
<html>
<body>
<script>
throw new Error('app error')
</script>
</body>
</html>
spec.js
Cypress.on('uncaught:exception', () => {
cy.task('uncaughtException', 'foo')
})
it('throws', () => {
cy.visit('error.html')
})
plugins/index.js
module.exports = (on, config) => {
on('task', {
uncaughtException () {
console.log('foo')
return null
},
})
}
It prints the task to the Command Log, but doesn't actually seem to run the code within the task.
@jennifer-shehane Thanks for your efforts. You should see the 'foo' output on the command line.
This behavior actually applies to any cy
command within an uncaught:exception
handler. I just do not think that we actually support running cypress commands within this handler.
@jennifer-shehane this feature would be very useful. For me especially calling cy.task() inside of a listener. I face also a very similar issue, but within the Cypress.overwrite(...) My case is that i save a log file from test execution. For that i use winston I wanted to log request headers and other stuff by overwriting request() but unfortunately my log("some info") inside of Cypress.overwrite(...) is just ignored. No error nor message. Do you have some thoughts about that?
Do yo think fix for this issue will solve also my problem with logging?
Couldn't add context to report because cy.task()
does not work inside Cypress.on('test:after:run'
version 4.0.2
I'm bumping into this issue with trying to call a cy.task
inside a Cypress.on('window:load')
event, as I'd like to collect some additional page data into a report with every page that's loaded.
The condition for the error is here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cypress/cy.js#L365
I've found that if I side-step this condition so the error isn't thrown, I can run my cy.task
and nothing seems to go wrong with Cypress, so it feels like this is a sledgehammer solution?
Can anyone give any clarity over why this check exists?
Here's all the history I could dig up: https://github.com/cypress-io/cypress/commit/bf0353562ca081fedf96ef30f86e053304abb33a https://github.com/cypress-io/cypress/commit/cd8a96d99500f29b4814bd0e8a56aee1a6d504f1
I've worked around this using a similar mechanism to the code coverage driver: https://github.com/cypress-io/code-coverage/blob/master/support.js#L144
During my window:load
listener I'm now saving the entries to a window.__my_handy_var__
, and then in after
I'm able to call cy.task
and send that data to the back-end task.
Why can't we pass a function that holds return cy.get("elementpath")
to the methods defined in commands.js?
I have method defined in a testPO.js getTextbox(){ return cy.get("[input='text']") }
and a function under commands.js Cypress.Commands.add("enterText", (element, text_to_enter) => { //code to enter the text })
I am calling cy.enterText(getTextbox(), "test")
method in test2.js file, this is throwing as error saying
You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
visible
The invalid subject you asserted on was:
undefined
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
It will be awesome to have this. I need to search for a file in a folder. I cannot use fs in Cypress.on("test:after:run") and I did on a cy.task.
I don't need to be able to run Cypress commands inside an uncaught:exception
; however, it would be lovely if Cypress would warn me what's happening, by forbidding me from running commands and failing the test with an explicit, clear message explaining that I can't do that.
I value the safeguards that Cypress has, which fail tests when I do an invalid thing. (In fact, it is one of these very safeguards that's triggering in the issue -- it's just not the safeguard that most accurately describes what I did wrong!)
My use case is:
cy.visit()
the appcy.log
's the ignored app exceptionlog
in the middle of visit
which has returned a promiseOf course, I don't control the implementation of cy.visit()
or the fact that it returns a promise, so the root cause is very hard to ascertain!
If Cypress had a 2nd level safeguard that prevents me from running commands during an on
that doesn't support them, it would have saved me 3 hours of debugging time. This is probably a much easier goal than fully supporting command execution in an event handler.
We could likely throw an error in this situation, to at least help people to understand that calling Cypress commands is not possible from within Cypress event listeners.
You could possibly work around this by calling the original command in the event listener in cy.now()
. This leads to some confusing logging and I wouldn't necessary recommend it, but it does seem to possibly run the command in some circumstances.
Cypress.on('uncaught:exception', () => {
cy.now('task', 'nameOfTask')
return false
})
Hi @jennifer-shehane I got same issue, and I tried to use cy.now
but I got blocker if the task requires input parameter how we handle it?
e.g cy.task('nameOfTask', 'params')
@jennifer-shehane I see that people liked your workaround, but could you please share the same to log? Fighting with this but no success.
Thank you,
Is there any updates or plans on this? The cy.now
workaround no longer works.
Current behavior:
I want to log all unexpected application errors by using the uncaught:exception and calling a task. But it seems that a task always needs a return value.
Returning null results into another weird behavior:
It seems that cypress tries to use the subject from the called task even if the task was called by
uncaught:exception
event.Desired behavior:
I would expect the subject returned by a task called from the
uncaught:exception
event will be ignored.Test code to reproduce
support.js
plugins.js
Versions
3.8.2