Global afterEach in cypress - cypress

I need a solution. I have a lot of spec files with tests and after each of them I use cy.logout command.
I want to avoid write this logout function each time and want it global.
But when I tried add this function in index.js file it was called at the beginning of each test because index.js works perfect only with beforeEach but not with afterEach.
What should I do?
Thanks
[EDIT]
I just find out that problem was provoke by my mistake in code in second test, so the only problem I have with Cypress is that I dont get any information where I made my mistake because Cypress show me that problem is afterEach hook and nothing else.

If you want your logout runs only once for each of your spec file, and not after each of your test within the file, you must use after instead of afterEach.
after(() => {
cy.logout()
})

Using afterEach() or beforeEach() block in support/index.js. Refer below page to run a "global" hook
https://filiphric.com/cypress-basics-before-beforeeach-after-aftereach

Related

CSSTransition component's callbacks are not called in Cypress testing environment

I've been using this React CSSTransition component:
http://reactcommunity.org/react-transition-group/css-transition
I'm adding Cypress tests. CSSTransition callbacks such as onExited and onEntered always run when I'm walking through my app in a regular browser (Chrome). But in the version of Chrome being automated by Cypress, these callbacks are either never called or never executed.
I wonder if anyone else has run into this issue, or has some ideas about why it's happening, and how to fix it, or work around it.
It had to do with cy.clock. If you use cy.clock earlier in the test, you need to use cy.tick, or
cy.clock().then((clock) => {
clock.restore()
})

Does beforeEach() work as expected in cypress?

I am trying to use beforeEach() in my cypress test but my dev says that the cypress code gets loaded at once and before each might not functions as its meant to. Is that true ?
It depends on what your expectation is of beforeEach(). But I can tell you how it works. A beforeEach() is indeed loaded at once, but it is performed before each it() in your test. So if your goal is to have the steps in the beforeEach to be executed in every test, than it works exactly as you wish.

How can I execute code before all tests suite with Cypress?

Basically, I want to login once before all my tests in all files are executed.
Should I call my login command in each test file using the before hook or is there any way to do it once before all tests?
Short answer: You can write your login command in a before hook within the supportFile (the file that is loaded automatically before your other spec files). This before hook will run before any of the code in your other test files.
Recommendations: That being said, this approach leaves little flexibility for variation in your individual test files that you may want in the future like:
What if you want to seed the database differently for one test?
What if you want to log in as a different user with different permissions?
What if you need to do something in onBeforeLoad once?
I would recommend just having the login command in a before hook in each individual spec file.
I would also further recommend having your login command in a beforeEach hook to avoid sharing any state in between tests.
describe('Hooks', function() {
before(function() {
// runs once before all tests in the block
})
})
https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Hooks
I would login before EACH test because there could be things that happen in a previous test that affects the results of the current test. With a fresh login, you're starting with a clean state every time. If you want to test a "chain" of unrelated actions (action A THEN action B), then write that as a separate test, but have basic functionality in individual tests.
describe('/page'), () => {
beforeEach(() => {
cy.login() // custom command that handles login w/o UI
cy.visit('/page') // go to the page you are testing
})
// tests
})
You should include a beforeEach block in every test file. This block should login and navigate to the page in question.
I would wrap the function to execute in a before block, as others already suggested.
Now, looking at the docs, I would make that happen in the cypress/support/index.js file.

UglifyJS2 call minify function programmatically

I would like to know if I can call the minify main function programmatically.
I am able to run the same code as defined in compress, however replacing UglifyJS.Compressor with UglifyJS.minify does not work.
This is because I would like to pass the same options defined in the README of UglifyJS2, instead of the ones in compress.
Please note that I am running the code in Nashorn, and not in Node.js so that would be similar to a browser environment.
This is a work in progress: see this issue and this PR.

Can i use BeforeEach and afterEach functions in config file in protractor?

I wonder if can use beforeEach and afterEach functions in config file in protractor in order to avoid repeating common code for all scripts ?
Thanks.
Nope. But Protractor's config does allow you to use beforeLaunch, onPrepare, and onComplete blocks. Those should get you where you want to be.

Resources