我们如何忽略cy.route()请求发送中的openHash值 [英] How can we ignore the openHash value in cy.route() request send

查看:61
本文介绍了我们如何忽略cy.route()请求发送中的openHash值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cypress中,我正在使用 cy.route()发送以下请求,但是cypress无法识别以下请求发送.在路由网址中,有一个openHash值,该值对于每个POST请求都会有所不同.有什么方法可以忽略openHash值或接受在那里显示的值.

In Cypress I am using cy.route() for sending the below request, but cypress is not identifying the below request send. In the route url there is a openHash value which will be different for every POST request. Is there any way to ignore the openHash value or accept what ever value displays there.

到目前为止,我已经尝试通过以下方式在路由中提供url.

So far I have tried by giving the url in following ways in route.

 url: '**/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=**',
 url: '**/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=**&ajaxCall=true**',

我相信在使用cy.route()时,POST网址需要完全匹配.有人可以建议

I believe while using cy.route() the POST url need to match exactly. Could someone please advise

赛普拉斯版本:5.4.0

Student.feature

Feature: Update student details

Background: User logged in to application
        Given I should load all of the routes required for tests

Scenario: Update student details
        When I am logged in as the student user
        And I click on "Student" subtab
        And I should see details displayed

步骤定义:

import { Then, When, And } from "cypress-cucumber-preprocessor/steps";

before(() => {
  Then('I should load all of the routes required for tests', () => {
        cy.server();
        cy.route({
           method: 'POST',
           url: '**student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=5fc8329a76e73&ajaxCall=true**',
           delay: 2000
        }).as('getStudentTabDetails');
    })
})   


Then('I am logged in as the student user', () => {
  cy.get('[name=loginUsername]').type("Student1");
  cy.get('[name=loginPassword]').type("somePassword1", { sensitive: true });
  cy.contains('Login').click();
})


Then('I click on {string} subtab', (student) => {
  cy.get('#main a').contains(student).click({force:true});
});
  
Then('I should see details displayed', () => {
  cy.wait('@getStudentTabDetails', { timeout: 5000 });
});
    

错误:CypressError超时重试:cy.wait()超时,等待5000ms到路由的第一个请求:getStudentTabDetails.从未发生过请求.

Error: CypressError Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: getStudentTabDetails. No request ever occurred.

推荐答案

Cypress.minimatch是可用于检查路线匹配器的工具.

Cypress.minimatch is a tool that can be used for checking the route matchers.

默认情况下,赛普拉斯使用minimatch对请求URL进行glob模式测试.

By default Cypress uses minimatch to test glob patterns against request URLs.

如果您正在努力编写正确的模式,则可以通过直接在开发人员工具控制台中进行测试来加快迭代速度.

If you’re struggling with writing the correct pattern you can iterate much faster by testing directly in your Developer Tools console.

您在问题中显示的两条路线实际上通过了最小匹配测试.

The two routes you show in the question actually pass the minimatch test.

const url = 'http://example/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=5fc8329a76e73&ajaxCall=true';

const pattern1 = '**/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=**';
console.log( Cypress.minimatch(url, pattern1) );  // true

const pattern2 = '**/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=**&ajaxCall=true**';
console.log( Cypress.minimatch(url, pattern2) );  // true


这是赛普拉斯小提琴,其中显示了如何使用新的拦截方法处理查询参数.


Here is a Cypress fiddle that shows how to use the new intercept method to handle query parameters.

/// <reference types="@cypress/fiddle" />

const test = {
  html: `
    <p class="text-lg"></p>
    <script>
      setTimeout(() => {
        const url = 'http://example/student/details.php?viewDetails=project&stdCount=1&sectionID=1&openHash=5fc8329a76e73&ajaxCall=true';
        window.fetch(url, { method: 'POST'});
      }, 1000);
    </script>
  `,
  test: `
  cy.intercept({
    method: 'POST',
    url: '/student/details.php',
    query: {
      viewDetails: 'project',   // whatever query parts you care about
      stdCount: '1',
      sectionID: '1'
    }
  }, {})                 // Added an empty stub here, as my url does not actually exist
  .as('getStudentTabDetails');
  cy.wait('@getStudentTabDetails')
  `
}

it('', () => {
  cy.runExample(test)
});

POST 是使用本机fetch()进行的,如果不使用

The POST is made with native fetch(), which would not be captured in the old cy.route() method without using a polyfill.

这篇关于我们如何忽略cy.route()请求发送中的openHash值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