赛普拉斯:检查当前 URL

Cypress: Check current URL

我正在尝试获取 URL,如果执行以下操作,我将在重定向后获取:

  cy.url().then(url => {
    cy.log(url);
  });

然后我得到初始 URL,但不是(新 url),如何在重定向后在 cypress 中得到 url? redirection img (new url)

解决方法: 要获得重定向的 URL 只需简单地添加 cy.get 和您的元素,这样它将等待重定向页面加载。这是对我有用的解决方案:

  cy.get('.crumb > p')
  cy.url().then(url => {
    cy.log(url);
  });

正如你所写的那样,你需要先等待来自新 URL 的元素可见,使用 cy.get('any-element-from-the-new-url'),之后你可以得到新的 URL 只使用 cy.url(),在这种情况下,如果你想记录这个 URL,你可以只使用 cy.log(cy.url())

一种更有意义的方法是在您期望的路径名上添加断言。文档可以阐明 cy.location()。在不知道所有 url 字符串的情况下,您可以对路径名使用包含断言。

// visiting url - https://website.com/favorites
// redirects to - https://website.com/favorites/regional/local

cy.location('pathname') // yields "/favorites/regional/local"
  .should('include', '/favorites/')
  .then(cy.log)