Home > other >  URL is not changing. After first time login, the page is redirected to T&C page to accept
URL is not changing. After first time login, the page is redirected to T&C page to accept

Time:01-15

Login.spec.js

  it.only('TC02 - Login with valid data as reseller ', () => {
    const id = ut.record(testdata, 2);
    cy.login(id.username, id.password);
    cy.title().should('equal', 'Compass - Home');
    cy.visit('/users');
    cy.logout();
  });

LoginPage.js -> from POM folder

checkHome() {
  const path = '/license-agreement';

  cy.url().then(($url) => {
      if($url.includes(path)) {
          cy.log("Yes")
      } else  {
          cy.log("No")
      }
   })
}

After, login I am calling checkHome method to see if the page is redirected to home or agreement. If its on agreement page then I get accept button and click it.

But when I log url it outputs the previous page url.

Also, this is solved if I put wait in between. But I don't want to use wait and something dynamic solution to this

CodePudding user response:

For testing purposes, you should find a way to avoid this type of conditional testing.

If you cannot then you may want to include the code that calls the checkHome() method. However, to avoid using cy.wait(), you can either add a cy.location('pathname').should('include', '/commmon-path/') if your url's are common for the home page or agreement or query a common element in both pages and append a should (ie cy.get('.common-element-in-home-and-agreement').should('be.visible')).

CodePudding user response:

To wait on the url, instead of .then() try using should() with a function to handle either/or, because should will retry until condition is satisfied

cy.url().should( url => {

  // Assert the url is one of the two expected, retry until passes
  expect(url).to.satisfy(function(url) { 
    return url.includes('/license-agreement') || url.includes('/home')  
  })

  if (url.includes(path)) {
    cy.log("Yes")
  } else  {
    cy.log("No")
  }
})
  •  Tags:  
  • Related