-
Notifications
You must be signed in to change notification settings - Fork 672
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
Implement request hooks #1341
Comments
Just want to add my use case for this feature. I'm currently building a SPA that has a .net web api backend. At the moment we're trialling using SoapUI for this kind of testing but are finding it a little clunky.
All of this is trivial to achieve in TestCafe as it's actually running in the browser. In our regression environment we then call our API and compare the results to a saved json file. As it's a financial application, some of the saved results can be quite large and property level testing is not appropriate - we usually just want to ensure that the entire response is the same as the previous test run. I'd like to use TestCafe to login, navigate to a report screen, fill out the report parameters, run the report and then intercept the xhr request for the results - this might be an xhr request that I'd like to wait several minutes for.
I'd be happy taking a dependency on another library (think Ava rely on Jest to do a similar assertion) to do that snapshot. If I could do this in TestCafe, then I'd have one coherent strategy and framework for all functional UI and API testing which would be 💯 |
Is there any movement on this issue? Looks like it's planned for development. Any timeframe on this? |
Would love to use this feature too! |
Request HookAPI for adding/removing
where Examples of usage fixture.requestHooks(hook1);
test.requestHooks(hook1, hook2, ...);
fixture.requestHooks([hook1, hook2], hook3);
test.requestHooks([hook1, hook2], hook3, [hook4, hook5]); Exporting classes
RequestLogger
Default values for {
logRequestHeaders: false,
logRequestBody: false,
stringifyRequestBody: false,
logResponseHeaders: false,
logResponseBody: false,
stringifyResponseBody: false
} i.e. by default the APIMethods
Properties
For Example import { RequestLogger } from 'testcafe';
const logger = RequestLogger('https://example.com');
fixture `test`
.page('https://example.com');
test
.requestHooks(logger)
('test', async t => {
await t.expect(logger.contains(r => r.response.statusCode === 200)).ok();
}); Request MockAPIWe build our API depending on nock library.
Example var mock = RequestMock()
.onRequestTo(requestFilterRule1)
.respond(responseMock1)
.onRequestTo(requestFilterRule2)
.respond(responseMock2) where information about Example import { RequestMock } from 'testcafe';
const requestMock = RequestMock()
.onRequestTo('http://external-service.com/api/users'}) /*see RequestFilterRule*/
.respond({data: 123}) /* JSON response */
.onRequestTo(...)
.respond('The error is occured!!!') /*HTML response*/
.onRequestTo (...)
.respond(null, 204) /*custom statusCode*/
.onRequestTo(...)
.respond('<html_markup>', 200, { 'server': 'nginx/1.10.3' }) /* custom headers */
.onRequestTo(...)
.respond(function (req, res){ /* respond function */
res.headers[‘x-calculated-header’] = ‘calculated-value’;
res.statusCode = ‘200’;
const responseBody = fs.readFileSync(req.params['filename']).toString();
res.setBody(responseBody);
});
fixture `Fixture`
.page(‘http://example.com/’)
.rquestHooks(requestMock);
test('test', () => {
await t.click(‘body’);
}) RequestFilterRuleAllows to specify request filtering rule for requests. Examples /*String*/
'http://example.com' -> RequestFilterRule(`'http://example.com'`)
/*Regular expession*/
/example.com/ -> RequestFilterRule(`/example.com/`)
/*Object with properties*/
{ url: 'http://example.com', method: 'GET', isAjax: false } -> RequestFilterRule(`{ url: 'http://example.com', method: 'GET', isAjax: false }`)
/*Custom function*/
RequestFilterRule(function (request) {
return request.url === 'http://example.com' &&
request.method === 'post' &&
request.isAjax &&
request.body === '{ test: true }' &&
request.headers['content-type'] === 'application/json';
} |
@miherlosev is this functionality available somewhere now, or is this just the planned interface? |
@curtisblackwell This is in progress now. @miherlosev already have created a pull request but it requires some time to finish it. If you are interested in this we'll be able to provide you with a dev build once this feature is merged |
This looks really promising. I have a question though. Will you be able to assert that a request-hook has been triggered? |
I am not sure understand your question. |
@AlexanderMoskovkin understood, thank you. I'm interested, but it sounds like timing may be an issue. |
Thinking about it, it's probably not actually needed for integration tests. |
Will you release a dev version so that we can try out the request hooks feature? |
@elgreco247 |
I notice that at least some of this feature is present in the alpha 2 build. Could you explain to me how to use it to achieve what I want to achieve. |
Feature Before testing you need to install the latest alpha version - |
The |
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?
feature
What is the current behavior?
You can't intercept HTTP requests
What is the expected behavior?
Add API to intercept HTTP requests:
RequestHook
abstract class from which implementations could inherit;t.addRequestHook
,test.useRequestHook
andfixture.useRequestHook
methods.t.removeRequestHook
methodsHaving this mechanism we can implement
RequestLogger
(#1270) andRequestMock
(#1271)The text was updated successfully, but these errors were encountered: