我已经阅读了 Cypress 上v6.9.1
关于拦截的文档,但我在真正理解如何匹配单个单词 url 时遇到了一些困难。
我面临的问题的一个实际例子。我提出以下请求以label
在我的应用程序中创建一个:
POST http://localhost:8081/70e70322-1633-4ca9-b1e2-9240cef416e7/label
我会假设,从文档中我可以通过执行以下操作来匹配这条路线:
// My code
cy.intercept("POST", "/label").as("createLabel");
// From the examples in Cypress pages
cy.intercept('GET', '/users')
// matches this: GET http://localhost/users
// ...but not this: POST http://localhost/users
我尝试了以下方法:
// Passing a simple string
cy.intercept("POST", "label").as("createLabel");
// Passing a regex
cy.intercept("POST", /label/g).as("createLabel");
// Passing a glob pattern
cy.intercept("POST", "**/label").as("createLabel");
我对此一头雾水,并没有真正理解拦截基于单个单词的请求的确切方法。这是一个简单的例子,但我在许多其他测试中都遇到了这个问题,为了解决我的问题,我必须拦截所有请求(这使我的测试变得脆弱,而且没有未来的证明):
// This is just a "hacky" way to make it work and avoid the app to
// make a request to my backend after creating another resource
cy.intercept("GET", "*");
问题
如何使赛普拉斯匹配请求中的单个单词?
GET http://localhost/label
-> 请拦截label
URL 上的请求POST http://localhost/user/{userId}/comment
-> 请拦截user
对他们的请求POST http://localhost/user/{userId}/comment
-> 请拦截comment
对他们的请求