尝试 ajax 请求时断开连接。柏

发布于 2025-01-14 13:34:45 字数 1062 浏览 0 评论 0原文

我对自动化测试还很陌生(直到最近我还是一名手动测试员),最近我开始使用 Cypress。

我一直在尝试从下拉列表中选择一个项目。选择一项后,下一个下拉列表应该出现在下面。应发出“POST”请求(如下面的 gif 所示)。

手动测试期间的浏览器行为

从第一个项目中选择一个项目后在赛普拉斯中运行此测试用例时下拉列表 我收到一条以我的母语显示的关于暂时缺少互联网连接且未发出“POST”请求的警报。

下面我提供了一个代码片段:

cy.intercept('POST', 'https://sprzedajemy.pl/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2').as('authenticate')

cy.get(".theButton.theButtonRed.theButtonAdd").click({ force: true });

cy.pause();

cy.get("#category_selector_5>:nth-child(" + id + ")").then(function ($elem) {

   cy.get("#category_selector_5").select($elem.text(),{ force: true });

   cy.wait('@authenticate').its('response.statusCode').should('eq', 200)

});

错误消息,它是波兰语,基本上意味着:“没有互联网连接。请稍后重试。对于给您带来的不便,我们深表歉意

整个测试用例执行过程中的网络选项卡赛普拉斯

提前非常感谢。

I am quite new to automated testing (up until recently I'v been a manual tester) and I've recently taken up Cypress.

I've been trying to select an item from a drop down list. After selecting one item the next drop-down list should occur under. A 'POST' request should be made (as in the gif bellow).

browser behaviour during manual testing

When this test case is runned in Cypress after selecting an item from the first drop-down list I am getting an alert in my native language about a temporary lack of internet connection and no 'POST' request is being made.

Below I provide a code snippet:

cy.intercept('POST', 'https://sprzedajemy.pl/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2').as('authenticate')

cy.get(".theButton.theButtonRed.theButtonAdd").click({ force: true });

cy.pause();

cy.get("#category_selector_5>:nth-child(" + id + ")").then(function ($elem) {

   cy.get("#category_selector_5").select($elem.text(),{ force: true });

   cy.wait('@authenticate').its('response.statusCode').should('eq', 200)

});

error message, it's in Polish, it means basically: "No internet connection. Please try again later. Sorry for the inconvenience

network tab throughout the whole test case execution in Cypress

Many thanks in advance.

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

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

发布评论

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

评论(1

坏尐絯 2025-01-21 13:34:45

匹配 url 有点像魔术,但我认为问题出在 POST url 的 -1647426073 部分。

不确定是 - 还是该部分中缺少字母字符,但它破坏了匹配。

但可以使用通配符使其正常工作。

我没有断开连接,所以也许这是另一个问题。

下面总结了有效和无效的 cy.intercept()。为了方便起见,我使用较长形式的routeMatcher。

const routeMatcher = {
  method: 'POST',

  // url: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2',  //no
  // url: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2', //yes
  // url: '**/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2', //yes
  // url: '**/tmp_id/*?category_id=id_2',  //yes

  // path: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2',  //no
  // path: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2',  //yes
  // path: '**/tmp_id/*?category_id=id_2',  //yes

  // pathname: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073',  //no
  // pathname: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*',  //yes
  // pathname: '**/tmp_id/*',  //yes
  // query: {
  //   category_id: 'id_2'
  // }
}

cy.intercept(routeMatcher).as('authenticate')

一些注释

  • 基本 URL https://sprzedajemy.pl 由 Cypress 假定,因此您可以将其省略

  • * 通配符必须替换整个段(之间的部分>\?),所以 /*?category_id=id_2 但不是/*1647426073?category_id=id_2

  • 您可以通过使用通配符来进一步缩短网址的开头,如下所示**/tmp_id/*

  • pathname: 选项无需查询部分即可工作,或者您可以添加单独查询选项以进行更强的匹配

  • 您可以完全省略 URL 并添加 {times:1} 仅捕获单个 POST 请求。

    const RouteMatcher = {
      方法:'POST',
      次数:1
    }
    cy.intercept(routeMatcher).as('身份验证')
    

断言第二个下拉列表

如果您断言第二个下拉列表的内容,则无需等待 POST 拦截(除非您特别想检查它是否发生)

cy.get('#category_selector_5').select('Motoryzacja')
cy.get('#category_selector_2 option').should('have.length', 16)  // passes

Matching urls is a bit of a black art, but I think the problem is the -1647426073 portion of the POST url.

Not sure if it's the - or the lack of alpha chars in that section, but it breaks the matching.

But it can be wildcarded to make it work.

I'm not getting a disconnection, so maybe that's another issue.

The following summarizes working and not-working cy.intercept(). I'm using the longer form of routeMatcher for convenience.

const routeMatcher = {
  method: 'POST',

  // url: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2',  //no
  // url: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2', //yes
  // url: '**/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2', //yes
  // url: '**/tmp_id/*?category_id=id_2',  //yes

  // path: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073?category_id=id_2',  //no
  // path: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*?category_id=id_2',  //yes
  // path: '**/tmp_id/*?category_id=id_2',  //yes

  // pathname: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/-1647426073',  //no
  // pathname: '/new-offer-manager/ajax-get-category-selectors-box/tmp_id/*',  //yes
  // pathname: '**/tmp_id/*',  //yes
  // query: {
  //   category_id: 'id_2'
  // }
}

cy.intercept(routeMatcher).as('authenticate')

Some notes

  • the base url https://sprzedajemy.pl is assumed by Cypress so you can leave it out

  • the * wildcard must replace the whole segment (part between \ and ?), so /*?category_id=id_2 but not /*1647426073?category_id=id_2

  • you can shorten further by wildcarding the start of the url as per **/tmp_id/*

  • pathname: option works without the query portion, or you can add the query option separately to make a stronger match

  • you can leave out the URL altogether and add a {times:1} to catch only that single POST request.

    const routeMatcher = {
      method: 'POST',
      times: 1
    }
    cy.intercept(routeMatcher).as('authenticate')
    

Asserting the 2nd dropdown

If you assert the contents of the 2nd dropdown, there is no need to wait for the POST intercept (unless you specifically want to check it occurs)

cy.get('#category_selector_5').select('Motoryzacja')
cy.get('#category_selector_2 option').should('have.length', 16)  // passes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文