0

在赛普拉斯中,我cy.route()用于发送以下请求,但赛普拉斯没有识别以下请求发送。在路由 url 中有一个 openHash 值,每个 POST 请求都会有所不同。有什么方法可以忽略 openHash 值或接受那里显示的任何值。

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

 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 url 需要完全匹配。有人可以建议吗

Cypress version: 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

Step definition

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 });
});
    

Error: CypressError 超时重试:cy.wait() 超时等待 5000 毫秒以等待对路由的第一个请求:getStudentTabDetails。从未发生过请求。

4

1 回答 1

2

Cypress.minimatch是一个可用于检查路由匹配器的工具。

默认情况下,赛普拉斯使用 minimatch 来针对请求 URL 测试 glob 模式。

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

您在问题中显示的两条路线实际上通过了 minimatch 测试。

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

这是一个赛普拉斯小提琴,展示了如何使用新的拦截方法来处理查询参数。

/// <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() 制作的,如果不使用 polyfill 就不会在旧方法cy.route()捕获

于 2020-12-04T00:06:46.783 回答