Skip to content

Request hooks#2200

Merged
miherlosev merged 7 commits intoDevExpress:masterfrom
miherlosev:request_hooks
Apr 16, 2018
Merged

Request hooks#2200
miherlosev merged 7 commits intoDevExpress:masterfrom
miherlosev:request_hooks

Conversation

@miherlosev
Copy link
Copy Markdown
Collaborator

@miherlosev miherlosev commented Mar 7, 2018

Typedefs will be updated in a separate PR.

Feature has 2 parts: in testcafe and in hammerhead.
Firstly, the testcafe part will be reviewed. After that - the hammerhead (DevExpress/testcafe-hammerhead#1527).

For convenience,in this PR the hammerhead part used as module - see https://github.com/DevExpress/testcafe/compare/master...miherlosev:request_hooks?expand=1#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R106.

Changes:

  • add RequestMock, RequestLogger and RequestHook classes
  • rename ClientFunctionResultPromise to ReExecutablePromise .
  • add API for add/removing request hooks on fixture, page and testcontroller levels
  • add RequestHookConfigureAPIError class
  • move assertThrow to testing helpers

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 107f92f have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 0f0dca7 have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 97d450a have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 4647454 have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit e2a94a2 have failed. See details:

@miherlosev miherlosev force-pushed the request_hooks branch 2 times, most recently from ba8df34 to cf9a52c Compare March 15, 2018 08:46
@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit cf9a52c have passed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 6274729 have failed. See details:

@miherlosev miherlosev changed the title [WIP] Request hooks Request hooks Mar 15, 2018
@miherlosev
Copy link
Copy Markdown
Collaborator Author

@testcafe-build-bot \retest

@miherlosev
Copy link
Copy Markdown
Collaborator Author

Let's start to review this PR.
/cc @AlexanderMoskovkin @AndreyBelym

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 6274729 have passed. See details:

Comment thread src/errors/runtime/type-assertions.js Outdated
getActualValueMsg: (value, type) => isNullOrUndefined(value) ? String(value) : type
},

