pgmendormi
pgmendormi

Reputation: 224

How can I get the url in cypress?

When I click on the button, I try to check if I'm on the right page.

Here is my code:

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('[email protected]')
    cy.get(":input[placeholder='Mot de passe']").type('guigui')
    cy.contains('Se connecter').click() 


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

  })
})

On this code, I am on localhost3000, I click on the parent who has to redirect me to localhost3000/connexion/parent and I want to login but I don't know if it's functioning or not.

I already tried to do different things to print the URL, but none work.

Upvotes: 7

Views: 18558

Answers (2)

PeaceAndQuiet
PeaceAndQuiet

Reputation: 1780

You can get the url with command cy.url() or cy.location().

You would use cy.url() like this:

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

Upvotes: 2

Alapan Das
Alapan Das

Reputation: 18650

You have to use the cy.url().

  1. To print the URL you can use:
cy.url().then((url) => {
  cy.log('Current URL is: ' + url)
})
  1. If you want to validate the URL you can use:
cy.url().should('include', '/connexion/parent')

Upvotes: 10

Related Questions