2

I'm trying to add an 'if' statement into my login test as there are two login pages that can be loaded. The test below is failing when it can't find the text 'Not your account?', when it should be continuing to the login steps. I think this is happening because both login pages have the same elements 'a.auth0-lock-alternative-link' but contain different text.

How can I create an if statement in the test to fulfill both these scenarios?

describe('My Login Test2', function (){
  it('Visit Risk App Landing Page', function (){
    cy.visit('https://bvt-riskassessment.lmkcloud.net')
    cy.get('button').click()
    if(cy.get('a.auth0-lock-alternative-link').contains('Not your account?'))
    {
      cy.get('a.auth0-lock-alternative-link').contains('Not your account?').click();
    }
    cy.fixture('loginUser').as('myUserFixture');
    cy.get('@myUserFixture').then(user => {
      cy.get('input.auth0-lock-input').first().type(user.email);
      cy.get('input.auth0-lock-input').last().type(user.password);
      cy.get('button').click()
      cy.url().should('eq','https://bvt-riskassessment.lmkcloud.net/workflow')
    })
  })
})
4
  • What is the exact error message you receive when the test fails?
    – Kate Paulk
    Commented May 23, 2018 at 11:35
  • Error Reads: CypressError: Timed Out retrying: Expected to find content: 'Not your account?" within the element: <a.auth0-lock-alternative-link> but never did.
    – Adam A
    Commented May 23, 2018 at 12:12
  • without seeing the source of the login page its hard to help you , please could you add that
    – Amias
    Commented May 23, 2018 at 13:12
  • Sorry to be stupid, but what exactly are you asking to see? Do you want to see the html from the browser dev tools?
    – Adam A
    Commented May 23, 2018 at 13:49

1 Answer 1

2

Here:

if(cy.get('a.auth0-lock-alternative-link').contains('Not your account?'))
{
  cy.get('a.auth0-lock-alternative-link').contains('Not your account?').click();
}

to evaluate expression in if statement you call contains('Not your account?') method. Since contains method has the default assertion that makes your test fail once the element does not contain the required value (if you will try to pass the test manually step by step you will realize that there is no such element when you see log-in form the first time).

In other words, contains is not a Boolean function, it just looks up and returns the element that matches the specified conditions. Since there is no matching elements on the page, your code in if(...) fails.

You should query element's text and then check if it meets your conditions. Check this post for details. Pay attention to "If you need to hold a reference or compare values of text".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.