requestHookInheritor: {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to call it isRequestHookSubclass.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All conditions are named without is keyword: number, nonNegativeNumber and etc. See more details https://github.com/DevExpress/testcafe/blob/master/src/errors/runtime/type-assertions.js#L10.
But the term subclass is more relevant than inheritor for this context.
Links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance

So, the final name - requestHookSubclass.

Comment thread src/api/exportable-lib/index.js Outdated

RequestHook,

ResultPromise,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we need to discuss why this thing is exported.

Copy link
Copy Markdown
Collaborator Author

@miherlosev miherlosev Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Motivation:
Requests are asynchronous. It's impossible to check result of custom request hook without adding timeouts. For example, try to remove PromiseResult for this test - https://github.com/DevExpress/testcafe/pull/2200/files#diff-4b5df15c8ad0e18c0978bf61152150ceR33 and try to fix unstable.

It means that customer cannot create a custom request hook with convenient API.


this.onResponseCallCountInternal = 0;

Object.defineProperty(this, 'onResponseCallCount', {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use ES6 class getter here. Also, IMHO it's better to name it responseHandlerCallCount (and use internalResponseHandlerCallCount for the internal property).

Copy link
Copy Markdown
Collaborator Author

@miherlosev miherlosev Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use ES6 class getter here

Ok. Let it be.

Also, IMHO it's better to name it responseHandlerCallCount (and use internalResponseHandlerCallCount for the internal property).

It is not handler. It is method from base class. Simular as in Reporter plugin.

Variants: onResponseMethodCallCount, responseCallCount and etc.
So, I will keep it as is.

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit f7b28a3 have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 5a0696a have failed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 5a0696a have failed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 5a0696a have passed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

FPR

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 6fe36e5 have passed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit e09a61c have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 0c61cd6 have passed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit b90bb56 have failed. See details:

constructor (name) {
super(pageUrl);

this.name = name;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems the name field is not used anywhere

await t
.click(buttonSelector)
.expect(buttonSelector.textContent).eql('Done')
.expect(logger.contains(r => r.response.body === browserName)).ok();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add check that other requests (except /get-browser-name) are not saved

});
}

onRequest (/*RequestEvent event*/) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this class is abstract this method should throw an error. If a subclass shouldn't do anything here they just should have an empty method

Comment thread src/api/test-controller/index.js Outdated
hooks.forEach(hook => {
if (this.testRun.requestHooks.includes(hook)) {
remove(this.testRun.requestHooks, hook);
this.testRun._disposeRequestHook(hook);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to encapsulate this login in testRun:

this.testRun.addRequestHook
this.testRun.removeRequestHook

// usage:
hooks.forEach(hook => this.testRun.addRequestHook(hook));
hooks.forEach(hook => this.testRun.removeRequestHook(hook));

Comment thread src/test-run/index.js

this.requestHooks = Array.from(this.test.requestHooks);

this._initRequestHooks();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need that function that contains only one line? I guess inline this.requestHooks.forEach(hook => this._initRequestHook(hook)); call is good.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's debatable.
Compare two code parts. Which is more readable?

1

        this.injectable.scripts.push('/testcafe-automation.js');
        this.injectable.scripts.push('/testcafe-driver.js');
        this.injectable.styles.push('/testcafe-ui-styles.css');

        this.requestHooks = Array.from(this.test.requestHooks);

        this._initRequestHooks();

2

        this.injectable.scripts.push('/testcafe-automation.js');
        this.injectable.scripts.push('/testcafe-driver.js');
        this.injectable.styles.push('/testcafe-ui-styles.css');

        this.requestHooks = Array.from(this.test.requestHooks);

        this.requestHooks.forEach(hook => this._initRequestHook(hook));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I Don't see much differences)
In the first case you have to keep in mind that you need to call the _initRequestHooks function after the this.requestHooks = .... In the second case it's obvious from the code.

const pageUrl = 'http://localhost:3000/fixtures/api/es-next/request-hooks/pages/index.html';
const logger = new RequestLogger(pageUrl);

fixture `RequestLogger`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a unit test.

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 443cce4 have passed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

FPR

@AlexanderMoskovkin
Copy link
Copy Markdown
Contributor

@miherlosev rebase to upstream please

@miherlosev
Copy link
Copy Markdown
Collaborator Author

miherlosev commented Apr 2, 2018

@miherlosev rebase to upstream please

It will be do after hammerhead version will be updated.

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 77d24e3 have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit 77d24e3 have passed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

FPR

@miherlosev
Copy link
Copy Markdown
Collaborator Author

ping @AlexanderMoskovkin @AndreyBelym

@miherlosev
Copy link
Copy Markdown
Collaborator Author

@AndreyBelym Review but don't merge until hammerhead version was updated.

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit 5b472c8 have failed. See details:

@timwis
Copy link
Copy Markdown

timwis commented Apr 10, 2018

Hi folks - I hope you'll pardon my question, but the fact that xhr request stubbing isn't already provided in testcafe makes me think I may be missing the point or something. Isn't it the case that, without this feature, you can only use testcafe for end-to-end tests? Or is there another way to stub xhr requests? Requiring my tests hit my production backend API will definitely slow things down. Is it, perhaps, about a flag in my application code on NODE_ENV=test and I should swap out the back-end with fixtures/sample data? Or should I use sinon or nock or something?

@inikulin
Copy link
Copy Markdown
Contributor

@timwis You can use proxy between testcafe and origin to redirect XHR requests (see https://devexpress.github.io/testcafe/documentation/using-testcafe/command-line-interface.html#--proxy-host). However, if you want to do that from your test code you'll need this feature.

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit eceadab have failed. See details:

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

❌ Tests for the commit f370d01 have failed. See details:

@miherlosev
Copy link
Copy Markdown
Collaborator Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Copy Markdown
Collaborator

✅ Tests for the commit f370d01 have passed. See details:

@miherlosev miherlosev merged commit d7db437 into DevExpress:master Apr 16, 2018
@miherlosev miherlosev deleted the request_hooks branch April 20, 2018 09:20
kirovboris pushed a commit to kirovboris/testcafe-phoenix that referenced this pull request Dec 18, 2019
* Request hooks

* fix Alex review issues

* minor changes

* rename 'sessionId ' to 'testRunId' for logged request (RequestLogger)

* fix tests

* use the current hammerhead version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants