如何使用表单数据在柏树中断言查询参数?

发布于 2025-02-09 11:37:57 字数 1100 浏览 0 评论 0 原文

我正在我的一项柏树测试中测试搜索功能,并且难以获取请求URL的所有部分。在我的页面上,我有一个表格,用户可以输入一个要搜索的数字。当用户单击“搜索”时,系统会提供带有适当数据的AJAX请求。一切都很好。

在我的柏树测试中,我在这样的测试之一中拦截了 get 请求:

cy.intercept('GET', '/api/v1/foo/?include=**').as('myRequest');

...

那就是 get request 当用户单击“提交”按钮至搜索。

在我的测试中,我将文本输入这样的文本字段:

...

cy.get('[data-cy="number"]').type('12345');

文本已正确地插入输入中。 接下来,我正在触发这样的ajax请求:

...

cy.get('[data-cy="search"]').should('exist').click();

cy.wait('@myRequest').then((interception) => {
    console.log(interception.request.url); // /api/v1/forms/?include=foo <-- does not have filter[number]...
    expect(interception.request.url).to
    .include('filter[number]=12345');
});

提交真实请求(不是通过柏树)时,请求URL看起来像这样:

https://example.com/api/v1/foo/?include=bar&filter[number]=12345

但是,当Cypress提出请求时,似乎没有拾取查询字段,并且我的测试失败了。

我还尝试过使用期望(interception.request.query)>>> code> undefined 。

我如何在测试中正确拾取 filter [number] 查询参数?

I am testing search functionality in one of my Cypress tests and having trouble getting to all parts of the request url. On my page, I have a form that a user can enter a number to search for. When a user clicks "Search", the system makes an ajax request with the appropriate data. Everything is working great.

In my Cypress test, I'm intercepting a GET request in one of my tests like this:

cy.intercept('GET', '/api/v1/foo/?include=**').as('myRequest');

...

That is the GET request that is made when a user clicks the submit button to search.

Within my test, I am entering text into a text field like this:

...

cy.get('[data-cy="number"]').type('12345');

The text is getting properly interred into the input.
Next, I am triggering an ajax request like this:

...

cy.get('[data-cy="search"]').should('exist').click();

cy.wait('@myRequest').then((interception) => {
    console.log(interception.request.url); // /api/v1/forms/?include=foo <-- does not have filter[number]...
    expect(interception.request.url).to
    .include('filter[number]=12345');
});

When submitting a real request (not through cypress) the request url looks like this:

https://example.com/api/v1/foo/?include=bar&filter[number]=12345

However, when cypress makes the request, it's seemingly not picking up the query field and my test is failing.

I've also tried using expect(interception.request.query) but it is always undefined.

How can I properly pick up the filter[number] query param in my test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2025-02-16 11:37:57

看来有前面的请求使用您的 cy.wait('@myrequest')

查询参数匹配有点棘手,但是它使用RouteMatcher函数并检查正确的参数,然后分配动态别名。

cy.intercept('/api/v1/foo/?include=**', (req) => {
  if (req.query.include === 'bar' && req.query["filter[number]"] === '12345') {
    req.alias = 'myRequest'   // alias for only these parameters
  }
  req.continue()
})

cy.get('[data-cy="search"]').should('exist').click();

cy.wait('@myRequest').then((interception) => {
  ...

})

It looks like there's preceding requests that are using up your cy.wait('@myRequest').

Query parameter matching is a bit tricky, but it works using a routeMatcher function and checking for the correct params, then assigning a dynamic alias.

cy.intercept('/api/v1/foo/?include=**', (req) => {
  if (req.query.include === 'bar' && req.query["filter[number]"] === '12345') {
    req.alias = 'myRequest'   // alias for only these parameters
  }
  req.continue()
})

cy.get('[data-cy="search"]').should('exist').click();

cy.wait('@myRequest').then((interception) => {
  ...

})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文