-
Notifications
You must be signed in to change notification settings - Fork 676
Implement per-test, not per-browser hooks #1345
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
Comments
Hello @chadmyers, We've had discussion before about this feature in context of #903, but decided to postpone it's implementation since where was no demand for it at the time. The question is should this hooks have access to BTW, for currently you can use the following workaround to achieve desired functionality: function once(fn) {
var executed = false;
return function() {
if (!executed) {
executed = true;
return fn.apply(this, arguments);
}
};
}
fixture `My fixture`
.beforeEach(once(async t => {
// ...
}));
test
.before(once(async t => {
//...
}))
('My test', async t => {
//...
}); |
@inikulin Thank you for the response! I'm loving TestCafe, btw. I'll definitely check out the "once()" trick. That may do what I need. You asked for my use case: Our testing philosophy for UI tests (what we call "regression tests" since we follow the unit/integration/regression test hierarchy scheme) is that we reset the DB back to a known state before each test. So before each test, I want to run our "clear()" routine. The problem I ran into is that if 4 browsers are running the test simultaneously and they fire off clear() at various times (due to asynchronous execution) they can stomp on each other. So I'd like to run the clear once for all four browsers, run the test for all browsers, then move onto the next test. The workaround I used was to do all the test setup in the fixture hook, but that means I can only have one test per fixture and we usually have more tests per fixture. It's possible I may be bringing my prior-tool philosophy into this tool and that's why I'm having this difficulty, so feel free to tell me I'm "doing it wrong" and I'll readjust my understanding accordingly :) Thanks again for a great tool! |
I understand the conundrum about the TestController access in the "beforeAll" (or whatever it would be called) method. For my purposes, I do not need access to the browser in the beforeAll(), in fact, that would be the entire point of this method would be "Before each test, before the browsers are spawned". The |
@chadmyers Thank you for the feedback. We need to figure out naming now. I wonder if |
If I'm not mistaken, it looks like the
|
@djbreen7 The approach with |
I'm trying to use djbreen7's solution to login once and run multiple tests while logged in. Ie. behavior that would normally be handled in a I tried using Any ideas on how to work around this? I really just want to login once, then run a couple tests (same file). Thanks! |
@qualityshepherd Do you have sample code somewhere? I've had a lot of luck with Roles. |
@qualityshepherd |
Ahhh... figured out the problem I was having with Roles. I'm using page objects and had a My solution is now to have a
And then...
|
|
Regarding the question of should the is should the Fixture .before and .after hooks have access to TestController, I would say that it would be very useful to have this. Our example is as follows:
Now we could of course develop a custom hook to insert this into the database directly but this has other security implications. We could also go direct to the database, but again security issues and it means the test can't be run in production or controlled environments. Also the solutions suggested to abuse the .beforeEach() hook to run only once don't seem like they would work as the setup takes 20 seconds and you could enter a situation where other tests start running before the prerequisite has been setup. Allowing .before and .after to have access to the TestController will save the execution time of 100 seconds for this fixture. When you add it up across all the fixtures, it does result in TestCafe running slower than had the tests been run in other frameworks that support this. |
Please... I BEG of you... give me a But seriously, do. |
@qualityshepherd, based on your previous comment, you seem to have found a solution using Roles. Therefore, it's not quite clear why you need the |
@elimence, Thank you for your opinion. |
Hello everybody. Do you have any news on this ticket? Thanks in advance. |
Hi @vicentesaettone09 Thank you for your inquiry. At present, the feature is not implemented. |
With every test I write... I get more an more annoyed by the lack of a |
Is there currently a workaround to login once at the fixture level, and execute multiple tests within that fixture? |
@omartaghlabi try out Testcafe's User Roles. They're a little janky (sorry guys, but #fact) but they do work. I have an example project where I use them. Take a peek and see if that helps... |
I'll try that out. Thanks @qualityshepherd! |
I researched the issue and created a separate testcafe-once-hook module, which allows you to emulate the required behavior. Please check the following github repository to see how the module works: https://github.com/AlexKamaev/testcafe-once-hook-example The
|
Nice... will give it a go... only thing is I also need an |
I'll close the issue since now we have a separate module to support the described behavior. If there are any issues that cannot be covered by the module, feel free to describe them here. |
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow. |
Are you requesting a feature or reporting a bug?
Requesting a feature
What is the current behavior?
Fixture hooks are run once per fixture, test hooks are run once per test per browser (if using the
all
option or something likechrome,firefox
).What is the expected behavior?
I'd like a third type of hook which runs once per test for all browsers. So if I'm running a fixture with 2 tests in it against
chrome,firefox
browsers, I'd like it to do:The text was updated successfully, but these errors were encountered: