我正在测试几个过滤器(它们位于后端),以便使用用before(() => {...})块编写的间谍函数连续测试仪表板:
function aliasQuery(
request: CyHttpMessages.IncomingHttpRequest,
operationName: string,
): void {
const { body } = request;
if (body.operationName === operationName) {
request.alias = operationName;
}
}
export function spyOnGraphQL(operationName: string): void {
cy.fixture('hosts').then(({ graphQLHostname }) => {
cy.intercept(ApiMethods.Post, graphQLHostname, (request) => {
aliasQuery(request, operationName);
});
});
}然后在for循环中使用
cy.wait(`@${operationName}`).should(({response}) => {...})逐个检查过滤器。
但是,在使用每个过滤器并获得结果之后,我需要通过发送另一个graphql请求来重置所有筛选器,这个请求的查询名与过滤器的查询名匹配,因此当再次调用cy.wait时,它捕获重置过滤器请求,该请求破坏了everything.It,如下所示:
cy.wait,它捕获请求1H 216H 117使用cy.wait捕获请求2->问题开始于H 219F 220在应用新的过滤器之前,是否有一种方法可以清除cy.intercept捕获的请求?或者至少区分重置请求和过滤器请求,例如使用请求有效负载?
发布于 2022-08-26 13:46:06
您可以使用times选项确保您的拦截只匹配一次,并在等待请求时重新定义它。示例:
cy.intercept({ url: /\/api\/.*\/path\/.*/, times: 1 }).as("fetchingMyData")
cy.wait("@fetchingMyData").then(() => { ... }) // the first wait
cy.intercept({ url: /\/api\/.*\/path\/.*/, times: 1 }).as("fetchingMyData2") // this resets the alias and the intercepted requests so that any request made before this point won't be considered.
cy.wait("@fetchingMyData2").then(() => { ... }) // the second wait查看文档中的选项以获得更多信息:https://docs.cypress.io/api/commands/intercept#routeMatcher-RouteMatcher
发布于 2021-09-16 06:08:09
我没有找到清理请求队列的方法,但是我可以使用额外的回调来忽略一些请求,给它们提供不同的别名。
function aliasQuery(
request: CyHttpMessages.IncomingHttpRequest,
operationName: string,
callback?: TypeAliasQueryCallback,
): void {
const { body } = request;
if (body.operationName === operationName) {
request.alias = operationName;
if (typeof callback === 'function') {
callback(request);
}
}
}
export function spyOnGraphQL(
operationName: string,
callback?: TypeAliasQueryCallback,
): void {
cy.fixture('hosts').then(({ graphQLHostname }) => {
cy.intercept(ApiMethods.Post, graphQLHostname, (request) => {
aliasQuery(request, operationName, callback);
});
});
}
export function ignoreResetRequest(
request: CyHttpMessages.IncomingHttpRequest,
): void {
const { body } = request;
// Checking if the filters are sent (resetting sends empty array)
if (!body.variables.filter.and.length) {
request.alias = 'ignored';
}
}
spyOnGraphQL('some_operation_name', ignoreResetRequest);https://stackoverflow.com/questions/69195797
复制相似问题