-
Notifications
You must be signed in to change notification settings - Fork 5
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
TestCafe wait for results #878
Changes from 8 commits
a4c042e
52a14a2
ad31563
cf58836
80d526e
1c3f480
e0fb742
914e2f9
e48ac04
d27a6a6
0881390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { Selector, t } from 'testcafe'; | ||
import { Selector, RequestLogger, t } from 'testcafe'; | ||
import { registerIE11NoCacheHook } from '../../test-utils/testcafe'; | ||
|
||
/** | ||
* Models the user interaction with a {import('@yext/answers-search-ui').VerticalResultsComponent}. | ||
*/ | ||
class VerticalResults { | ||
constructor () { | ||
this._selector = Selector('.yxt-Results') | ||
this._selector = Selector('.yxt-Results'); | ||
this._searchComplete = Selector('.yxt-Results--searchComplete'); | ||
this._resultsWrapper = Selector('.Answers-resultsWrapper'); | ||
this._focusedCard = Selector('.yxt-Card--pinFocused'); | ||
this._getNthCard = index => Selector(`.yxt-Card[data-opts*="${index}"]`); | ||
this._noResults = Selector('.yxt-AlternativeVerticals-noResultsInfo'); | ||
this._resultsCount = Selector('.yxt-VerticalResultsCount-total'); | ||
this._resultsCountStart = Selector('.yxt-VerticalResultsCount-start'); | ||
this._searchQueryUrl = /v2\/accounts\/me\/answers\/vertical\/query/; | ||
} | ||
|
||
/** | ||
|
@@ -66,6 +69,44 @@ class VerticalResults { | |
return this._selector.exists; | ||
} | ||
|
||
/** | ||
* Wait for results to load on page by checking query response status and searchComplete state | ||
* (timeout is set to 10 seconds) | ||
*/ | ||
async waitOnSearchComplete() { | ||
const responseWaitTimeout = 10000; | ||
const waitTimeInterval = 200; | ||
let totalWaitTime = 0; | ||
while (totalWaitTime < responseWaitTimeout && !this.isLoggerResultsPresent()) { | ||
await t.wait(waitTimeInterval); | ||
totalWaitTime += waitTimeInterval; | ||
} | ||
await t.expect(this._searchComplete.exists).ok(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test would still consider passed if |
||
this._queryRequestLogger.clear(); | ||
} | ||
|
||
/** | ||
* Returns true if there exists a query response from logger with status code 200 | ||
* @returns {boolean} | ||
*/ | ||
async isLoggerResultsPresent() { | ||
return await this._queryRequestLogger.contains(r => r.response.statusCode === 200); | ||
} | ||
|
||
/** | ||
* Register a RequestLogger that tracks vertical query requests to given test. | ||
* If browser is IE11, register an Ie11NoCacheHook. | ||
* | ||
* @param {import('testcafe').TestController} testInstance | ||
*/ | ||
async registerLogger(testInstance) { | ||
this._queryRequestLogger = RequestLogger({ | ||
url: this._searchQueryUrl | ||
}); | ||
await testInstance.addRequestHooks(this._queryRequestLogger); | ||
await registerIE11NoCacheHook(testInstance, this._searchQueryUrl); | ||
} | ||
|
||
/** | ||
* Gets the number of pixels that the element's content is scrolled upward | ||
* @returns {Promise<number>} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { RequestHook } from 'testcafe'; | ||
|
||
/** | ||
* IE11NoCacheHook solves the problem of IE11 caching all ajax requests | ||
* by default by adding a 'no-store' header to requests in ie11. | ||
* | ||
* Without this hook, ajax requests cached by IE11 will be ignored | ||
* testcafe's RequestLogger. This is only a problem in IE11. | ||
*/ | ||
export default class IE11NoCacheHook extends RequestHook { | ||
/** | ||
* This comes from the suggestion here on the testcafe github | ||
* https://github.com/DevExpress/testcafe/issues/3780#issuecomment-496955368 | ||
* The --disable-page-caching flag did not work for ie11, and neither | ||
* did trying to set the header in onRequest(), so this workaround was used instead. | ||
* | ||
* @param {Object} event | ||
*/ | ||
_onConfigureResponse (event) { | ||
super._onConfigureResponse(event); | ||
event.setHeader('cache-control', 'no-store'); | ||
} | ||
|
||
async onRequest () { | ||
// We don't need to do anything here, but still need to override the method. | ||
} | ||
|
||
async onResponse () { | ||
// We don't need to do anything here, but still need to override the method. | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ClientFunction } from 'testcafe'; | ||
import IE11NoCacheHook from './ie11nocachehook'; | ||
|
||
/** | ||
* Register the Ie11NoCacheHook, if the current browser is IE11. | ||
* | ||
* @param {import('testcafe').TestController} testInstance | ||
* @param {string} url | ||
*/ | ||
export async function registerIE11NoCacheHook (testInstance, url) { | ||
const isIE11 = await ClientFunction(() => { | ||
return !!window.MSInputMethodContext && !!document.documentMode; | ||
})(); | ||
if (isIE11) { | ||
const ie11Hook = new IE11NoCacheHook(url); | ||
return testInstance.addRequestHooks(ie11Hook); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I know I just recently approved this, but after looking at the SDK half of this item maybe this waitOnSearchComplete should go into its own request logger class instead of inside the VerticalResults component's class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, if we can move this to it's own class we could easily expand it to universal search. Yen, if we have a new class such as
SearchRequestLogger
, we could have aregisterVerticalSearchLogger
and aregisterUniversalSearchLogger
function so that we could set it up for either type of searchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, yeah that sounds good