如何在柏树中获得 url ?

How can I get the url in cypress?

当我点击按钮时,我尝试检查我是否在正确的页面上。

这是我的代码:

describe('espace_parent is functional', () => {
  it('test login parent', () => {

    cy.visit('http://localhost:3000')
    cy.contains('parent').click()
    cy.contains('Espace Parent')



    cy.get(":input[placeholder='Adresse e-mail']").type('guigui@guigui.fr')
    cy.get(":input[placeholder='Mot de passe']").type('guigui')
    cy.contains('Se connecter').click() 


    cy.log('Current URL is ')
    cy.contains("Accueil")

  })
})

在这段代码中,我在 localhost3000 上,我点击必须将我重定向到 localhost3000/connexion/parent 的家长,我想登录,但我不知道它是否正常工作。

我已经尝试做不同的事情来打印 URL,但是 none 工作。

您必须使用 cy.url()

  1. 要打印 URL,您可以使用:
cy.url().then((url) => {
  cy.log('Current URL is: ' + url)
})
  1. 如果你想验证 URL 你可以使用:
cy.url().should('include', '/connexion/parent')

您可以通过命令cy.url() or cy.location()获得url。

您可以这样使用 cy.url()

// with should()
cy.url().should('eq', 'http://localhost:3000/connexion/parent');
cy.url().should('include', '/connexion/parent');
// with then()
cy.url().then(url => cy.log('Current URL is', url);