我正在我的一项柏树测试中测试搜索功能,并且难以获取请求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?
发布评论
评论(1)
看来有前面的请求使用您的
cy.wait('@myrequest')
。查询参数匹配有点棘手,但是它使用RouteMatcher函数并检查正确的参数,然后分配动态别名。
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.