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
Add the capability to execute a custom script before page initialization scripts #1739
Comments
This feature is useful e.g. if you want to override |
Yes. This feature is good |
I think that
|
I would suggest to extend the proposal with several points made in #2234:
Currently the framework has undocumented |
This would also be helpful if certain local storage keys need to be set before the page loads. |
Note: we already have build-in support for accessing console messages |
any update for this enhancement? |
Would you please describe a real life case for which you need this functionality? |
@miherlosev Service worker are usually registered with an inline script: <script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
}
</script> So if i try to mock |
@miherlosev |
@NickCis, @bitjoo - thank you for providing additional details. You can solve these issues by using RequestMock to handle specific page requests. Take a look at the following topics in the documentation: For example, you can mock a service worker script for browsers that don't support this feature using the following code: import { ClientFunction, RequestMock } from 'testcafe';
const supportsServiceWorkers = ClientFunction(() => 'serviceWorker' in navigator);
fixture `ServiceWorker`
.page('http://my-site.com')
.beforeEach(async t => {
const canUseServiceWorkers = await supportsServiceWorkers();
if (canUseServiceWorkers)
return;
await t.addRequestMocks(
RequestMock()
.onRequest('http://my-site.com/service-worker.js')
.respond('');
);
}); |
@miherlosev One more use case is that. Since client scripts are loading after page load, we can not mock any setInterval, setTimout functions |
@AlexKamaev would you guys accept a PR if I implement it? |
@benmonro, you are welcome to submit your PR. We will be happy to review it. |
hey all, started this PR w/ the changes: #3880 |
We are discussing the API with the team. We'll prepare our full vision of the issue within a few days. |
After discussing with the team we introduce the following API principles for adding custom scripts to the tested page:
Client script initializersString
Object with content fixture `Fixture`
.clientScripts({ content: 'var i = 0' '}); Object with path fixture `Fixture`
.clientScripts({ path: './assets/helpers.js' }); Object with page property (specified the pages for which the target script will be injected) fixture `Fixture`
.clientScripts({
path: 'assets/helper.js'
page: 'http://example.com'
}) Object with 'module' property fixture `Fixture`
.clientScripts({ module: 'dom-utils' }) Possible argumentsfixutre `Fixture`.clientScripts('test1.js'); // single argument
fixture `Fixture.clientScripts('test1.js', 'test2.js'); // list of arguments
fixture `Fixture.clientScripts(['test1.js']); // array of arguments API:Test and fixture fixture `My fixture`.clientScripts('test.js');
test.clientScripts('test.js'); Command line testcafe chrome /tests --client-scripts asserts/jquery.js,mockDate.js Configuration {
"clientScripts": ['wait-for-react-helper.js']
} Runner const runner = testcafe.createRunner();
return runner
.src(['tests/fixture1.js', 'tests/func/fixture3.js'])
.browsers(['chrome', 'safari'])
.clientScripts(['/assets/jquery.js'])
.run(); |
Would you also please consider a separate configuration for "beforeEach" that doesn't need to be set in each fixture? I had originally requested that and that issue was closed in favor of this one. Jest and mocha and other test runners have this feature as well for things like testing library add ons (like testcafe-testing-library) and it adds a nice way to centrally configure those things |
If you want to add a custom script for each fixture, use the For example, the following code will add const runner = testcafe.createRunner();
return runner
.src(['tests/fixture1.js', 'tests/func/fixture3.js'])
.browsers(['chrome', 'safari'])
.clientScripts(['/assets/jquery.js'])
.run(); |
Yes but what if it's not a client script? Ie something I just want to set
up in the test side
On Thu, Jun 13, 2019 at 5:30 AM Mikhail Losev ***@***.***> wrote:
Would you also please consider a separate configuration for "beforeEach"
that doesn't need to be set in each fixture?
If you want to add a custom script for each fixture, use the clientScripts
method/property at the Configuration, Command line and Runner levels.
For example, the following code will add /assets/jquery.js content to the
tested page in each fixture.
const runner = testcafe.createRunner();
return runner
.src(['tests/fixture1.js', 'tests/func/fixture3.js'])
.browsers(['chrome', 'safari'])
.clientScripts(['/assets/jquery.js'])
.run();
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1739?email_source=notifications&email_token=AADBPBAQ757VIVZ7IWO7B6DP2I4WDA5CNFSM4DYYFYX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXTQ66Q#issuecomment-501682042>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADBPBF5O7F6FA3TWAE6QFDP2I4WDANCNFSM4DYYFYXQ>
.
--
Ben Monro
Software Developer
|
@benmonro |
@miherlosev yes I did that but you closed that issue in favor of this one. #3857 fixture.before would still require a modification in each test figure file. I would like a way to globally set fixture.before I'm the way they jest or mocha let you set that as well. |
@benmonro |
* initial * fix wrong rebase * new logic for resolving relative paths (test api and others) * refactoring * fix tests * provide error messages for uncaught errors occured in injected client scripts * fix tests * fix error messages * fix server tests * fix relative paths * fix type definition * prevent multiple method calls for clientScripts and requestHooks * fix an error message during module loading * specify execution order for clientScripts and requestHooks * skip test * skip test * refix the test * fix client scripts loading order and handle module path resolving errors
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. |
) * initial * fix wrong rebase * new logic for resolving relative paths (test api and others) * refactoring * fix tests * provide error messages for uncaught errors occured in injected client scripts * fix tests * fix error messages * fix server tests * fix relative paths * fix type definition * prevent multiple method calls for clientScripts and requestHooks * fix an error message during module loading * specify execution order for clientScripts and requestHooks * skip test * skip test * refix the test * fix client scripts loading order and handle module path resolving errors
Are you requesting a feature or reporting a bug?
feature suggestion
What is the current behavior?
TestCafe executes a ClientFunction when the page is fully loaded.
What is the expected behavior?
Add ability to execute any client script in the browser before other page scripts.
Here are some use cases:
Catch JS errors and console output
Executing code in the browser before the page code evaluates
Proposed API:
Feel free to vote for the feature if you consider it useful and please let us know about your use case for it.
The text was updated successfully, but these errors were encountered: